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

int main(){
    int n, m;
    cin >> n >> m;
    long long int doha[n][m];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            cin >> doha[i][j];
    int q; cin >> q;
    vector<long long int>resenje;
    for (int i = 0; i < q; i++) {
        int x, y;
        cin >> x >> y;
        long long int pomocna = doha[x - 1][y - 1];
        doha[x - 1][y - 1] = 10000000000000001;
        
        long long int najmanja_guzva[n][m];
        
        najmanja_guzva[0][0] = doha[0][0];
        for (int i = 1; i < n; i++)
            najmanja_guzva[i][0] = najmanja_guzva[i - 1][0] + doha[i][0];
        for (int j = 1; j < m; j++)
            najmanja_guzva[0][j] = najmanja_guzva[0][j - 1] + doha[0][j];
    
        for (int i = 1; i < n; i++)
            for (int j = 1; j < m; j++)
                najmanja_guzva[i][j] = min(najmanja_guzva[i - 1][j], najmanja_guzva[i][j - 1]) + doha[i][j]; 
    resenje.push_back(najmanja_guzva[n - 1][m - 1]);
    doha[x - 1][y - 1] = pomocna;
    }
    for (int i = 0; i < q; i++)
        cout << resenje[i] << endl;
    
    return 0;
}