#include <bits/stdc++.h>

using namespace std;

int minCena(vector<vector<int>> doha, int n, int m, int x, int y){
    if (m == 0 && n == 0)
        return doha[n][m];
    if (n > 0 && m > 0){
        if((n - 1 != x || m != y) && (n != x || m - 1 != y))
            return doha[n][m] + min(minCena(doha, n - 1, m, x, y), minCena(doha, n, m-1, x, y));
        if (n - 1 != x || m != y)
            return doha[n][m] + minCena(doha, n - 1, m, x, y);
        return doha[n][m] + minCena(doha, n, m - 1, x, y);
    }
    if (n > 0 && (n - 1 != x || m != y))
        return doha[n][m] + minCena(doha, n - 1, m, x, y);
    if (m > 0 && (n != x || m - 1 != y))
        return doha[n][m] + minCena(doha, n, m - 1, x, y);
    return 2000000000;
}

int main()
{
    int n, m;
    cin >> n >> m;
    vector <vector<int>> doha;
    vector <int> l;
    for (int i = 0; i < n; i++){
        l.clear();
        for (int j = 0; j < m; j++){
            int k;
            cin >> k;
            l.push_back(k);
        }
        doha.push_back(l);
    }
    int q;
    cin >> q;
    int x, y;
    for (int i = 0; i < q; i++){
        cin >> x >> y;
        cout << minCena(doha, n - 1, m - 1, x - 1, y - 1) << endl;
    }
    return 0;
}