#include<iostream>
using namespace std;


long long int nmzmax = 999999999;
long long int nmz = 999999999;
long long int m, n;
int** dohe;


void NadjiPut(int startX, int startY, int* putanja, int sledeciKorak)
{

    long long int tzb = 0;

    if (startX == m - 1)
    {
        for (int i = startY; i < n; i++)
        {
            if (dohe[startX][i] == -1)
            {
                return;
            }
            putanja[sledeciKorak + i - startY] = dohe[startX][i];
            //cout << "(" << startX << ")" << "(" << i << ")" << " ";
        }

        for (int i = 0; i < sledeciKorak + n - startY; i++)
        {
            //cout << putanja[i] << " ";
            tzb += putanja[i];
        }

        //cout << "-----" << tzb;
        //cout << endl;

        if (tzb < nmz)
            nmz = tzb;

        return;
    }


    if (startY == n - 1)
    {
        for (int i = startX; i < m; i++)
        {
            if (dohe[i][startY] == -1)
            {
                return;
            }
            putanja[sledeciKorak + i - startX] = dohe[i][startY];
            //cout << "(" << i << ")" << "(" << startY << ")" << " ";
        }

        for (int i = 0; i < sledeciKorak + m - startX; i++)
        {
            //cout << putanja[i] << " ";
            tzb += putanja[i];
        }

        if (tzb < nmz)
            nmz = tzb;
        //cout << "-----" << tzb;
        //cout << endl;
        return;
    }


    if (dohe[startX][startY] == -1)
    {
        return;
    }
    putanja[sledeciKorak] = dohe[startX][startY];
    //cout << "(" << startX << ")" << "(" << startY << ")" << " ";

    // pogledaj desno
    NadjiPut(startX + 1, startY, putanja, sledeciKorak + 1);

    // pogledaj dole
    NadjiPut(startX, startY + 1, putanja, sledeciKorak + 1);

}


int main()
{

    cin >> m >> n;
    dohe = new int* [m];
    for (int i = 0; i < m; i++)
    {
        dohe[i] = new int[n];
    }

    for (long long int i = 0; i < m; i++)
    {
        for (long long int j = 0; j < n; j++)
        {
            cin >> dohe[i][j];
        }
    }

    int ukupnoBlokiranih;
    cin >> ukupnoBlokiranih;


    for (int i = 0; i < ukupnoBlokiranih; i++)
    {
        int blokX, blokY;
        cin >> blokX >> blokY;
        blokX--;
        blokY--;
        int temp = dohe[blokX][blokY];
        dohe[blokX][blokY] = -1;
        nmz = nmzmax;
        int* putanja = new int[m + n];

        NadjiPut(0, 0, putanja, 0);

        dohe[blokX][blokY] = temp;
        //cout << endl << endl << nmz << endl;
        cout << nmz << endl;
    }

    return 0;
}