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

int r,k;

vector<vector<unsigned long long>> matrica;
vector<vector<unsigned long long>> rast;
vector<unsigned long long> konacno;


int main()
{
    int r,k;
    cin>>r>>k;

    for(int i=0;i<r;i++)
    {
        vector<unsigned long long> v1;
        for(int j=0;j<k;j++)
        {
            int a;
            cin>>a;
            v1.push_back(a);
        }
        matrica.push_back(v1);
    }
    /*cout<<rast[2][3]<<"\n";
    cout<<rast[0][0]<<"\n";*/
    for (int i=0; i<r; i++)
    {
        vector<unsigned long long> v2;
        for (int j=0; j<k; j++)
            v2.push_back(0);
        rast.push_back(v2);
    }
    /*for (int i=0; i<r;i++)
    {
        for (int j=0; j<k;j++)
            cout<<rast[i][j]<<" ";
        cout<<"\n";
    }*/

    int q;
    cin>>q;
    int m=q;

    while(q)
    {
        int x,y;
        cin>>x>>y;
        unsigned long long a=matrica[x-1][y-1];
        matrica[x-1][y-1]=1e15;

        rast[0][0]=matrica[0][0];

        for(int i=1;i<=r-1;i++)
            rast[i][0]=rast[i-1][0]+matrica[i][0];
        for(int j=1;j<=k-1;j++)
            rast[0][j]=rast[0][j-1]+matrica[0][j];

        for(int i=1;i<=r-1;i++)
            for(int j=1;j<=k-1;j++)
                rast[i][j]=min(rast[i-1][j],rast[i][j-1])+matrica[i][j];

        /*cout<<matrica[x-1][y-1]<<"\n"<<rast[x-1][y-1]<<"\n";
        cout<<rast[r-1][k-2]<<"\n";
        cout<<rast[r-2][k-2]<<"\n";

        cout<<rast[r-1][k-1];*/

        konacno.push_back(rast[r-1][k-1]);

        matrica[x-1][y-1]=a;

        q--;
    }
    for(int i=0;i<m;i++)
        cout<<konacno[i]<<"\n";

    return 0;
}