#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));
    vector <vector<ll> > mini(N+2, vector <ll> (M+2, 0));
    ///set <pair<ll, ll> > putevi;
    ///vector <pair<ll, ll> > p;
    for(int i = 1; i <= N; i++) {for(int j = 1; j <= M; j++) cin >> A[i][j];}
    mini[1][1] = A[1][1];
    for(int i = 2; i <= N; i++) mini[i][1] = mini[i - 1][1] + A[i][1];
    for(int j = 2; j <= M; j++) mini[1][j] = mini[1][j - 1] + A[1][j];
    for(int i = 2; i <= N; i++)
    {
        for(int j = 2; j <= M; j++)
        {
            ll manji = min(mini[i - 1][j], mini[i][j - 1]);
            mini[i][j] = manji + A[i][j];
        }
    }
    ///for(int i = 1; i <= N; i++) {{for(int j = 1; j <= M; j++) cout << mini[i][j] << " ";} cout << endl;}
    ///cout << "MINI:  " << mini[N][M] << endl;
    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];}}
        cout << tren_mini[N][M] << endl;
    }
    return 0;
}
/**
7 10
9 7 3 1 2 4 8 2 6 1
2 2 20 1 4 3 2 7 6 8
5 8 6 6 6 3 3 2 1 18
12 13 9 2 23 4 8 13 12 4
4 2 7 1 12 9 3 2 8 9
6 14 5 0 80 1 6 9 4 71
9 5 6 4 3 50 2 4 13 4
1
3 4
**/