#include <iostream>
#include <bits/stdc++.h> 
/*
3 4
1 2 3 4
5 6 7 8
9 10 11 12
2
1 2
2 4*/
using namespace std;

long long int A[10000][10000];

void ertekadas(vector<pair<long long int,long long int>> vege,long long int n,long long int m, long long int x, long long int y,priority_queue<pair<long long int,vector<pair<long long int,long long int>>>>& Utvonalak,long long int jelenlegi)
{
    if(x==n-1&&y==m-1)
    {
        pair<long long int,vector<pair<long long int,long long int>>> B;
        jelenlegi+=A[x][y];
        B.first=-jelenlegi;
        B.second=vege;
        Utvonalak.push(B);
        return;
    }
    pair<long long int,long long int> B;
    B.first=x;
    B.second=y;
    vege.push_back(B);
    jelenlegi+=A[x][y];
if(x<n-1)
{
    x++;
    ertekadas(vege,n,m,x,y,Utvonalak,jelenlegi);
    x--;
}
if(y<m-1)
{
    y++;
    ertekadas(vege,n,m,x,y,Utvonalak,jelenlegi);
}
}

int main()
{
long long int a,b,q;
vector<long long int> megoldas;
vector<pair<long long int,long long int>> vege;
cin>>a>>b;
priority_queue<pair<long long int,vector<pair<long long int,long long int>>>> Utvonalak;
priority_queue<pair<long long int,vector<pair<long long int,long long int>>>> MentesUtvonalak;
pair<long long int,vector<pair<long long int,long long int>>> B;
for(long long int i=0;i<a;i++)
{
    for(long long int j=0;j<b;j++)
    {
        long long int c;
        cin>>c;
        A[i][j]=c;
    }
}
ertekadas(vege,a,b,0,0,Utvonalak,0);
MentesUtvonalak=Utvonalak;
cin>>q;
for(long long int i=0;i<q;i++)
{
    long long int c,d;
    bool mehet=true;
    cin>>c>>d;
    c--;
    d--;
    Utvonalak=MentesUtvonalak;
    while(mehet)
    {
        B=Utvonalak.top();
        if(B.second[c+d].first==c&&B.second[c+d].second==d)
        {
                Utvonalak.pop();
        }
        else
        {
            megoldas.push_back(-B.first);
            mehet=false;
        }
        /*        bool vanebenne=false;
        for(long long int k=0;k<B.second.size();k++)
        {
            if(B.second[k].first==c&&B.second[k].second==d)
            {
                Utvonalak.pop();
                vanebenne=true;
                break;
            }
        }
        if(!vanebenne)
        {
            megoldas.push_back(-B.first);
            mehet=false;
        }*/
    }
}
for(long long int i=0;i<q;i++)
{
    cout<<megoldas[i]<<endl;
}
}