#include <bits/stdc++.h>
using namespace std;


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int n, m;
    cin >> n >> m;
    vector<vector<int>> a(n + 1, vector<int>(m + 1));

    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> a[i][j];
    // PROBA
    //a[2][4] = 1000000;
    vector<vector<int>> dp(n + 1, vector<int>(m + 1, 1e9 + 1));
    dp[1][1] = a[1][1];
    for (int i = 2; i <= m; i++)
        dp[1][i] = dp[1][i - 1] + a[1][i];

    for (int i = 2; i <= n; i++)
        dp[i][1] = dp[i - 1][1] + a[i][1];

    vector<vector<bool>> put(n + 1, vector<bool>(m + 1, false));
    put[1][1] = true;
    for (int i = 2; i <= n; i++)
        for (int j = 2; j <= m; j++)
        {
            dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + a[i][j];
        }
    // Nalazim taj put, RADI
    int i = n;
    int j = m;
    while (!(i == 1 && j == 1))
    {
        if (dp[i - 1][j] < dp[i][j - 1])
            put[i--][j] = true;
        else
            put[i][j--] = true;
    }
    /*
        // Stampam DP
        cout << endl;
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
                cout << dp[i][j] << " ";
            cout << endl;
        }

        // STAMPAM PUT
        cout << endl;
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
                cout << put[i][j] << " ";
            cout << endl;
        }
    */
    int q;
    cin >> q;
    while (q--)
    {
        int x, y;
        cin >> x >> y;
        if (!put[x][y])
        {
            cout << dp[n][m] << endl;
        }
        else
        {
            vector<vector<int>> dp1(n + 1, vector<int>(m + 1, 1e9 + 1));
            dp1 = dp;
            int temp = a[x][y];
            a[x][y] = 1e9 + 1;
            if(x == 1)
            for (int i = y; i <= m; i++)
                dp1[1][i] = dp1[1][i - 1] + a[1][i];
            if(y == 1)
            for (int i = x; i <= n; i++)
                dp1[i][1] = dp1[i - 1][1] + a[i][1];

            for (int i = x; i <= n; i++)
                for (int j = y; j <= m; j++)
                {
                    dp1[i][j] = min(dp1[i - 1][j], dp1[i][j - 1]) + a[i][j];
                }

            cout << dp1[n][m] << endl;
            a[x][y] = temp;
        }
    }
}