using System;

namespace ConsoleApp55
{
    class Program
    {
        static void ispitaj(int x, int y, int n, int m, long[,] tabela, long[,] pomocna)
        {
            pomocna[0, 0] = tabela[0, 0];
            for (int i = 1; i < n; i++)
                pomocna[i, 0] = pomocna[i - 1, 0] + tabela[i, 0];
            for (int j = 1; j < m; j++)
                pomocna[0, j] = pomocna[0, j - 1] + tabela[0, j];
            for (int i = 1; i < n; i++)
                for (int j = 1; j < m; j++)
                {
                    pomocna[i, j] = pomocna[i - 1, j] < pomocna[i, j - 1] ? pomocna[i - 1, j] + tabela[i, j] : pomocna[i, j - 1] + tabela[i, j];
                }
            Console.WriteLine(pomocna[n-1, m-1]);
        }
        static void Main(string[] args)
        {
            // unos
            string[] unos = Console.ReadLine().Split();
            int n = int.Parse(unos[0]), m = int.Parse(unos[1]);
            long[,] tabela = new long[n, m], pomocna = new long[n, m];
            for (int i = 0; i<n; i++)
            {
                unos = Console.ReadLine().Split();
                for (int j = 0; j < m; j++)
                    tabela[i, j] = long.Parse(unos[j]);
            }
            int q = int.Parse(Console.ReadLine());
            int[] x = new int[q], y = new int[q];
            for (int i=0; i<q; i++)
            {
                unos = Console.ReadLine().Split();
                x[i] = int.Parse(unos[0])-1; y[i] = int.Parse(unos[1])-1;
            }

            // ispitivanje
            for (int i = 0; i < q; i++)
            {
                long pamti = tabela[x[i], y[i]];
                tabela[x[i], y[i]] = 10000000000; // vece od najveceg sigurno
                ispitaj(x[i], y[i], n, m, tabela, pomocna);
                tabela[x[i], y[i]] = pamti;
            }
        }
    }
}