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

#define maksA 1005
const int maks = 1e9;

int n, m, q;
int a[maksA][maksA];
int br[maksA + 1][maksA + 1];

void ucitaj (int n,int m,int a[][maksA])
{
    int i,j;
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j<=m; j++)
        {
            scanf("%lld",&a[i][j]);
        }
    }
}
/*void pisi(int n, int m, int a[][1000])
{
    int i,j;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
            printf("%5d ",a[i][j]);
        }
        printf("\n");
    }
}*/

int minimum (int A,int B)
{
    return (A < B) ? A : B;
}

void fill (int n, int m, int br[][maksA+1], int x)
{
    int i,j;
    for (i = 0; i <= n; i++)
    {
        for (j = 0; j <= m; j++)
        {
            br[i][j] = x;
        }
    }
}
int main()
{
    int i,j,x,y;
    scanf("%d%d",&n,&m);

    ucitaj(n,m,a);
    scanf("%d",&q);
    while (q--)
    {
        scanf("%d%d",&x,&y);

        fill(n,m,br,maks);

        br[1][1] = a[1][1];
        for (i = 1; i <= n; i++)
        {
            for (j = 1; j <= m; j++)
            {
                if (x == i && y == j) continue;

                br[i][j] = minimum(br[i][j],(br[i-1][j] + a[i][j]));
                br[i][j] = minimum(br[i][j],(br[i][j-1] + a[i][j]));
            }
        }
        printf("%lld\n",br[n][m]);
    }
    return 0;
}