using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Guzva2
{
    class Program
    {
        struct cvor
        {
            public int x, y, z;
        }
        static cvor najmanji(int[,] cvorovi, bool[,] stalni, int n, int m)
        {
            cvor min = new cvor();
            min.z = int.MaxValue;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++)
                    if (!stalni[i, j])
                        if (cvorovi[i, j] != null)
                        {
                            if (cvorovi[i, j] < min.z)
                            {
                                min.z = cvorovi[i, j];
                                min.x = i;
                                min.y = j;
                            }
                        }
            return min;
        }
        static int dijkstra(int[,] tezina, int n, int m, int x, int y)
        {
            int[,] cvorovi = new int[n, m];
            bool[,] stalni = new bool[n, m];
            for (int p = 0; p < n; p++)
                for (int q = 0; q < m; q++)
                {
                    cvorovi[p, q] = int.MaxValue;
                    stalni[p, q] = false;
                }
            cvorovi[0, 0] = tezina[0, 0];
            stalni[0, 0] = true;
            int i = 0, j = 0;
            cvor min = new cvor();
            min.x = 0;
            min.y = 0;
            min.z = cvorovi[0, 0];
            x--;
            y--;
            while (!(i == n - 1 && j == m - 1))
            {
                if (i + 1 == x && j == y)
                {
                    if (j == m - 1)
                    {
                        cvorovi[i + 1, j - 1] = cvorovi[i, j - 1] + tezina[i + 1, j - 1];
                        i++;
                        j--;
                    }
                    else
                    {
                        cvorovi[i, j + 1] = cvorovi[i, j] + tezina[i, j + 1];
                        j++;
                    }
                }
                else if (i == x && j == y - 1)
                {
                    if (i == n - 1)
                    {
                        cvorovi[i - 1, j + 1] = cvorovi[i - 1, j] + tezina[i - 1, j + 1];
                        j++;
                        i--;
                    }
                    else
                    {
                        cvorovi[i + 1, j] = cvorovi[i, j] + tezina[i + 1, j];
                        i++;
                    }
                }
                else
                {
                    if (j != m - 1)
                    {
                        if (cvorovi[i, j] + tezina[i, j + 1] < cvorovi[i, j + 1])
                            cvorovi[i, j + 1] = cvorovi[i, j] + tezina[i, j + 1];
                    }
                    if (i != n - 1)
                    {
                        if (cvorovi[i, j] + tezina[i + 1, j] < cvorovi[i + 1, j])
                            cvorovi[i + 1, j] = cvorovi[i, j] + tezina[i + 1, j];
                    }
                    min = najmanji(cvorovi, stalni, n, m);
                    i = min.x;
                    j = min.y;
                }
                stalni[i, j] = true;
            }
            return cvorovi[n - 1, m - 1];
        }
        static void Main(string[] args)
        {
            string[] str = Console.ReadLine().Split();
            int n = int.Parse(str[0]);
            int m = int.Parse(str[1]);
            int[,] tezina = new int[n, m];
            for (int i = 0; i < n; i++)
            {
                str = Console.ReadLine().Split();
                for (int j = 0; j < m; j++)
                    tezina[i, j] = int.Parse(str[j]);
            }
            int q = int.Parse(Console.ReadLine());
            for (int i = 0; i < q; i++)
            {
                str = Console.ReadLine().Split();
                int x = int.Parse(str[0]);
                int y = int.Parse(str[1]);
                Console.WriteLine(dijkstra(tezina, n, m, x, y));
            }
        }
    }
}