#include <bits/stdc++.h>
#define ull unsigned long long

int nmax = 1e16+1;

using namespace std;

ull minPathSum(int** A, int n11, int n12, int i1, int j1) {
    ull **dp = (ull**)malloc(n11*sizeof(ull*));
    for (int i = 0; i < n11; ++i)
        dp[i] = (ull*)malloc(n12*sizeof(ull));
    int i,j;for(i = 0;i<n11;i++)
    for(j = 0;j<n12;j++)
    {
dp[i][j] = A[i][j];
if(i==0 && j==0)continue;
ull m = nmax ,n = nmax;
if(i>0)
m = dp[i-1][j];
if(j>0)
n =  dp[i][j-1];
if (i == i1 && j == j1) {
    dp[i][j] = nmax;
    continue;
}

if(m>n)
dp[i][j] = dp[i][j]+n;
else
        dp[i][j] = dp[i][j]+ m;

    }

    /*for (int i = 0; i < n11; ++i) {
        for (int j = 0; j < n12; ++j)
            printf("%llu ", dp[i][j]);
        printf("\n");
    }*/
    return dp[n11-1][n12-1];

}

int main() {

    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cerr.tie(0);
    int n,m;
    scanf("%d%d",&n,&m);
    int **A = (int**)malloc(n*sizeof(int*));
    for (int i = 0; i < n; ++i)
        A[i] = (int*)malloc(m*sizeof(int));
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
            scanf("%d",&A[i][j]);
    /*    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
            printf("%d ",A[i][j]);*/
    int q;
    scanf("%d",&q);
    while(q--) {
    int i,j;
    scanf("%d%d",&i,&j);
    i--; j--;
    printf("%d\n",minPathSum(A,n,m,i,j));
    }



}