using System;

namespace Guzva
{
    class Program
    {
        static long Min(long m, long n, long[,] matrica)
        {
            long[,] mat = new long[m, n];
            for (long i = 0; i < m; i++)
            {
                for (long j = 0; j < n; j++)
                {
                    if (i == 0 && j == 0) mat[i, j] = matrica[i, j];
                    else if (i == 0 && j != 0) mat[i, j] = matrica[i, j] + mat[i, j - 1];
                    else if (i != 0 && j == 0) mat[i, j] = matrica[i, j] + mat[i - 1, j];
                    else mat[i, j] = Math.Min(mat[i - 1, j], mat[i, j - 1]) + matrica[i, j];
                }
            }
            return mat[m - 1, n - 1];
        }

        static void Main(string[] args)
        {
            string[] unos = Console.ReadLine().Split();
            long m = long.Parse(unos[0]);
            long n = long.Parse(unos[1]);
            long[,] matrica = new long[m, n];
            for (long i = 0; i < m; i++)
            {
                string[] unos1 = Console.ReadLine().Split(" ");
                for (long j = 0; j < n; j++)
                {
                    matrica[i, j] = long.Parse(unos1[j]);
                }
            }
            long brojBlokiranih = long.Parse(Console.ReadLine());
            long[] xBlokiranih = new long[brojBlokiranih];
            long[] yBlokiranih = new long[brojBlokiranih];
            for (long i = 0; i < brojBlokiranih; i++)
            {
                string[] unosBlokirani = Console.ReadLine().Split();
                xBlokiranih[i] = long.Parse(unosBlokirani[0]);
                yBlokiranih[i] = long.Parse(unosBlokirani[1]);
            }
            for (long i = 0; i < brojBlokiranih; i++)
            {
                long temp = 0;
                temp = matrica[xBlokiranih[i] - 1, yBlokiranih[i] - 1];
                matrica[xBlokiranih[i] - 1, yBlokiranih[i] - 1] = long.MaxValue / 10;
                long rez = Min(m, n, matrica);
                Console.WriteLine(rez);
                matrica[xBlokiranih[i] - 1, yBlokiranih[i] - 1] = temp;
            }
        }
    }
}