#include <bits/stdc++.h>

using namespace std;

int minSumPathUtil(int i, int j, vector<vector<int>> &matrix, vector<vector<int>> &dp, int qn, int qm)
{
  if (i==0 && j == 0) return matrix[0][0];
  if ((i<0 || j<0) || (i==qn && j==qm)) return 1e9;
  if (dp[i][j]!=-1) return dp[i][j];
    
  int up = matrix[i][j]+minSumPathUtil(i-1,j,matrix,dp,qn,qm);
  int left = matrix[i][j]+minSumPathUtil(i,j-1,matrix,dp,qn,qm);
  
  return dp[i][j] = min(up,left);
}

int minSumPath(int n, int m, vector<vector<int> > &matrix, int qn, int qm){
    vector<vector<int> > dp(n, vector<int>(m,-1));
    return minSumPathUtil(n-1,m-1,matrix,dp,qn,qm);   
}

int main() {
int n;
    cin >> n;
    int m;
    cin >> m;
    vector<vector<int>> matrix(n);
    for (int i = 0; i < n; i++) {
        matrix[i].resize(m);
        for (int j = 0; j < m; j++) cin >> matrix[i][j];
    }
    int Q;
    int qn, qm;
    cin >> Q;
    for (int i = 0; i < Q; i++) {
        cin >> qn >> qm;
        cout<<minSumPath(n,m,matrix,qn-1,qm-1)<<endl;
    }  
}