using System;

class HelloWorld {
  static UInt64 Put(UInt64[,] a, UInt64 N, UInt64 M, UInt64 x, UInt64 y){
      UInt64[,] pomoc = new UInt64[N, M];

      pomoc[0, 0] = a[0, 0];
      for (UInt64 j = 1; j < M; j++) {
          if (j == y && x == 0) {
              pomoc[0, j] = UInt64.MaxValue;
          } else if(pomoc[0, j-1] == UInt64.MaxValue) {
             pomoc[0,j] = UInt64.MaxValue;
          } else  pomoc[0, j] = pomoc[0, j - 1] + a[0, j];
      }
      for (UInt64 i = 1; i < N; i++) {
          if (i == x && y == 0) {
              pomoc[i, 0] = UInt64.MaxValue;
          } else if (pomoc[i-1, 0] == UInt64.MaxValue){
              pomoc[i, 0] = UInt64.MaxValue;
          } else  pomoc[i, 0] = pomoc[i - 1, 0] + a[i, 0];
      }
   for (UInt64 i = 1; i < N; i++) {
          for (UInt64 j = 1; j < M; j++) {
              
             UInt64 left = pomoc[i, j - 1];
              UInt64 up = pomoc[i - 1, j];
              pomoc[i, j] = a[i, j] + Math.Min(up, left);

              
              if (i == x && j == y) {
                  pomoc[i, j] = UInt64.MaxValue;
              }
          }
      }
      return pomoc[N - 1, M - 1];
  }

  static void Main() {
    var s = Console.ReadLine().Split();
    UInt64 N = UInt64.Parse(s[0]);
    UInt64 M = UInt64.Parse(s[1]);
    UInt64[,] a = new UInt64[N,M];
    UInt64 i = 0, j =0;
    for(i = 0;i<N;i++){
        s = Console.ReadLine().Split();
        for(j = 0;j<M;j++){
            a[i,j] = UInt64.Parse(s[j]);
        }
    }
    UInt64 Q = UInt64.Parse(Console.ReadLine());
    for(i=0;i<Q;i++){
     s = Console.ReadLine().Split();

    Console.WriteLine(Put(a, N, M, UInt64.Parse(s[0])-1, UInt64.Parse(s[1])-1));
    }
  }
}