using System;
class HelloWorld {
  static UInt64 Positions(UInt64[,] a, UInt64 N, UInt64 M, UInt64 x, UInt64 y){
      UInt64 i  = 0, j =0, temp = 0;
       UInt64[,] b = (UInt64[,])a.Clone();
      for(i = 0;i<N;i++){
        for(j=0;j<M;j++){
          UInt64 left = j > 0 ? b[i, j-1] : UInt64.MaxValue;
          UInt64 above = i > 0 ? b[i-1, j] : UInt64.MaxValue;
          if(left == UInt64.MaxValue && above == UInt64.MaxValue) {}
          else b[i, j] = b[i,j] +  Math.Min(left, above);
          if(i == x && j == y) {
              b[i,j] = UInt64.MaxValue;
          }
          else if((i > 0 || j>0) && left == UInt64.MaxValue && above == UInt64.MaxValue) {
              b[i,j]= UInt64.MaxValue;
          }
         
        }
      }
      return b[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(Positions(a, N, M, UInt64.Parse(s[0])-1, UInt64.Parse(s[1])-1));
    }
  }
}