#include <stdio.h>
int min(int a, int b)
{
    if (a<b)
    {
        return a;
    }
    else
    {
        return b;
    }
}
int main() {
    int rows, columns, d=0;
    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 = 1; c <= cases; c++) {
        int a, b;
        scanf("%d%d", &a, &b);
        int i=1;
        int j=1;
        while (i!=a && j!=b)
        {
            if (matrix[i+1][j]!=matrix[a][b] || matrix[i][j+1]!=matrix[a][b])
            {
                d=d+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])
                {
                    j++;
                }
                else
                {
                    i++;
                }
            }
            else
            {
                d=d+matrix[i+1][j] + matrix[i][j+1] -matrix[a][b];
            }
        }
        d=d+matrix[rows][columns];
        printf("%d", d);
    }
    return 0;
}