#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n, m;
    cin >> n >> m;
    int doha[n][m];
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            cin >> doha[i][j];
        }
    }
    int q;
    cin >> q;
    int granice[2*q];
    int p = 1;
    for(int i = 0; i < 2*q; i++)
    {
    	cin >> granice[i];
	}
    int resenja[q];
    int a, b;
    int br = 1;
    int z1, z2;
    for(int k = 0; k < q; k++){
    	z1=doha[granice[k]+1][granice[k+1]+1];
    	doha[granice[2*k-1]][granice[2*k]]=100000000;
    	int i = 0, j = 0;
     	while(true)
   		{	
    		if(i==n-1||j==m-1)
    		{
    			if(i==n-1)
    			{
    				j++;
    				br += doha[i][j];
				}
				else
				{
					i++;
					br += doha[i][j];
				}
			}
			else if(doha[i][j+1]>doha[i+1][j])
			{
				i++;
				br += doha[i][j];
			}
			else
			{
				j++;
				br += doha[i][j];
			}
			if(i==n-1&&j==m-1)
			{
				break;
			}
		}
		doha[granice[k]][granice[k+1]]=z1;
		resenja[k]=br;
		br=1;
	}	
	for(int i = 0; i < q; i++)
	{
		if(resenja[i]>100000000)
		{
			resenja[i]-=100000000;
		}
		cout << resenja[i] << endl;
	}
}