#include <bits/stdc++.h>

using namespace std;

vector<vector<long long>> a;

vector<vector<long long>> dpl;
vector<vector<long long>> dpd;

vector<vector<bool>> put;
vector<vector<long long>> pcr;
vector<vector<long long>> pck;

vector<long long> sol;

int main()
{
    int n;
    int m;

    cin >> n;
    cin >> m;

    for(int i = 0; i < n; i++)
    {
        vector<long long> v;

        a.push_back(v);
        dpl.push_back(v);
        dpd.push_back(v);
        pcr.push_back(v);
        pck.push_back(v);

        vector<bool> b;
        put.push_back(b);


        for(int j = 0; j < m; j++)
        {
            long long g;
            cin >> g;

            a[i].push_back(g);
            dpl[i].push_back(-1);
            dpd[i].push_back(-1);
            pcr[i].push_back(-1);
            pck[i].push_back(-1);

            put[i].push_back(false);
        }
    }


    dpl[0][0] = a[0][0];

    for(int i = 1; i < n; i++)
        dpl[i][0] = dpl[i - 1][0] + a[i][0];
    for(int i = 1; i < m; i++)
        dpl[0][i] = dpl[0][i - 1] + a[0][i];
    for(int i = 1; i < n; i++)
        for(int j = 1; j < m; j++)
            dpl[i][j] = min(dpl[i - 1][j], dpl[i][j - 1]) + a[i][j];

    dpd[n - 1][m - 1] = a[n - 1][m - 1];

    for(int i = n - 2; i >= 0; i--)
        dpd[i][m - 1] = dpd[i + 1][m - 1] + a[i][m - 1];
    for(int i = m - 2; i >= 0; i--)
        dpd[n - 1][i] = dpd[n - 1][i + 1] + a[n - 1][i];
    for(int i = n - 2; i >= 0; i--)
        for(int j = m - 2; j >= 0; j--)
            dpd[i][j] = min(dpd[i + 1][j], dpd[i][j + 1]) + a[i][j];

    int indn = 0;
    int indm = 0;

    put[0][0] = 1;
    put[n - 1][m - 1] = 1;

    while(indn < n - 1 || indm < m - 1)
    {
        if(indn >= n - 1)
            indm++;
        else if(indm >= m - 1)
            indn++;
        else if(dpd[indn][indm] - dpd[indn + 1][indm] == a[indn][indm])
            indn++;
        else
            indm++;

        put[indn][indm] = 1;
    }

    for(int i = 0; i < n; i++)
        for(int j = 0; j < m - 1; j++)
            if(j == 0)
                pcr[i][j] = dpl[i][j] + dpd[i][j + 1];
            else
                pcr[i][j] = min(pcr[i][j - 1], dpl[i][j] + dpd[i][j + 1]);

    for(int j = 0; j < m; j++)
        for(int i = 0; i < n - 1; i++)
            if(i == 0)
                pck[i][j] = dpl[i][j] + dpd[i + 1][j];
            else
                pck[i][j] = min(pck[i - 1][j], dpl[i][j] + dpd[i + 1][j]);

    int q;
    cin >> q;

    for(int i = 0; i < q; i++)
    {
        int x;
        int y;

        cin >> x;
        cin >> y;

        x--;
        y--;

        if(!put[x][y])
            sol.push_back(dpl[n - 1][m - 1]);
        else
            if(x == n - 1)
                sol.push_back(min(pck[n - 2][m - 1], pcr[x - 1][y]));
            else if(y == m - 1)
                sol.push_back(min(pck[x][y - 1], pcr[n - 1][m - 2]));
            else if(x == 0)
                sol.push_back(pck[x][y - 1]);
            else if(y == 0)
                sol.push_back(pcr[x - 1][y]);
            else
                sol.push_back(min(pck[x][y - 1], pcr[x - 1][y]));
    }

    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
            cout << pck[i][j] << " ";
        cout << '\n';
    }
    cout << '\n';
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
            cout << pcr[i][j] << " ";
        cout << '\n';
    }
    cout << '\n';

    for(int i = 0; i < q; i++)
        cout << sol[i] << '\n';



    return 0;
}