#include <bits/stdc++.h>
#define N_MAX 10001
#define M_MAX 10001
#define INF 1000000000

using namespace std;

long long int dp[N_MAX][M_MAX];
long long int mat[N_MAX][M_MAX];

long long int minPath(int blocked_x, int blocked_y, int n, int m) {
    fill(dp[0], dp[m - 1], 0);
    blocked_x--;
    blocked_y--;
    dp[0][0] = mat[0][0];
    for(int i = 1; i < n; i++) {
        if(i == blocked_x && 0 == blocked_y) {
            dp[i][0] = INF;
        } else {
            dp[i][0] = dp[i - 1][0] + mat[i][0];
        }
    }
    for(int j = 1; j < m; j++) {
        if(0 == blocked_x && j == blocked_y) {
            dp[0][j] = INF;
        } else {
            dp[0][j] = dp[0][j - 1] + mat[0][j];
        }
    }
    for(int i = 1; i < n; i++) {
        for(int j = 1; j < m; j++) {
            if(i == blocked_x && j == blocked_y) {
                dp[i][j] = INF;
            } else {
                dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + mat[i][j];
            }
        }
    }
    return dp[n - 1][m - 1];
}

int main() {
    ios_base::sync_with_stdio(NULL);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            cin >> mat[i][j];
        }
    }
    int q;
    cin >> q;
    while(q--) {
        int x, y;
        cin >> x >> y;
        cout << minPath(x, y, n, m) << "\n";
    }
    return 0;
}