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

namespace Guzva
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split();
            long n = Convert.ToInt64(s[0]);
            long m = Convert.ToInt64(s[1]);
            long[,] matrica = new long[n, m];
            for(long i = 0; i < n; i++)
            {
                s = Console.ReadLine().Split();
                for (long j = 0; j < m; j++)
                {
                    matrica[i, j] = Convert.ToInt64(s[j]);
                }
            }
            long slucajeviq = Convert.ToInt64(Console.ReadLine());
            long [,] slucajevi = new long[slucajeviq, 2];
            for(int i = 0; i < slucajeviq; i++)
            {
                s = Console.ReadLine().Split();
                slucajevi[i, 0] = Convert.ToInt64(s[0]);
                slucajevi[i, 1] = Convert.ToInt64(s[1]);
            }
            for(int q1 = 0; q1 < slucajeviq; q1++)
            {
                long[,] resenje = new long[n + 1, m + 1];
                long N = resenje.GetLength(0);
                long M = resenje.GetLength(1);
                long p = slucajevi[q1, 0];
                long q = slucajevi[q1, 1];
                long kofa = matrica[p - 1, q - 1];
                matrica[p - 1, q - 1] = 100000000000;
                for (long i = 1; i < N; i++)
                {
                    for (long j = 1; j < M; j++)
                    {
                        if(resenje[i-1,j] == 0 && resenje[i, j - 1] != 0)
                        {
                            resenje[i, j] = matrica[i - 1, j - 1] + resenje[i, j-1];
                        }
                        else if(resenje[i,j-1] == 0 && resenje[i - 1, j] != 0)
                        {
                            resenje[i, j] = matrica[i - 1, j - 1] + resenje[i-1, j];
                        }
                        else
                        {
                            resenje[i, j] = matrica[i - 1, j - 1] + Math.Min(resenje[i - 1, j], resenje[i, j - 1]);
                        }
                    }
                }
                Console.WriteLine(resenje[n, m]);
                matrica[p - 1, q - 1] = kofa;
            }
        }
    }
}