#include <bits/stdc++.h>
#include <climits>

using namespace std;

int main()
{
    unsigned long int n, m;
	cin >> n >> m;
	unsigned long int doha[n][m];
	
	for(unsigned long int i = 0; i < n; i++)
	{
	    for(unsigned long int j = 0; j < m; j++)
	    {
	        cin >> doha[i][j];
	    }
	}
	
	unsigned long int q;
	cin >> q;
	unsigned long x, y;
	vector<unsigned long int> res;
	
	for(unsigned long int i = 0; i < q; i++)
	{
	    cin >> x >> y;
	    x--;
	    y--;
	    
	    unsigned long int dajkstra[n][m] = {0};
	
    	dajkstra[0][0] = doha[0][0];
    	
    	for(unsigned long int i = 1; i < m; i++)
    	{
    	    if(x == 0 && y == i)
    	    {
    	        for(unsigned long int j = i; j < m; j++)
    	        {
    	            dajkstra[0][j] = ULONG_MAX;
    	            dajkstra[1][j] = ULONG_MAX;
    	        }
    	        break;
    	    }
    	    
    	    dajkstra[0][i] = dajkstra[0][i - 1] + doha[0][i];
    	    dajkstra[1][i] = dajkstra[0][i] + doha[1][i];
    	}
    	
    	for(unsigned long int i = 1; i < n; i++)
    	{
    	    dajkstra[i][0] = dajkstra[i - 1][0] + doha[i][0];
    	    for(unsigned long int j = 1; j < m; j++)
    	    {
    	        if(x == i && y == j)
    	        {
    	            
    	            if(i < n - 1)
    	            {
    	                for(unsigned long int k = i; k < m; k++)
    	                {
    	                    dajkstra[i][k] = ULONG_MAX;
    	                    dajkstra[i + 1][j] = ULONG_MAX;
    	                }
    	            }
    	            else
    	            {
    	                for(unsigned long int k = i; k < m; k++)
    	                {
    	                    dajkstra[i][k] = ULONG_MAX;
    	                }
    	            }
    	            
    	            break;
    	        }
    	        
    	        if((dajkstra[i][j - 1] + doha[i][j]) < dajkstra[i][j])
    	        {
    	            dajkstra[i][j] = dajkstra[i][j - 1] + doha[i][j];
    	        }
    	        
    	        if(i < n - 1)
    	        {
    	            dajkstra[i + 1][j] = dajkstra[i][j] + doha[i + 1][j];
    	        }
    	    }
    	}
    	
    	res.push_back(dajkstra[n - 1][m - 1]);
	}
	
	for(auto e : res)
	{
	    cout << e << "\n";
	}
	
	return 0;
}