#include <bits/stdc++.h>

using namespace std;

#define INF 1000000000

int main()
{
    long long n,m;
    cin>>n>>m;

    long long matrica[n+10][m+10]={0};

    for (long long i=1;i<=n;i++)
    {
        for (long long j=1;j<=m;j++)
        {
            cin>>matrica[i][j];
            //cout<<matrica[i][j]<<endl;
        }
    }

    /*for (long long i=1;i<=n;i++)
    {
        for (long long j=1;j<=m;j++) cout<<matrica[i][j]<<" ";

        cout<<endl;
    }*/

    long long q;
    cin>>q;

    for (long long z=0;z<q;z++)
    {
        long long x,y;
        cin>>x>>y;

        long long vred=matrica[x][y];
        matrica[x][y]=INF;

        long long dp[n+10][m+10];

        dp[1][1]=matrica[1][1];

        for (long long j=2;j<=n;j++) dp[j][1]=dp[j-1][1]+matrica[j][1];
        for (long long j=2;j<=m;j++) dp[1][j]=dp[1][j-1]+matrica[1][j];

        for (long long i=2;i<=n;i++)
        {
            for (long long j=2;j<=m;j++) dp[i][j]=matrica[i][j]+min(dp[i-1][j],dp[i][j-1]);
        }

        cout<<dp[n][m]<<endl;

        matrica[x][y]=vred;
    }
}