#include <stdio.h>
int min(int a, int b)
{
    if (a<b)
    {
        return a;
    }
    else
    {
        return b;
    }
}
int main() {
    int rows, columns, a, b;
    scanf("%d%d", &rows, &columns);
    int matrix[rows][columns];
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
    int cases;
    scanf("%d", &cases);
    for (int c = 0; c < cases; c++) {
        int row, column;
        scanf("%d%d", &row, &column);

        int sum = 0;
        for (int a = 0; a <= row; a++) {
            for (int b = 0; b <= column; b++) {
                sum += matrix[a][b];
            }
        }
        int i=1;
        int j=1;
        while (i!=a && j!=b)
        {
            if (i*(j+1)==a*b || (i+1)*j!=a*b)
                c=c+min(matrix[i+1][j], matrix[i][j+1]);
                if (matrix[i+1][j] + matrix[i][j+1]-min(matrix[i+1][j], matrix[i][j+1])==matrix[i+1][j])
                {
                    i++;
                }
                else
                {
                    j++;
                }
            else
            {
                c=c+matrix[i+1][j] + matrix[i][j+1] -matrix[a][b];
            }
        }
        c=c+matrix[rows][columns];
        printf("%d", a);
    }

    return 0;
}