#include <iostream>

using namespace std;

int main()
{
    int n, m;

    cin >> n >> m;

    int mat[n][m];

    for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) cin >> mat[i][j];

    int t;

    cin >> t;

    int xb, yb;

    int res[t];

    for(int i = 0; i < t; i++){

        res[i] = 0;

        cin >> xb >> yb;

        if(xb == 1){

            res[i] += mat[0][0];

            for(int j = 0; j < m; j++) res[i] += mat[1][j];

        }

        else if(xb == 2){

            res[i] += mat[n-1][m-1];

            for(int j = 0; j < m; j++) res[i] += mat[0][j];
        }

    }

    for(int i = 0; i < t; i++) cout << res[i] << endl;

    return 0;
}