#include <bits/stdc++.h>
#include <chrono>
using namespace std;

/* TYPES  */
#define ll long long

/* FUNCTIONS */
#define f(i,s,e) for(long long int i=s;i<e;i++)
#define cf(i,s,e) for(long long int i=s;i<=e;i++)
#define rf(i,e,s) for(long long int i=e-1;i>=s;i--)
#define pb push_back
#define eb emplace_back

/* PRINTS */
template <class T>
void print_v(vector<T> &v) { cout << "{"; for (auto x : v) cout << x << ","; cout << "b}"; }
template <class Type>
void print_arr(Type arr, int len)
{ for(int i=0;i<len;i++) cout << arr[i] << " ";
  cout << endl; }

/* UTILS */
#define MOD 1000000007
#define PI 3.1415926535897932384626433832795
#define read(type) readInt<type>()
ll min(ll a,int b) { if (a<b) return a; return b; }
ll min(int a,ll b) { if (a<b) return a; return b; }
ll max(ll a,int b) { if (a>b) return a; return b; }
ll max(int a,ll b) { if (a>b) return a; return b; }
ll gcd(ll a,ll b) { if (b==0) return a; return gcd(b, a%b); }
ll lcm(ll a,ll b) { return a/gcd(a,b)*b; }
string to_upper(string a) { for (int i=0;i<(int)a.size();++i) if (a[i]>='a' && a[i]<='z') a[i]-='a'-'A'; return a; }
string to_lower(string a) { for (int i=0;i<(int)a.size();++i) if (a[i]>='A' && a[i]<='Z') a[i]+='a'-'A'; return a; }
bool prime(ll a) { if (a==1) return 0; for (int i=2;i<=round(sqrt(a));++i) if (a%i==0) return 0; return 1; }
void yes() { cout<<"YESn"; }
void no() { cout<<"NOn"; }

/*  All Required define Pre-Processors and typedef Constants */
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int  uint64;

const int MAXN=1007;
const int MAXM=1007;

int n,m,q,x,y;
bool seen[MAXN][MAXM];
vector<vector<ll>> a(MAXN);
ll sum_navijaca;

bool valid(int i, int j) {
  if(i<n && i>=0 && j<m && j>=0) {
    if(i==x && j==y) return false;
    else return true;
  }
  return false;
}

void backtrack(int i, int j, ll sum) {
  // printf("PROVERA i, j: %d, %d\n", i, j);
  
  if(sum_navijaca!=-1) return;
  if(!valid(i, j)) return;
  if(seen[i][j]) return;
  seen[i][j]=true;

  sum+=(long long)a[i][j];

  // ako dodjemo do stadiona
  // if(i == n-1 && j == m-1) { cerr << "STADION\n";sums.push_back(sum); }
  if(i == n-1 && j == m-1) { sum_navijaca=sum; return; }

  // printf("I, J, SUM: %d, %d, %d\n", i, j, sum);

  if(valid(i, j+1) && valid(i+1, j)) {
    if(a[i][j+1] < a[i+1][j]) {
      backtrack(i, j+1, sum);
      if(sum_navijaca==-1)
        backtrack(i+1, j, sum);
    }else{ 
      backtrack(i+1, j, sum);
      if(sum_navijaca==-1)
        backtrack(i, j+1, sum);
    }
  }else{
    if(valid(i,j+1)) { backtrack(i, j+1, sum); }
    else if(valid(i+1,j)) { backtrack(i+1,j, sum); }
    else return;
  }
}

/* TODO: 
 *  1. dodaj da uzmemo rezultat, minimalnu vrednost iz sums{}
 *  2. proveri za velike primere, (vreme <= 3s)
 *  3. probaj da nadjes neke teske primere
 *  4. pogledah cheapest path algo
 *  5. *** MISLIM da mi proverava i nakon sto nadje prvi sums[0],
 *  to jest bespotrebno proverava jos mnogo puta iako sam vec nasao min path
*/

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> m;

  for(int i=0;i<n;i++) for(int j=0;j<m;j++) { 
    int x;
    cin >> x;
    a[i].push_back(x);
  }

  cin >> q;

  // auto start=chrono::high_resolution_clock::now();
  while(q--) {
    memset(seen, 0, sizeof(seen));
    sum_navijaca=-1;
    cin >> x >> y;
    x--;y--;

    backtrack(0, 0, 0);
    cout << sum_navijaca << "\n";
  }
  // auto end=chrono::high_resolution_clock::now();
// 
  // using namespace std::chrono;
// 
  // cout << "Execute time: " << duration_cast<microseconds>(end-start).count() << "\n";

  return 0;
}