using System;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] S = Console.ReadLine().Split();
            int N = int.Parse(S[0]), M = int.Parse(S[1]), x;
            int[,] a = new int[N, M];
            for (int i = 0; i < N; i++)
            {
                S = Console.ReadLine().Split();
                for (int j = 0; j < M; 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[N, M];
                for (int i = 0; i < N; i++)
                {
                    for (int j = 0; j < M; 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[N - 1, M - 1]);
            }
        }
    }
}