#include <bits/stdc++.h>
#define ull unsigned long long
#define pii pair<int, int>
#define N 1000000
using namespace std;
vector<vector<long long>> doha;
vector<vector<long long>> od;
vector<vector<long long>> doo;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);

    int n, m;
    cin>>n>>m;
    doha.resize(n);
    od.resize(n);
    doo.resize(n);
    for(int i = 0; i<n; i++)
    {
        doha[i].resize(m);
        od[i].resize(m);
        doo[i].resize(m);
        for(int j = 0; j<m; j++)
            cin>>doha[i][j];
    }

    od[0][0]=doha[0][0];
    for(int i = 1; i<n; i++)
        od[i][0]=doha[i][0]+od[i-1][0];
    for(int j = 1; j<m; j++)
        od[0][j]=doha[0][j]+od[0][j-1];

    for(int i = 1; i<n; i++)
        for(int j = 1; j<m; j++)
            od[i][j]=doha[i][j]+min(od[i-1][j], od[i][j-1]);




    doo[n-1][m-1] = doha[n-1][m-1];
    for(int i = n-2; i>=0; i--)
        doo[i][m-1]=doha[i][m-1]+doo[i+1][m-1];
    for(int j = m-2; j>=0; j--)
        doo[n-1][j]=doha[n-1][j]+doo[n-1][j+1];

    for(int i = n-2; i>=0; i--)
        for(int j = m-2; j>=0; j--)
            doo[i][j]=doha[i][j]+min(doo[i+1][j], doo[i][j+1]);

    /*for(int i = 0; i<n; i++){
        for(int j = 0; j<m; j++)
            cout<<doo[i][j]<<" ";
        cout<<"\n";
    }*/
    int q;cin>>q;
    for(int qq = 0; qq<q; qq++)
    {
        int x, y;cin>>x>>y;
        x--;y--;
        if(od[x][y]+doo[x][y]-doha[x][y]!=od[n-1][m-1])
        {
            cout<<od[n-1][m-1]<<"\n";
        }else
        {
            ull ans1=-1, ans2=-2;
            if(x>0 && y<m-1)
            {
                ans1=od[x-1][y]+doo[x-1][y+1];
            }else if(y>0 && x<n-1)
            {
                ans2 = od[x][y-1]+doo[x+1][y-1];
            }

            if(ans1==-1)
                cout<<ans2<<"\n";
            else if(ans2==-1)
                cout<<ans1<<"\n";
            else
                cout<<min(ans1, ans2)<<"\n";
        }
    }








    return 0;
}