#include <bits/stdc++.h>
#include<algorithm>
using namespace std;

int main()
{
    int n,m,q,x,y;
    int i,j;
    cin >> n >> m;
    long grid[n][m];
    for(i=0;i<n;i++) 
    {
        for(j=0;j<m;j++) 
        {
            cin >> grid[i][j];
        }
    }
    cin >> q;
    long long sum[n][m];
    sum[0][0] = grid[0][0];

    for(int p=0;p<q;p++)
    {
        cin >> x >> y;
        long t = grid[x-1][y-1];
        grid[x-1][y-1] = 100000000000;
        
        for(i=1;i<m;++i)
        {
            sum[0][i] = sum[0][i-1] + grid[0][i];
        }
        for(i=1;i<n;++i)
        {
            sum[i][0] = sum[i-1][0] + grid[i][0];
        }
        
        for(i=1;i<n;++i)
        {
            for(j=1;j<m;++j)
            {
                
                sum[i][j] = grid[i][j] + min(sum[i-1][j],sum[i][j-1]);
            }
        }
        
        cout << sum[n-1][m-1]<< endl;
        grid[x-1][y-1] = t;
    }
    
    return 0;
}