#include <iostream>
#include <iomanip>
#include <set>
#include <limits>
#include <vector>
using namespace std;

typedef pair<int, int> poz;

const long long INF = LLONG_MAX;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;
    vector<vector<long long>> zabranjenaPolja(n+1,vector<long long> (m+1, -1));

    long long a[n+1][m+1];
    long long d[n+1][m+1];
    poz prior[n+1][m+1];
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
            cin >> a[i][j];
    }
    d[1][1] = a[1][1];
    prior[1][1] = make_pair(1, 1);
    for(int j = 1; j <= m; j++)
    {
        d[0][j] = INF;
    }
    for(int i = 1; i <= n; i++)
    {
        d[i][0] = INF;
    }
    /*for(int i = 0; i <= n; i++)
    {
        for(int j = 0; j <= m; j++)
            cout << d[i][j] << " ";
        cout << endl;
    }*/
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            if(i != 1 || j != 1)
                if(d[i-1][j] < d[i][j-1])
                {
                    d[i][j] = d[i-1][j] + a[i][j];
                    prior[i][j] = make_pair(i-1, j);
                }
                else
                {
                    d[i][j] = d[i][j-1]+a[i][j];
                    prior[i][j] = make_pair(i, j-1);
                }
        }
    }
    set<poz> putanja;
    poz trenutno_polje = make_pair(n, m);
    while(trenutno_polje.first != 1 || trenutno_polje.second != 1)
    {
        putanja.insert(trenutno_polje);
        trenutno_polje = prior[trenutno_polje.first][trenutno_polje.second];
    }
    int T;
    cin >> T;
    long long d1[n+1][m+1];
    while(T--)
    {
        poz zPolje;
        cin >> zPolje.first >> zPolje.second;
        if(zabranjenaPolja[zPolje.first][zPolje.second] != -1)
        {
            cout << zabranjenaPolja[zPolje.first][zPolje.second] << endl;
            continue;
        }
        else if(putanja.find(zPolje) == putanja.end())
        {
            cout << d[n][m] << endl;
            continue;
        }

        /*long long *p = &d[0][0];
        long long *q = &d1[0][0];
        memcpy(q, p, (m+1)*(n+1)*sizeof(*p));*/
        d1[zPolje.first][zPolje.second] = INF;

        for(int i = zPolje.first; i <= n; i++)
        {
            for(int j = zPolje.second; j <= m; j++)
            {

                if(i != zPolje.first || j != zPolje.second)
                {
                    if (i == zPolje.first)
                    {
                        d1[i-1][j] = d[i-1][j];
                    }
                    if(j == zPolje.second)
                    {
                        d1[i][j-1] = d[i][j-1];
                    }
                    if(d1[i-1][j] == INF && d1[i][j-1] == INF)
                    {
                        d1[i][j] = INF;
                    }
                    else
                        if(d1[i-1][j] < d1[i][j-1])
                        {
                            d1[i][j] = d1[i-1][j] + a[i][j];
                        }
                        else
                        {
                            d1[i][j] = d1[i][j-1]+a[i][j];
                        }
                }
            }
        }
        cout << d1[n][m] << endl;
        zabranjenaPolja[zPolje.first][zPolje.second] = d1[n][m];
    }

    return 0;
}