#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int findNinCost(vector<vector<int>> const &cost)
{
    if (cost.size() == 0) 
        return 0;
    
    int N = cost.size();
    int M = cost[0].size();

    int _[N][M];

    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
            _[i][j] = cost[i][j];

            if (i == 0 && j > 0) 
                _[0][j] += _[0][j - 1];

            else if (j == 0 && i > 0) 
                _[i][0] += _[i - 1][0];
            

            else if (i > 0 && j > 0) {
                _[i][j] += min(_[i - 1][j], _[i][j - 1]);
            }
        }
    }

    return _[N - 1][M - 1];
}
 
int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n,m;
    cin >> n >> m;
    vector<vector<int>> v( n , vector<int> (m, 0)); 
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
            cin >> v[i][j];
    int q;
    cin >> q;
    for(int i = 0; i<q;i++)
    {
        int a,b;
        cin >> a >> b;
        a--; b--;
        int old_vr = v[a][b];
        v[a][b] = 1000001;
        cout << findNinCost(v) << "\n";
        v[a][b] =old_vr;
    }
    return 0;
}