using System;

namespace Trobojka
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split();
            int m = int.Parse(s[0]),n=int.Parse(s[1]),x;
            int[,] a = new int[m, n];
            for (int i = 0; i < m; i++)
            {
                s = Console.ReadLine().Split();
                for (int j = 0; j < n; j++)
                    a[i, j] = int.Parse(s[j]);
            }
            int Q = int.Parse(Console.ReadLine());
            int[,] b = new int[Q, 2];
            for (int i = 0; i < Q; i++)
            {
                s = Console.ReadLine().Split();
                b[i, 0] = int.Parse(s[0])-1; 
                b[i, 1] = int.Parse(s[1])-1;
            }

            for (int t = 0; t < Q; t++)
            {
                x = a[b[t, 0], b[t, 1]];
                a[b[t, 0], b[t, 1]] = 1000000001;
                int[,] k = new int[m, n];
                for (int i = 0; i < m; i++)
                {
                    for (int j = 0; j < n; j++)
                    {
                        if (i == 0 && j == 0) k[i, j] = a[i, j];
                        else k[i, j] = a[i, j] + Math.Min((i==0?int.MaxValue:k[i-1,j]),(j==0?int.MaxValue:k[i,j-1]));
                    }
                }

                a[b[t, 0], b[t, 1]] = x;
                Console.WriteLine(k[m-1,n-1]);
            }
        }
    }
}