#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;
        int jx = 0;
        int jy = 0;
        int br = 0;
        while ( jx != n - 1 && jy != m - 1)
        {
            if ( polje[jx + 1] < polje[jy + 1] && ( jx + 1 != x && jy + 1 != y))
            {
                br += polje[jx + 1];
                jx ++;
            }
            else
            {
                br += polje[jy + 1];
                jy ++;
            }
        }
        cout << br << endl;
    }
}