using System;

class GFG
{

    static void scenariji1(long [,] cost,long x,long y,long[,] cost1)
    {
 
        cost[x-1, y-1] = int.MaxValue;
        



        Console.WriteLine(minCost(cost, cost.GetLength(0) - 1, cost.GetLength(1) - 1));
    }

    static long min(long x, long y, long z)
    {
        if (x < y)
            return ((x < z) ? x : z);
        else
            return ((y < z) ? y : z);
    }

    static long minCost(long[,] cost, long m, long n)
    {
        if (n < 0 || m < 0)
            return int.MaxValue;
        else if (m == 0 && n == 0)
            return cost[m, n];
        else
            return cost[m, n]
                + min(int.MaxValue,
                      minCost(cost, m - 1, n),
                      minCost(cost, m, n - 1));
    }

    
    public static void Main()
    {

        string t = Console.ReadLine();
        string[] t1 = t.Split(' ');
        long a = long.Parse(t1[0]);
        long b = long.Parse(t1[1]);
      

        long[,] cost = new long[a,b];
        long[,] cost1 = new long[a,b];
        for (long i = 0; i < a; i++)
        {
            t = Console.ReadLine();
            t1 = t.Split(' ');
            for (long j = 0; j < b; j++)
            {
                cost[i,j]= long.Parse(t1[j]);
                cost1[i, j] = long.Parse(t1[j]);
            }
        }
        
        long scenariji= long.Parse(Console.ReadLine());
        long[,] scenarijiMAT = new long[scenariji, 2];
        for (long i = 0; i < scenariji; i++)
        {
            t = Console.ReadLine();
            t1 = t.Split(' ');
            scenarijiMAT[i,0] = long.Parse(t1[0]);
            scenarijiMAT[i,1] = long.Parse(t1[1]);

           
        }

        for (long i = 0; i < scenariji; i++)
        {
            scenariji1(cost, scenarijiMAT[i, 0], scenarijiMAT[i, 1],cost1);
            cost = cost1;


        }

    }
}