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

namespace _27_9
{
    class Program
    {
        static void Main(string[] args)
        {
            string[]s= Console.ReadLine().Split(' ');
            int n=int.Parse(s[0]);
            int m=int.Parse(s[1]);
            int[,] a = new int[n, m];
            for(int i=0;i<n;i++)
            {
                s = Console.ReadLine().Split(' ');
                for (int j = 0; j < m; j++)
                    a[i, j] = int.Parse(s[j]);
            }
            int q=int.Parse(Console.ReadLine());
            int[,] b = new int[q, 2];
            for(int i=0;i<q;i++)
            {
                s = Console.ReadLine().Split(' ');
                b[i,0]=int.Parse(s[0]);
                b[i,1]=int.Parse(s[1]);
            }
            int pi = 0, pj = 0, nav = a[0,0];
            for(int k=0;k<q;k++)
            {
                while(pi<n-1&&pj<m-1)
                {
                    if (a[pi + 1, pj] <= b[pi, pj + 1] && pi != a[k, 0] && pj != a[k,1])
                    {
                        pi++;
                        nav += a[pi + 1, pj];
                    }
                    else
                    {
                        if (pi!= a[k, 0] && pj != a[k,1])
                        {
                            pj++;
                            nav += a[pi, pj + 1];
                        }
                        else
                        {
                            pi++;
                            nav += a[pi + 1, pj];
                        }
                    }
                }
                if(pi==n-1)
                {
                    while(pj != m - 1)
                    {
                        pj++;
                        nav += a[pi, pj + 1];
                    }
                }
                else
                {
                    while (pi != n - 1)
                    {
                        pi++;
                        nav += a[pi+1,pj];
                    }
                }
                Console.WriteLine("{0}",nav);
            }
        }
    }
}