#include <iostream>
#include <vector>
using namespace std;


long long int minPathSum(vector<vector<long long int> >& grid) {
        long long int m = grid.size()-1;
        long long int n = grid[0].size()-1;

        vector<long long int> cur(m, grid[0][0]);

        for (long long int i = 1; i < m; i++)
            cur[i] = cur[i - 1] + grid[i][0];

        for (long long int j = 1; j < n; j++) {
            cur[0] += grid[0][j];
            for (long long int i = 1; i < m; i++)
                cur[i] = min(cur[i - 1], cur[i]) + grid[i][j];
        }

        return cur[m-2];

    }

int main()
{
    long long int n,m;
    cin >> n >> m;
    int num;
    vector<vector<long long int> > doha(m+1);
    for(unsigned long long int i = 0; i < n; i++)
    {
        for(unsigned long long int j = 0; j < m; j++)
        {

            cin >> num;

            doha[i].push_back(num);

        }
    }

    for(int i=0; i < n+1; i++){
        doha[i].push_back(0);
    }
    for(int i=0; i < m+1; i++){
        doha[n].push_back(0);
    }

    long long int q;
    cin >> q;
    vector<pair<long long int,long long int> > coordinates;
    long long int x,y;



    for(unsigned long long int i =0 ; i < q; i++)
    {
        cin >> x >> y;
        pair<long long int,long long int> newpair;
        newpair.first = x-1;
        newpair.second = y-1;
        coordinates.push_back(newpair);
    }
    vector<long long int> ress;

   /*  for(unsigned long long int i = 0; i <= n; i++)
    {
        for(unsigned long long int j = 0; j <= m; j++)
        {

            cout << doha[i][j] << " ";
           // auxi[i][j] = doha[i][j];
        }
        cout << '\n';
    }
*/
    for(unsigned long long int i = 0; i < q; i++)
    {
        long long int x = coordinates[i].first;
        long long int y = coordinates[i].second;
        long long int temp = doha[x][y];
        doha[x][y] = 1e10;

        long long int res = minPathSum(doha);

        ress.push_back(res);
        doha[x][y] = temp;

    }
    for(auto a : ress)
        cout << a << "\n";
    return 0;
}