import java.util.ArrayList;
import java.util.Scanner;

class Main
{

    public static ArrayList<Integer> findPaths(int[][] mat, ArrayList<Integer> path, int i, int j, int a, int b, ArrayList<Integer> sums)
    {
        int p = 0;
        int maxsum = 0;
        // base case
        if (mat == null || mat.length == 0) {
            return sums;
        }

        int M = mat.length;
        int N = mat[0].length;

        // if the last cell is reached, print the route
        if (i == M - 1 && j == N - 1)
        {

            int sum = 0;
            path.add(mat[i][j]);

            for (int k = 0; k < path.size(); k++) {
                sum += path.get(k);
            }
            if(maxsum == 0){
                maxsum = sum;
            }else if(maxsum > sum){
                maxsum = sum;
            }
            sums.add(sum);
            p++;
            path.remove(path.size() - 1);
            //System.out.println(path + " " + maxsum);


        }

        // include the current cell in the path
        path.add(mat[i][j]);

        // move right
        if ((i >= 0 && i < M && j + 1 >= 0 && j + 1 < N && (j + 1 != b ||  i != a))) {
            findPaths(mat, path, i, j + 1, a, b, sums);
        }

        // move down
        if ((i + 1 >= 0 && i + 1 < M && j >= 0 && j < N && (i + 1 != a || j != b))) {
            findPaths(mat, path, i + 1, j, a, b, sums);
        }
        // backtrack: remove the current cell from the path
        path.remove(path.size() - 1);
        return sums;

    }

    public static void main(String[] args)
    {
        Scanner skener = new Scanner(System.in);
        int a = skener.nextInt();
        int b = skener.nextInt();
        int[][] mat = new int[a][b];

        for (int i = 0; i < a; i++) {
            for (int j = 0; j < b; j++) {
                mat[i][j] = skener.nextInt();
            }
        }

        ArrayList<Integer> path = new ArrayList<>();

        ArrayList<Integer> sums = new ArrayList<>();
        int maxsum = 0;
        int q = skener.nextInt();
        int[] qa = new int[q];
        int[] qb = new int[q];
        for (int i = 0; i < q; i++) {
            qa[i] = skener.nextInt();
            qb[i] = skener.nextInt();
        }
        for (int i = 0; i < q; i++) {
            qa[i]--;
            qb[i]--;
            int min = 0;
            sums = findPaths(mat, path, 0, 0, qa[i], qb[i], sums);
            for (int j = 0; j < sums.size(); j++) {
                if(min == 0){
                    min = sums.get(j);
                }else if(min > sums.get(j)){
                    min = sums.get(j);
                }
            }
            sums = new ArrayList<>();
            System.out.println(min);
        }

    }
}