#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n, m;
    cin >> n >> m;
    vector<vector<int>> navijaci(n,vector<int>(m));
    vector<vector<long long>> koraci(n, vector<long long>(m, 1000000000));

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> navijaci[i][j];
        }
    }

    koraci[0][0] = navijaci[0][0];
    int q;
    cin>>q;
    while(q--)
    {
        int x, y;
        cin>>x>>y;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (i == x-1 && j == y-1) continue;
                if (i > 0) koraci[i][j] = min(koraci[i][j], koraci[i-1][j] + navijaci[i][j]);
                if (j > 0) koraci[i][j] = min(koraci[i][j], koraci[i][j-1] + navijaci[i][j]);
            }
        }
        cout << koraci[n-1][m-1] << endl;
    }
    return 0;
}