#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define mp make_pair
#define mt make_tuple
#define sz size()
#define pb push_back
#define SPED ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define all(x) (x).begin(),(x).end()
#define allr(x) (x).rbegin(),(x).rend()
#define inv(n) power((n), mod - 2)
#define ff(i,a,b) for (long long (i) = (a); (i) < (b); (i)++)
#define fff(i,a,b) for (long long (i) = (a); (i) <= b; (i)++)
#define bff(i,a,b) for (long long (i) = (b)-1; (i) >= (a); (i)--)
#define bfff(i,a,b) for (long long (i) = (b); (i) >= (a); (i)--)
#define sum_overflow(a,b) __builtin_add_overflow_p ((a), (b), (__typeof__ ((a) + (b))) 0)
#define mul_overflow(a,b) __builtin_mul_overflow_p ((a), (b), (__typeof__ ((a) * (b))) 0)
#define popcount(x) __builtin_popcountll(x)
#define PI 3.14159265358979323846

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef pair<ll,ll> pll;
typedef vector<vector<ll>> graf;
typedef array<ll,3> all;
typedef array<double,3> add;

const ll mod = 1e9+7;
const ll MOD = 998244353;
const ll MAXN = 100000;
const ll INF = 1e18;
const ld EPS = 1e-9;

template <class T> using ordered_set = tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update>;
template <class T> using ordered_multiset = tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;

void stampaj(graf& dp,ll n,ll m)
{
    fff(i,1,n)
    {
        fff(j,1,m)
        {
            cout << dp[i][j] << " ";
        }
        cout << "\n";
    }
    cout << "\n";
}
int main()
{
    SPED;
    ll n,m;
    cin >> n >> m;
    vector<vector<ll>> mat(n+1,vector<ll>(m+1));
    fff(i,1,n)
    {
        fff(j,1,m)
        cin >> mat[i][j];
    }
    vector<vector<ll>> dpdown(n+1,vector<ll>(m+1));
    vector<vector<ll>> dpup(n+1,vector<ll>(m+1));
    vector<vector<bool>> vis(n+1,vector<bool>(m+1));
    dpdown[1][1]=mat[1][1];
    fff(i,2,n)
    {
        dpdown[i][1]=dpdown[i-1][1]+mat[i][1];
    }
    fff(j,2,m)
    {
        dpdown[1][j]=dpdown[1][j-1]+mat[1][j];
    }
    fff(i,2,n)
    {
        fff(j,2,m)
        {
            dpdown[i][j]=min(dpdown[i-1][j],dpdown[i][j-1])+mat[i][j];
        }
    }
    dpup[n][m]=mat[n][m];
    bfff(i,1,n-1)
    {
        dpup[i][m]=dpup[i+1][m]+mat[i][m];
    }
    bfff(j,1,m-1)
    {
        dpup[n][j]=dpup[n][j+1]+mat[n][j];
    }
    bfff(i,1,n-1)
    {
        bfff(j,1,m-1)
        {
            dpup[i][j]=min(dpup[i+1][j],dpup[i][j+1])+mat[i][j];
        }
    }
    //stampaj(dpdown,n,m);
    //stampaj(dpup,n,m);
    ll n1=n;
    ll m1=m;
    vis[n][m]=true;
    while(true)
    {
        if(n1>1 && m1>1)
        {
            if(dpdown[n1-1][m1]<dpdown[n1][m1-1])
            {
                vis[n1-1][m1]=true;
                n1--;
            }
            else
            {
                vis[n1][m1-1]=true;
                m1--;
            }
        }
        else if(n1>1)
        {
            vis[n1-1][m1]=true;
            n1--;
        }
        else if(m1>1)
        {
            vis[n1][m1-1]=true;
            m1--;
        }
        else break;
    }
    ll q;
    cin >> q;
    while(q--)
    {
        ll i,j;
        cin >> i >> j;
        ll ans = INF;
        if(!vis[i][j])
            ans=dpdown[n][m];
        else
        {
            if(i>1 && j>1)
            {
                ans = min(dpdown[i][j-1]+dpup[i+1][j-1],dpdown[i-1][j]+dpup[i-1][j+1]);
            }
            else if(i==1 || j==m)
            {
                ans = dpdown[i][j-1]+dpup[i+1][j-1];
            }
            else if(i==n || j==1)
            {
                ans = dpdown[i-1][j]+dpup[i-1][j+1];
            }
        }
        cout << ans << "\n";
    }
    return 0;
}