#include <iostream>
#include <limits>
#define INF std::numeric_limits<int>::max()
using namespace std;

int n, m;

int main(void) {

    cin >> n >> m;

    int a[100][100];

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> a[i][j];
        }
    }
    
    int q;
    cin >> q;
    for(int k=0; k<q; k++) {
        int x, y;
        cin >> x >> y;
        x--;
        y--;
        int mat[n][m] = { 0 };

        mat[0][0] = a[0][0];
        mat[x][y] = INF;

        for(int i=1; i<n; i++) {
            if(mat[i][0] == INF) continue;
            else if(mat[i-1][0] == INF) mat[i][0] = INF;
            else mat[i][0] = a[i][0] + mat[i-1][0];
        }
        for(int j=1; j<m; j++) {
            if(mat[0][j] == INF) continue;
            else if(mat[0][j-1] == INF) mat[0][j] = INF;
            else mat[0][j] = a[0][j] + mat[0][j-1];
        }

        for(int i=1; i<n; i++) {
            for(int j=1; j<m; j++) {
                if(mat[i][j] != INF)
                    mat[i][j] = min(mat[i-1][j], mat[i][j-1]) + a[i][j];
            }
        }
        cout << mat[n-1][m-1] << "\n";
    }
    

    return 0;
}