#include <iostream>
#include <limits>
#include <stdio.h>
#include <stdlib.h>
int move(int**matrix,int sum,int x,int y,int k,int j){
  if((x==k)&&(y==j)){
    return sum;}
  else if ((x - 1 == k) && (y > j)){
    for (int i = j; i < y; i++){
      sum = sum + matrix[k][i];
    }
    return sum;}
    else if ((x > k) && (y - 1 == j)){
    for (int i = k; i < x; i++) {
      sum = sum + matrix[i][j];
    }
    return sum;
  }
  sum = sum + matrix[k][j];
  return std::min(move(matrix, sum, x, y, k + 1, j),
                  move(matrix, sum, x, y, k, j + 1));}
int main(){
  int x, y;
  scanf("%d%d", &x, &y);
  int **matrix;
  matrix = (int **)malloc(x * sizeof(int *));
  for (int i = 0; i < y; i++) {
    matrix[i] = (int *)malloc(y * sizeof(int));}
  for (int i = 0; i < x; i++){
    for (int j = 0; j < y; j++){
      scanf("%d", &matrix[i][j]);
    }
  }
  int q;
  scanf("%d", &q);
  int arrx[q];
  int arry[q];
  for (int i = 0; i < q; i++){
    scanf("%d%d", &arrx[i], &arry[i]);}
  int a;
  int k, j;
  for (int i = 0; i < q; i++) {
    a = matrix[arrx[i] - 1][arry[i] - 1];
    matrix[arrx[i] - 1][arry[i] - 1] = 100;
    int sum = 0;
    k = 0;
    j = 0;
    printf("%d\n", move(matrix, sum, x, y, k, j));
    matrix[arrx[i] - 1][arry[i] - 1] = a;
  }
  return 0;
}