#include <bits/stdc++.h>

using namespace std;

void izracunajZbir(const vector<vector<int>>& matrica, int n, int m, int v, int k, int zbir, int x, int y){
    zbir+=matrica[v][k];
        if (v==n-1 && k==m-1){
                cout << zbir <<"\n";
                        return;
                            }
                                else{
                                        if(v == n-1){
                                                    if(v == x-1 && k+1 == y-1){
                                                                    zbir = zbir - matrica[v][k];
                                                                                    izracunajZbir(matrica,n,m,v-1,k+1,zbir,x,y);
                                                                                                }
                                                                                                            else
                                                                                                                            izracunajZbir(matrica,n,m,v,k+1,zbir,x,y);
                                                                                                                                    }
                                                                                                                                            if(k == m-1){
                                                                                                                                                        if(v+1 == x-1 && k == y-1){
                                                                                                                                                                        zbir = zbir - matrica[v][k];
                                                                                                                                                                                        izracunajZbir(matrica,n,m,v+1,k-1,zbir,x,y);
                                                                                                                                                                                                    }
                                                                                                                                                                                                                else
                                                                                                                                                                                                                                izracunajZbir(matrica,n,m,v+1,k,zbir,x,y);
                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                if(matrica[v+1][k] < matrica[v][k+1]){
                                                                                                                                                                                                                                                            if(v+1 == x-1 && k == y-1){
                                                                                                                                                                                                                                                                            izracunajZbir(matrica,n,m,v,k+1,zbir,x,y);
                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                    else if(v<n-1){
                                                                                                                                                                                                                                                                                                                    izracunajZbir(matrica,n,m,v+1,k,zbir,x,y);
                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                if(matrica[v+1][k] > matrica[v][k+1]){
                                                                                                                                                                                                                                                                                                                                                            if(v == x-1 && k+1 == y-1){
                                                                                                                                                                                                                                                                                                                                                                            izracunajZbir(matrica,n,m,v+1,k,zbir,x,y);
                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                    else if(k<m-1){
                                                                                                                                                                                                                                                                                                                                                                                                                    izracunajZbir(matrica,n,m,v,k+1,zbir,x,y);
                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                            }

                                                                                                                                                                                                                                                                                                                                                                                                                                            int main()
                                                                                                                                                                                                                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                                                                                                                                                                                                                int N, M, Q;
                                                                                                                                                                                                                                                                                                                                                                                                                                                    cin >> N >> M;
                                                                                                                                                                                                                                                                                                                                                                                                                                                        vector<vector<int>> matrica(N);
                                                                                                                                                                                                                                                                                                                                                                                                                                                            for(int i=0;i<N;i++){
                                                                                                                                                                                                                                                                                                                                                                                                                                                                    matrica[i].resize(M);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                            for(int j=0;j<M;j++)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        cin>>matrica[i][j];
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    cin >> Q;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        for(int i = 0; i<Q; i++){
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                int x, y;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        cin >> x >> y;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                izracunajZbir(matrica,N,M,0,0,0,x,y);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                return 0;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }