#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll n,m;
    cin >> n >> m;
    ll dp[n+1][m+1],a[n+1][m+1],p[n+1][m+1];
    for(ll i=1;i<=n;i++)
    {
        for(ll j=1;j<=m;j++)
        {
            cin >> a[i][j];
        }
    }
    ll q;
    cin >> q;
    while(q--)
    {
        ll x,y;
        cin >> x >> y;
        for(ll i=1;i<=n;i++)
        {
            for(ll j=1;j<=m;j++)
            {
                dp[i][j]=1e15;
            }
        }
        dp[1][1]=a[1][1];
        for(ll i=1;i<=n;i++)
        {
            for(ll j=1;j<=m;j++)
            {
                if(x==i&&y==j) continue;
                if(i+1<=n) dp[i+1][j]=min(dp[i+1][j],dp[i][j]+a[i+1][j]);
                if(j+1<=m) dp[i][j+1]=min(dp[i][j+1],dp[i][j]+a[i][j+1]);
            }
        }
      /*  for(ll i=1;i<=n;i++)
        {
            for(ll j=1;j<=m;j++)
            {
                cout << dp[i][j] << " ";
            }
            cout << endl;
        }*/
        cout << dp[n][m] << endl;
    }
    return 0;
}