#include <bits/stdc++.h>
#define MAXN 300010
#define pb push_back
#define ll long long
#define fi first
#define se second
#define mp make_pair
#define pii pair<int, int>

using namespace std;

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<vector<int>> a(n+10), dp1(n+10), dp2(n+10), prefixMin_row(n+10), prefixMin_column(n+10);
    vector<vector<bool>> path(n+10), onPath(n+10);
    for (int i = 0; i < n; i++)
    {
        a[i].resize(m+10);
        dp1[i].resize(m+10);
        dp2[i].resize(m+10);
        prefixMin_row[i].resize(m+10);
        prefixMin_column[i].resize(m+10);
        path[i].resize(m+10);              //right ~ true, down ~ false
        onPath[i].resize(m+10, false);
        for (int j = 0; j < m; j++)
        {
            cin >> a[i][j];
            if (i == 0 && j == 0)
            {
                dp1[i][j] = a[i][j];
            }
            else if (i == 0)
            {
                dp1[i][j] = dp1[i][j-1] + a[i][j];
                path[i][j] = true;
            }
            else if (j == 0)
            {
                dp1[i][j] = dp1[i-1][j] + a[i][j];
                path[i][j] = false;
            }
            else
            {
                dp1[i][j] = min(dp1[i-1][j], dp1[i][j-1]) + a[i][j];
                path[i][j] = dp1[i-1][j] > dp1[i][j-1];
            }
        }
    }
    int i = n-1, j = m-1;
    while (i > 0 || j > 0)
    {
        onPath[i][j] = true;
        if (path[i][j]) j--;
        else i--;
    }
    for (int i = n-1; i >= 0; i--)
    {
        for (int j = m-1; j >= 0; j--)
        {
            if (i == n-1 && j == m-1)
            {
                dp2[i][j] = dp1[i][j];
                prefixMin_column[i][j] = dp2[i][j];
                prefixMin_row[i][j] = dp2[i][j];
            }
            else if (i == n-1)
            {
                dp2[i][j] = dp1[i][j] + dp2[i][j+1] - dp1[i][j+1] + a[i][j+1];
                prefixMin_column[i][j] = dp2[i][j];
                prefixMin_row[i][j] = min(dp2[i][j], prefixMin_row[i][j+1]);
            }
            else if (j == m-1)
            {
                dp2[i][j] = dp1[i][j] + dp2[i+1][j] - dp1[i+1][j] + a[i+1][j];
                prefixMin_column[i][j] = min(dp2[i][j], prefixMin_column[i+1][j]);
                prefixMin_row[i][j] = dp2[i][j];
            }
            else
            {
                dp2[i][j] = dp1[i][j] + min(dp2[i+1][j] - dp1[i+1][j] +a[i+1][j], dp2[i][j+1] - dp1[i][j+1] + a[i][j+1]);
                prefixMin_column[i][j] = min(dp2[i][j], prefixMin_column[i+1][j]);
                prefixMin_row[i][j] = min(dp2[i][j], prefixMin_row[i][j+1]);
            }
        }
    }
    int q;
    cin >> q;
    while (q--)
    {
        int x, y;
        cin >> x >> y;
        x--; y--;
        if (!onPath[x][y])
        {
            cout << dp1[n-1][m-1] << '\n';
            continue;
        }
        if (i == 0)
            cout << prefixMin_column[x+1][y-1] << '\n';
        else if (j == 0)
            cout << prefixMin_row[x-1][y+1] << '\n';
        else
            cout << min(prefixMin_column[x+1][y-1], prefixMin_row[i-1][j+1]) << '\n';
    }
}

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

	int tt = 1;
	//cin >> tt;
	while(tt--)
	{
		solve();
	}
	return 0;
}