#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;
 
void pisi(vector<vector<long long>> a)
{
    for(int i = 0; i<a.size(); i++){
        for(int j = 0; j<a[i].size(); j++)
            cout<<a[i][j]<<" ";
        cout<<"\n";
    }
    cout<<"\n";
}
 
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]);
    int q;cin>>q;
    for(int qq = 0; qq<q; qq++)
    {
        int x, y;cin>>x>>y;
        x--;y--;
 
        long long ans = 1e18;
        //cout<<ans<<"\n";
 
        if(x>=1 && y<=m-1)
        {
            int x1 = x-1, y1=y+1;
            while(y1<m)
            {
                ans=min(ans, od[x1][y1]+doo[x1+1][y1]);
                y1++;
            }
        }
        if(y>=1 && x<=n-1)
        {
            int x1 = x+1, y1=y-1;
            while(x1<n)
            {
                ans=min(ans, od[x1][y1]+doo[x1][y1+1]);
                x1++;
            }
        }
 
        cout<<ans<<"\n";
 
 
    }
    return 0;
}