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

using namespace std;

unsigned long long int mem[10 ^ 6][10 ^ 6] = {0};
unsigned long int n, m;
unsigned long int doha[10 ^ 6][10 ^ 6] = {0};

unsigned long long int alg(unsigned long int indx, unsigned long int indy)
{
    unsigned long long int desno, levo;
    bool l = 0, d = 0;
    
    if(mem[indx + 1][indy] > 0)
    {
        levo = mem[indx + 1][indy];
    }
    else
    {
        if(indx < n - 1)
        {
            levo = alg(indx + 1, indy);
            mem[indx + 1][indy] = levo;
        }
        else
        {
            l = 1;
        }
    }
    
    if(mem[indx][indy + 1] > 0)
    {
        desno = mem[indx][indy + 1];
    }
    else
    {
        if(indy < m - 1)
        {
            desno = alg(indx, indy + 1);
            mem[indx][indy + 1] = desno;
        }
        else
        {
            d = 1;
        }
    }
    
    
    if(d)
    {
        mem[indx][indy] = levo;
    }
    else if(l)
    {
        mem[indx][indy] = desno;
    }
    if(!d && !l)
    {
        mem[indx][indy] = (levo < desno) ? levo : desno;
    }
    return mem[indx][indy] + doha[indx][indy];
}

int main()
{
	cin >> 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 int x, y;
	
	vector<unsigned long long int> res;
	
	unsigned long long int *pok;
	pok = &mem[0][0];
	
	for(int i = 0; i < q; i++)
	{
	    mem[n - 1][m - 1] = doha[n - 1][m - 1];
	    cin >> x >> y;
	    x--;
	    y--;
	    mem[x][y] = ULLONG_MAX / 2;
	    
	    res.push_back(alg(0, 0));
	    memset(pok, 0, sizeof(mem));
	}
	
	for(auto e : res)
	{
	    cout << e << "\n";
	}
	
    return 0;
}