#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> matrica;
vector<vector<int>> nova;
vector<int> pomocni;
int main(){
    int n,m,q,x,y,stari;
    int element;
    cin >> n >> m;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cin >> element;
            pomocni.push_back(element);
        }
        matrica.push_back(pomocni);
        pomocni.clear();
    }
    cin >> q;
    pomocni.clear();
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            pomocni.push_back(0);
        }
        nova.push_back(pomocni);
        pomocni.clear();
    }
    for(int l=0;l<q;l++){
        cin >> x >> y;
        x=x-1;
        y=y-1;
        stari=matrica[x][y];
        matrica[x][y]=1000000001;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(i==0 && j==0){
                    nova[i][j]=matrica[i][j];
                    continue;
                }
                if(i==0){
                    nova[i][j]=matrica[i][j]+nova[i][j-1];
                    continue;
                }
                if(j==0){
                    nova[i][j]=matrica[i][j]+nova[i-1][j];
                    continue;
                }
                nova[i][j]=min(nova[i-1][j],nova[i][j-1])+matrica[i][j];
            }
        }
        cout << nova[n-1][m-1] << endl;
        matrica[x][y]=stari;
    }
}