#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int n,m,q;


int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> m;

    ///Simulacija matrice preko 2 pokazivaca
    long long int** D = (long long int**)malloc(n * sizeof(long long int*));
    long long int** prefiksnaD = (long long int**)malloc(n * sizeof(long long int*));


    for(int i = 0; i < n; i++){
        D[i] = (long long int*)malloc(m * sizeof(long long int));
        prefiksnaD[i] = (long long int*)malloc(m * sizeof(long long int));
    }

    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            long long int pom;
            cin >> pom;
            D[i][j] = pom;
        }
    }

    cin >> q;

    for(int i = 0; i < q; i++){
        long long int xi, yi;
        cin >> xi >> yi;
        long long int pom = D[xi-1][yi-1];
        D[xi - 1][yi - 1] = 1e17;
        //resi(D, prefiksnaD);
        for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            if(i == 0 && j == 0){
                prefiksnaD[i][j] = D[i][j];
            }

            else if(i > 0 && j == 0){
                prefiksnaD[i][j] = prefiksnaD[i - 1][j] + D[i][j];
            }

            else if(i == 0 && j > 0){
                prefiksnaD[i][j] = prefiksnaD[i][j-1] + D[i][j];
            }
            /// ovo je slucaj kada gledamo koje je bolje pomeranje dole ili desno a da nije na ivici ...
            else if(i > 0 && j > 0){
                if(prefiksnaD[i][j-1] >= prefiksnaD[i-1][j]){
                    prefiksnaD[i][j] = prefiksnaD[i-1][j] + D[i][j];
                }
                else{
                    prefiksnaD[i][j] = prefiksnaD[i][j-1] + D[i][j];
                }
            }
        }
    }/*
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                cout << D[i][j] << "     ";
            }
            cout << endl;
        } */

        D[xi - 1][yi - 1] = pom;
        cout << prefiksnaD[n - 1][m - 1] << endl;
    /*
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                cout << prefiksnaD[i][j] << "     ";
            }
            cout << endl;
        } */

    }

    return 0;
}