package Zadaci;
import java.util.Scanner; 
public class Zadatak {
		
	    public static int findMinCost(int[][] cost)
	    {
	       
	        if (cost == null || cost.length == 0) {
	            return 0;
	        }
	 
	      
	        int M = cost.length;
	        int N = cost[0].length;
	 
	        
	        int[][] T = new int[M][N];
	 

	        for (int i = 0; i < M; i++)
	        {
	            for (int j = 0; j < N; j++)
	            {
	                T[i][j] = cost[i][j];
	                if (i == 0 && j > 0) {
	                    T[0][j] += T[0][j - 1];
	                }
	 
	                
	                else if (j == 0 && i > 0) {
	                    T[i][0] += T[i - 1][0];
	                }
	 
	                
	                else if (i > 0 && j > 0) {
	                    T[i][j] += Integer.min(T[i - 1][j], T[i][j - 1]);
	                }
	            }
	        }
	 
	        
	        return T[M - 1][N - 1];
	    }
	 
	    	public static void main(String[] args)
	    	{
	    		Scanner sc=new Scanner(System.in);  

		        int n,m;
		        n=sc.nextInt();
		        m=sc.nextInt();
		        int [][] doha=new int[n][m];
		        for(int i=0;i<n;i++) {
		        	for(int j=0;j<m;j++) {
		        		doha[i][j]=sc.nextInt();
		        	}
		        }
		        
		        
		        int br = sc.nextInt();
		        int [] resenja=new int [br];
		        for(int i=0;i<br;i++) {
		        	int [][] doha2= doha;
		        	int b,f;
		        	b=sc.nextInt();
		        	f=sc.nextInt();
		        	doha2[b-1][f-1]=1000000;
		        	resenja[i]=findMinCost(doha2);
		        }
		        for(int i=0;i<br;i++) {
		        	System.out.println(resenja[i]);
		        }
		 
		      //  System.out.println("The minimum cost is " + findMinCost(doha));
		     //   System.out.println("The minimum cost is " + findMinCost(doha));
		    }
		}