using System;
class HelloWorld {
  static void Total(int[,] matrica, int curX, int curY, int a, int b, int N, int M) {
       int sum = matrica[0, 0];
       while(curX != N-1 || curY != M-1){
        if(curY < M-1 && (curY+1 != b  || curX > a) ){ //NA DESNO
  
            curY++;
            sum+=matrica[curX, curY];
            
        } else{//DOLE
         
            curX++;
            sum+=matrica[curX, curY];  
           
        }
    
    
    }
    Console.WriteLine(sum);
  }
  static void Main() {
        string[] s = Console.ReadLine().Split();
        int N = int.Parse(s[0]);
        int M = int.Parse(s[1]);
        int[,] matrica = new int[N, M];
 
        for (int i = 0; i < N; i++)
        {
            s = Console.ReadLine().Split();
            for (int j = 0; j < M; j++)
            {
                matrica[i, j] = int.Parse(s[j]);
            }
        }
  
    int Q = int.Parse(Console.ReadLine());
   
    for(int i =0;i <Q;i++){
         s = Console.ReadLine().Split();
         int a = int.Parse(s[0])-1;
         int b= int.Parse(s[1])-1;
          int curX = 0, curY = 0;
         Total(matrica, curX, curY, a, b ,N, M);
    }
   
  }
}