using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Guzhva
{
    class Program
    {
        public static long MinZbir(long[,] matrica)
        {
            long[,] dp = new long[matrica.GetLength(0) + 1, matrica.GetLength(1) + 1];
            dp[0, 0] = 0;
            dp[0, 1] = 0;
            dp[1, 0] = 0;
            for (int i = 2; i < dp.GetLength(0); i++) 
            {
                dp[i, 0] = matrica[i-2,0];
                for(int x=0;x<i;x++)
                {
                    dp[i, 0] += dp[x, 0];
                }    
            }
            for (int j = 2; j < dp.GetLength(1); j++)
            {
                dp[0, j] = matrica[0, j-2];
                for (int x = 0; x < j; x++)
                {
                    dp[0,j] += dp[0, x];
                }
            }
            for (int i = 1; i <dp.GetLength(0); i++)
            {
                for (int j = 1; j < dp.GetLength(1); j++)
                {
                    dp[i, j] = Math.Min(dp[i - 1, j], dp[i, j - 1]) + matrica[i - 1, j - 1];
                }
            }
            return dp[matrica.GetLength(0), matrica.GetLength(1)];
        }

        static void Main(string[] args)
        {
            string[] unos = Console.ReadLine().Split();
            long N = int.Parse(unos[0]);
            long M = int.Parse(unos[1]);
            long[,] matrica = new long[N, M];
            for(long i=0;i<N;i++)
            {
                unos = Console.ReadLine().Split();
                for(long j=0;j<M;j++)
                {
                    matrica[i, j] = long.Parse(unos[j]);
                }
            }
            long Q = long.Parse(Console.ReadLine());
            for(long i=0;i<Q;i++)
            {
                unos = Console.ReadLine().Split();
                long sacuvaj = matrica[long.Parse(unos[0])-1, long.Parse(unos[1])-1];
                matrica[long.Parse(unos[0])-1, long.Parse(unos[1])-1] = int.MaxValue;
                Console.WriteLine(MinZbir(matrica));
                matrica[long.Parse(unos[0])-1, long.Parse(unos[1])-1] = sacuvaj;
            }
        }
    }
}