#include<bits/stdc++.h>
using namespace std;
int n,m,a[10000][10000],x[100000],y[100000],q;
int zbir;
int kord1, kord2;
int l;
///namesti vrednosti
int main ()
{
    cin >> n >> m;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            cin >> a[i][j];
        }
    }


    cin >> q;
    for(int k=1; k<=q; k++)
    {
        zbir=a[1][1];
        cin >> x[k] >> y[k]; ///moras staviti da je ovo 10e18 ali vratiti posle!!!
        l=a[x[k]][y[k]];
        a[x[k]][y[k]]=2147483647;
        kord1=1; kord2=1;

        while(kord1!=n and kord2!=m)
        {

            if(a[kord1+1][kord2]>=a[kord1][kord2+1] and kord2+1!=m or (kord2+1==m and y[k]!=m))
            {
                zbir=zbir+a[kord1][kord2+1];
                kord2=kord2+1;
            }

            else
            {
                zbir=zbir+a[kord1+1][kord2];
                kord1=kord1+1;
            }

        }

        cout << zbir+a[n][m] << endl;
        zbir=0;
        a[x[k]][y[k]]=l;
    }
}