using System;

namespace Takmicenje_zadatak_3
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string s = Console.ReadLine();
            string[] values = s.Split(' ');
            int N = int.Parse(values[0]);
            int M = int.Parse(values[1]);
            int[,] mapa = new int[N,M];
            for (int i = 0; i < N; i++)
            {
                string bla = Console.ReadLine();
                string[] red = bla.Split(' ');
                for (int j = 0; j < M; j++)
                {
                    mapa[i,j] = int.Parse(red[j]);
                }
            }
            int br_pitanja = int.Parse(Console.ReadLine());
            int[] blockred = new int[br_pitanja];
            int[] blockkolone = new int[br_pitanja];
            for (int i = 0; i < br_pitanja; i++)
            {
                string bla2 = Console.ReadLine();
                string[] val = bla2.Split(' ');
                blockred[i] = int.Parse(val[0]);
                blockkolone[i] = int.Parse(val[1]);
            }


            for (int i = 0; i < br_pitanja; i++)
            {

                int original_vrednost = mapa[blockred[i] - 1, blockkolone[i] - 1];

                int block_red = blockred[i] - 1;
                int block_kolona = blockkolone[i] - 1; 

                mapa[block_red, block_kolona] = int.MaxValue;




                int start_r = 0;
                int start_k = 0;
                int end_r = N - 1;
                int end_k = M - 1;
                int cost = mapa[0, 0];

                while (start_r != end_r || start_k != end_k)
                {
                    if (start_r + 1 < N && start_k + 1 < M)
                    {
                        //ako je blok u poslednjem redu, onda ne sme da se sidje u taj red dok se ne prodje blok.
                        if (start_r + 1 == block_red && start_k < block_kolona)
                        {
                            cost += mapa[start_r, start_k + 1];
                            start_k++;
                        }
                        else if (start_k + 1 == block_kolona && start_r < block_red)
                        {
                            cost += mapa[start_r + 1, start_k];
                            start_r++;
                        }
                        else
                        {
                            //poredi samo costs.                                
                            if (mapa[start_r + 1, start_k] < mapa[start_r, start_k + 1])
                            {
                                cost += mapa[start_r + 1, start_k];
                                start_r++;
                            }
                            else
                            {
                                cost += mapa[start_r, start_k + 1];
                                start_k++;
                            }
                        }
                    }
                    else if (start_r + 1 == N)
                    {
                        //nema vise redova
                        cost += mapa[start_r, start_k + 1];
                        start_k++;
                    }
                    else if (start_k + 1 == M)
                    {
                        //nema vise kolona
                        cost += mapa[start_r + 1, start_k];
                        start_r++;
                    }
                }

                Console.WriteLine(cost);

                mapa[blockred[i] - 1, blockkolone[i] - 1] = original_vrednost;

            }
        }
    }
}