#include<bits/stdc++.h>

using namespace std;


int min_path_cost(vector<vector<int> > doha,int n,int m){
	//int doha_cost[n][m];
	vector<vector<int> > doha_cost = doha;
	int i,j;
	
	doha_cost[0][0] = doha[0][0];
	for(i = 1; i < m; i++){
		if(doha[0][i] == INT_MAX || doha_cost[0][i-1] == INT_MAX){
			doha_cost[0][i] = INT_MAX;
		}else{
		doha_cost[0][i] = doha_cost[0][i-1] + doha[0][i];
		}
	}
	
	for(i = 1; i < n; i++){
		if(doha[i][0] == INT_MAX|| doha_cost[i-1][0] == INT_MAX){
			doha_cost[i][0] = INT_MAX;
		}else{
		doha_cost[i][0] = doha_cost[i-1][0] + doha[i][0];
		}
		
	}
	
	for(i = 1; i < n;i++){
		for(j = 1; j < m; j++){
			if(doha[i][j] == INT_MAX){
				doha_cost[i][j] = INT_MAX;
			}else{
				doha_cost[i][j] = doha[i][j] + min(doha_cost[i][j-1], doha_cost[i-1][j]);
			}
		}
	}
	
	/*for(i = 0; i < n;i++){
		for(j = 0; j < m; j++){
			cout<< doha_cost[i][j]<<" ";
		}
		cout<<endl;
	}
	*/
	
	return doha_cost[n-1][m-1];

}

void min_path_cost_with_q(int n,int m, vector<vector<int> > doha,int qi,int qj){
	
	doha[qi-1][qj-1] = INT_MAX;
	
	
	
	cout<<min_path_cost(doha,n,m);
}




int main()
{
	int n,m,i,j,q,x;
	cin>>n>>m;
	
	vector<vector<int> > doha;
	
	for (i = 0; i < n; i++) 
    {
        vector<int> v1;
   
        for (j = 0; j < m; j++) {
            cin>>x;
			v1.push_back(x);
			
        }
        doha.push_back(v1);
    }
	
	cin>>q;
	
	int qi[q], qj[q];

	//vector<int> qi(q),qj(q);
	
	for(i = 0; i < q;i++){
		cin>>qi[i]>>qj[i];
	}
	
	
	
	for(i = 0; i < q; i++){
		min_path_cost_with_q(n,m, doha, qi[i],qj[i]);	
		cout<<"\n";
	}
	
	
	
	
	return 0;
}