using System;

namespace guzva
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int n, m;
            string[] str = Console.ReadLine().Split();
            n = int.Parse(str[0]);
            m = int.Parse(str[1]);
            int[,] a = new int[n, m];
            for(int i = 0; i < n; i++)
            {
                str = Console.ReadLine().Split();
                for (int j = 0; j < m; j++)
                    a[i, j] = int.Parse(str[j]);
            }

            int[,] b = new int[n, m];
            b[n - 1, m - 1] = a[n - 1, m - 1];
            int q = int.Parse(Console.ReadLine());
            int x, y, t;
            
            for(int i = 0; i < q; i++)
            {
                str = Console.ReadLine().Split();
                x = int.Parse(str[0]);
                y = int.Parse(str[1]);
                x--;
                y--;
                t = b[x, y];
                b[x, y] = int.MaxValue;

                for (int j = m - 2; j >= 0; j--)
                    b[n - 1, j] = a[n - 1, j] + b[n - 1, j + 1];

                for (int j = n - 2; j >= 0; j--)
                    b[j, m - 1] = a[j, m - 1] + b[j + 1, m - 1];

                if (x == n - 1)
                {
                    for (int j = y; j >= 0; j--)
                        b[x, j] = int.MaxValue;
                }
                else if (y == m - 1)
                {
                    for (int j = x; j >= 0; j--)
                        b[j, y] = int.MaxValue;
                }

                for (int j = n - 2; j >= 0; j--)
                    for (int k = m - 2; k >= 0; k--)
                    {
                        if (x == j && y == k) continue;
                        b[j, k] = a[j, k] + Math.Min(b[j + 1, k], b[j, k + 1]);
                    }
                Console.WriteLine(b[0, 0]);
                b[x, y] = t;
            }


        }
    }
}