/**
 *    author:  wxhtzdy
 *    created: 18.12.2022 09:55:24
**/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);  
  int n, m;
  cin >> n >> m;
  vector<vector<int>> a(n, vector<int>(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }                
  const long long inf = (long long) 1e18;
  vector<vector<long long>> d0(n, vector<long long>(m, inf));
  vector<vector<long long>> d1(n, vector<long long>(m, inf));
  {
    d0[0][0] = a[0][0];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (i > 0) {
          d0[i][j] = min(d0[i][j], d0[i - 1][j] + a[i][j]);
        }
        if (j > 0) {
          d0[i][j] = min(d0[i][j], d0[i][j - 1] + a[i][j]);
        }
      } 
    }
  }
  {
    d1[n - 1][m - 1] = a[n - 1][m - 1];
    for (int i = n - 1; i >= 0; i--) {
      for (int j = m - 1; j >= 0; j--) {
        if (i + 1 < n) {
          d1[i][j] = min(d1[i][j], d1[i + 1][j] + a[i][j]);
        }
        if (j + 1 < m) {
          d1[i][j] = min(d1[i][j], d1[i][j + 1] + a[i][j]);
        }
      }
    }
  }
  vector<vector<long long>> row(n, vector<long long>(m, inf));
  vector<vector<long long>> col(n, vector<long long>(m, inf));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m - 1; j++) {
      if (i > 0) {
        row[i][j] = row[i - 1][j];
      }
      row[i][j] = min(row[i][j], d0[i][j] + d1[i][j + 1]);
    }
  }
  for (int i = 0; i < n - 1; i++) {
    for (int j = 0; j < m; j++) {
      if (j > 0) {
        col[i][j] = col[i][j - 1];
      }
      col[i][j] = min(col[i][j], d0[i][j] + d1[i + 1][j]);
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int x, y;
    cin >> x >> y;
    --x; --y; 
    long long ans = inf;
    if (x > 0) {
      ans = min(ans, row[x - 1][y]);
    }
    if (y > 0) {
      ans = min(ans, col[x][y - 1]);
    }
    cout << ans << '\n';
  }                                                                 
  return 0;
}