#include <iostream>

using namespace std;

void resi(int **a, int n, int m, int i, int j, unsigned long long int r, unsigned long long int *s, int ni, int nj){
    r+=a[i][j];
    if (j+1<m && (i!=ni || j+1!=nj)){
        resi(a,n,m,i,j+1,r,s,ni,nj);
    }
    if (i+1<n && (i+1!=ni || j!=nj)){
        resi(a,n,m,i+1,j,r,s,ni,nj);
    }
    if (i==n-1 && j==m-1){
        if (*s>=r) *s=r;
    }
}

int main()
{
    int q,n,m;
    scanf("%d%d",&n,&m);
    int **a;
    a=new int*[n];
    for(int i=0;i<n;i++){
        a[i]=new int[m];
        for(int j=0;j<m;j++){
            cin>>a[i][j];
        }
    }
    scanf("%d",&q);
    for(int k=0;k<q;k++){
        int x,y;
        scanf("%d%d",&x,&y);
        x--;
        y--;
        unsigned long long s=18446744073709551615;
        resi(a,n,m,0,0,0,&s,x,y);
        cout<<s<<endl;
    }
}