#include<bits/stdc++.h>

using namespace std;
#define BLOK 1000000888

unsigned long long blokada (vector<vector<int> > &a, int n, int m, int
x, int y) {

unsigned long long dp[n + 1][m + 1] = {0};
dp[x][y] = BLOK;

for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (dp[i][j] != BLOK)
                dp[i][j] = a[i][j];
                 //else dp[i][j] = BLOK;
                 //   dp[i][j]=a[i][j];
if ((i == 1) && (j>1)) {
dp[i][j] += (dp[i][j - 1]);
}
else if ((j == 1) && (i>1)) {
dp[i][j] += (dp[i - 1][j]);
}
else if (i > 1 && j > 1)
dp[i][j] += min(dp[i - 1][j], dp[i][j - 1] );
}
}

/*for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++)
            cerr << dp[i][j]<<' ';
            cerr<<endl;
}*/


return dp[n][m];
}


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

int m, n, q;
cin >> n >> m;
vector< vector <int> > a(n + 1, vector<int> (m + 1, 0));
    for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
            cin >> a[i][j];

    cin>>q;
while (q--) {
int x, y;
cin >> x >> y;
cout << blokada(a, n, m, x, y)<<endl;
}



return 0;
}