#include <iostream>
#include <vector>

using namespace std;


int minPathSum(vector<vector<long long>>& grid) {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);

        int rows = grid.size();
        if(rows==0)
            return 0;
        int cols = grid[0].size();
        vector<vector<long long>> dp(rows,vector<long long>(cols,0));
        int i,j;

        dp[0][0] = grid[0][0];  
       
        
        for(i=1;i<cols;++i){
            dp[0][i] = dp[0][i-1] + grid[0][i];
            
        }
        
        for(i=1;i<rows;++i){
            dp[i][0] = dp[i-1][0] + grid[i][0];
        
        }

        for(i=1;i<rows;++i)
        {
            for(j=1;j<cols;++j)
                dp[i][j] = grid[i][j] + min(dp[i-1][j],dp[i][j-1]);
        }
        return dp[rows-1][cols-1];
    }


int main()
{
    int n,m,q,input, red, kolona, a, b;
    cin>>n>>m;


    vector<long long> v(m, 1);
    vector<vector<long long>> grid(n, v);

    for (int i = 0; i<n; i++)
    {
        for (int j = 0; j<m; j++)
        {
            cin>>input;
            grid[i][j] = input;
        }
    }
    cin>>q;


    vector<int> v_of_ones(2, 1);

    vector<vector<int>> Q(q, v_of_ones);

    for (int i = 0; i < q; i++)
    {
        cin>>red>>kolona;
        Q[i][0] = red - 1;
        Q[i][1] = kolona - 1;
    }

    for (int i = 0; i < q; i++)
    {

        a = Q[i][0];
        b = Q[i][1];

        int prethodna_upisana_vrednost = grid[a][b];

        grid[a][b] = 1000000000000; //1e12

        int duzinaPuta = minPathSum(grid);
        cout << duzinaPuta<<endl;


        grid[a][b] = prethodna_upisana_vrednost;
    }
}