#include <stdio.h>
#include <stdlib.h>

#define MIN(a, b) (a < b ? a : b)

void pretraga(int, int, int);

int n, m, min = 0x7fffffff;
int x[500000], y[500000];
int brojac = 0;
int **doha;

int main()
{
    int i, j, q;

    scanf("%d%d", &n, &m);

    doha = malloc(n * sizeof(int *));

    for (i = 0; i < n; i++) {
        doha[i] = malloc(m * sizeof(int));

        for (j = 0; j < m; j++) {
            scanf("%d", doha[i] + j);
        }
    }

    scanf("%d", &q);

    for (i = 0; i < q; i++) {
        scanf("%d%d", x + i, y + i);
        x[i]--;
        y[i]--;
    }
    
    while (q--)
    {
        pretraga(0, 0, 0);
        printf("%d\n", min);
        min = 0x7fffffff;
        brojac++;
    }

    while (n--)
        free(doha[n]);
    
    free(doha);
}

void pretraga(int red, int kol, int sretnuto)
{
    sretnuto += doha[red][kol];

    if (red == n - 1 && kol == m - 1) {
        min = MIN(sretnuto, min);
        return;
    }

    if (red != n - 1 && !(red + 1 == x[brojac] && kol == y[brojac]))
        pretraga(red + 1, kol, sretnuto);
    if (kol != m - 1 && !(kol + 1 == y[brojac] && red == x[brojac]))
        pretraga(red, kol + 1, sretnuto);
}