#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
#include <cmath>

#define ll long long

using namespace ::std;

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<vector<ll>> mat(n, vector<ll>(m));
    vector<vector<ll>> cost(n, vector<ll>(m));
    for (auto &i : mat)
        for (auto &j : i)
            cin >> j;
    int q;
    cin >> q;
    int x, y;
    for (int iq = 0; iq < q; iq++)
    {
        cin >> x >> y;
        cost[x - 1][y - 1] = LLONG_MAX;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (i == x - 1 && j == y - 1)
                    continue;
                if (j == 0 && i == 0)
                    cost[i][j] = mat[i][j];
                else
                    cost[i][j] = LLONG_MAX;
                if (i > 0 && cost[i - 1][j] != LLONG_MAX)
                    cost[i][j] = min(cost[i][j], cost[i - 1][j] + mat[i][j]);
                if (j > 0 && cost[i][j - 1] != LLONG_MAX)
                    cost[i][j] = min(cost[i][j], cost[i][j - 1] + mat[i][j]);
            }
        }
        cout << cost[n - 1][m - 1] << '\n';
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}