#include <bits/stdc++.h>
using namespace std;
int main(){
    int n,m;
    cin >> n >> m;
    int polje[n][m];
    for ( int i = 0 ; i < n ; i ++ )
        for ( int j = 0 ; j < m ; j++ )
            cin >> polje[i][j];
    int q;
    cin >> q;
    while(q--)
    {
        int x,y;
        cin >> x >> y;
        x = x - 1;
        y = y - 1;
        int jx = 0;
        int jy = 0;
        int br = 0;
        while ( jx != n - 1 && jy != m - 1)
        {
            if ( jx + 1 == x && jy == y)
            {
                br += polje[jx][jy+1];
                jy++;
            }
            else if ( jx == x && jy + 1== y)
            {
                br += polje[jx+1][jy];
                jx++;
            }
            else
            {
                if ( polje[jx][jy+1] < polje[jx+1][jy])
                {
                    br += polje[jx][jy+1];
                    jy++;
                }
                else
                {
                    br += polje[jx+1][jy];
                    jx++;
                }
            }
        }
        cout << br + polje[0][0] + polje[n-1][m-1]<< endl;
    }
}