#include<bits/stdc++.h>

using namespace std;

int niz[100][100];

int dfs(int i, int j, int n, int m, int navijaci){
    if(i == n && j == m){
        return navijaci; 
    }
    int sl_i = i + 1;
    int sl_j = j + 1;
    if(sl_i <= n && niz[sl_i][j] != -1 && sl_j <= m && niz[i][sl_j]){
        return min(dfs(sl_i, j, n, m, niz[sl_i][j]), dfs(i, sl_j, n, m, niz[i][sl_j]));
    }
    else if(sl_i <= n && niz[sl_i][j] != -1){
        return dfs(sl_i, j, n, m, niz[sl_i][j]);
    }
    else{
        return dfs(i, sl_j, n, m, niz[i][sl_j]);
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            cin >> niz[i][j];
        }
    }
    int q;
    cin >> q;
    for(int i = 0; i < q; i++){
        int i_upit, j_upit;
        cin >> i_upit >> j_upit;
        i_upit -= 1;
        j_upit -= 1;
        int temp = niz[i_upit][j_upit];
        niz[i_upit][j_upit] = -1;
        if(j_upit == m - 1){
            cout << dfs(0, 0, n - 1, m - 2, niz[0][0]) + niz[n - 1][m - 1];
        }
        else if(i_upit == n - 1){
            cout << dfs(0, 0, n - 2, m - 1, niz[0][0]) + niz[n - 1][m - 1];
        }
        else cout << dfs(0, 0, n - 1, m - 1, niz[0][0]);
        niz[i_upit][j_upit] = temp;
    }
}