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

namespace ConsoleApplication333
{
    class Program
    {
        static long met(int[,] a, int n, int m, int x, int y, int xp, int yp, long[,] m1)
        {
            if (xp == n - 1 && yp == m - 1)
            {
                return a[xp, yp];
            }
            if (m1[xp, yp] != -1)
            {
                return m1[xp, yp];
            }
            if (xp == n - 1)
            {
                m1[xp, yp] = a[xp, yp] + met(a, n, m, x, y, xp, yp + 1, m1);
                return m1[xp, yp];
            }
            if (yp == m - 1)
            {
                m1[xp, yp] = a[xp, yp] + met(a, n, m, x, y, xp+1, yp, m1);
                return m1[xp, yp];
            }
            long des = met(a, n, m, x, y, xp + 1, yp, m1);
            long dole = met(a, n, m, x, y, xp, yp + 1, m1);
            m1[xp, yp] = a[xp, yp] + Math.Min(des, dole);
            return m1[xp, yp];
        }
        static void Main(string[] args)
        {
            string[] str123 = Console.ReadLine().Split();
            int n = int.Parse(str123[0]);
            int m = int.Parse(str123[1]);
            int[,] a = new int[n, m];
            for (int i = 0; i < n; i++)
            {
                string[] str1 = Console.ReadLine().Split();
                for (int j = 0; j < m; j++)
                {
                     a[i, j] = int.Parse(str1[j]);
                }
            }
            int q = int.Parse(Console.ReadLine());
            long[,] m1 = new long[n, m];
            for (int i = 0; i < q; i++)
            {
                for (int k = 0; k < n; k++)
                {
                    for (int k1 = 0; k1 < m; k1++)
                    {
                        m1[k, k1] = -1;
                    }
                }
                str123 = Console.ReadLine().Split();
                int x = int.Parse(str123[0]) - 1;
                int y = int.Parse(str123[1]) - 1;
                m1[n - 1, m - 1] = a[n - 1, m - 1];
                m1[x, y] = int.MaxValue;
                long s = met(a, n, m, x, y, 0, 0, m1);
                Console.WriteLine(s);
            }
            Console.ReadKey();
        }
    }
}