#include <bits/stdc++.h>

using namespace std;

#define inf (LLONG_MAX / 2)
#define ll long long

void popuni(int i, int j, set<pair<ll, ll> >& putt, vector<pair<ll, ll> >& put, vector<vector<ll> >& dp){
    if(!(i == 1 && j == 1)){
        int r, s;
        if(dp[i - 1][j] < dp[i][j - 1]){
            r = i - 1;
            s = j;
        }
        else{
            r = i;
            s = j - 1;
        }
        putt.insert({r, s});
        put.push_back({r, s});
        popuni(r, s, putt, put, dp);
    }
}

int main()
{
    int n, m;
    cin >> n >> m;
    vector<vector<ll>> dp(n + 1, vector<ll>(m + 1, inf));
    vector<vector<ll>> rez(n + 1, vector<ll>(m + 1));
    vector<vector<ll>> a(n + 1, vector<ll>(m + 1));
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            cin >> a[i][j];
    dp[1][1] = a[1][1];
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++){
            if(!(i == 1 && j == 1))
            dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + a[i][j];
        }
    vector<pair<ll, ll> > put;
    set<pair<ll, ll> > putt;
    put.push_back({n, m}); putt.insert({n, m});
    popuni(n, m, putt, put, dp);
    int q;
    int x, y;
    cin >> q;
    while(q){
        cin >> x >> y;
        if(!set.find({x, y}))
            cout << dp[n][m];
        else{

        }
        q--;
    }
    return 0;
}