#include <bits/stdc++.h>
#define ll long long
#define INF 1000000000000010

using namespace std;

int main()
{

    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    ll N, M, Q, X, Y;
    cin >> N >> M;
    vector <vector<ll> > A(N+2, vector <ll> (M + 2, INF));
    for(int i = 1; i <= N; i++) {for(int j = 1; j <= M; j++) cin >> A[i][j];}
    cin >> Q;
    while(Q--)
    {
        cin >> X >> Y;
        vector <vector<ll> > sad(N+2, vector <ll> (M+2, INF));
        vector <vector<ll> > tren_mini(N+2, vector <ll> (M+2, 0));
        for(int i = 1; i <= N; i++) {for(int j = 1; j <= M; j++) sad[i][j] = A[i][j];}
        sad[X][Y] = INF;
        tren_mini[1][1] = sad[1][1];
        for(int i = 2; i <= N; i++) tren_mini[i][1] = tren_mini[i - 1][1] + sad[i][1];
        for(int j = 2; j <= M; j++) tren_mini[1][j] = tren_mini[1][j - 1] + sad[1][j];
        for(int i = 2; i <= N; i++) {for(int j = 2; j <= M; j++) {tren_mini[i][j] = min(tren_mini[i - 1][j], tren_mini[i][j - 1]) + sad[i][j];}}
        cerr << "REZ:  ";
        cout << tren_mini[N][M] << endl;
    }
    return 0;
}