#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long int n, m, q, x, y, pom;
    cin >> n >> m;
    vector<long long int> sol;
    vector<vector<long long int>> mt, dp;
    for (int i = 0; i < n; i++)
    {
        vector<long long int> pom, pom2;
        for (int j = 0; j < m; j++)
        {
            cin >> x;
            pom.push_back(x);
            pom2.push_back(0);
        }
        mt.push_back(pom);
        dp.push_back(pom2);
    }
    cin >> q;
    for (int i = 0; i < q; i++)
    {
        cin >> x >> y;
        x--; y--;
        pom = mt[x][y];
        mt[x][y] = LLONG_MAX;
        dp[0][0] = mt[0][0];
        for (int i = 1; i < n; i++)
        {
            if (mt[i][0] == LLONG_MAX || dp[i - 1][0] == LLONG_MAX)
                dp[i][0] = LLONG_MAX;
            else
                dp[i][0] = mt[i][0] + dp[i - 1][0];
        }
        for (int i = 1; i < m; i++)
        {
            if (mt[0][i] == LLONG_MAX || dp[0][i - 1] == LLONG_MAX)
                dp[0][i] = LLONG_MAX;
            else
                dp[0][i] = mt[0][i] + dp[0][i - 1];
        }
        for (int i = 1; i < n; i++)
        {
            for (int j = 1; j < m; j++)
            {
                if (mt[i][j] == LLONG_MAX)
                    dp[i][j] = LLONG_MAX;
                else
                    dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + mt[i][j];
            }
        }
        sol.push_back(dp[n - 1][m - 1]);
        mt[x][y] = pom;
    }
    for (int i=0;i<sol.size();i++)
    {
        cout << sol[i] << endl;
    }
}