#include <iostream>

using namespace std;

int main(void){

    unsigned long long int n, m;
    cin >> n >> m;

    unsigned long long int matrica[n][m];

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

    unsigned long long int brojScenarija;
    cin >> brojScenarija;

    unsigned long long int x[brojScenarija], y[brojScenarija];
    for (unsigned long long int i = 0; i < brojScenarija; ++i)
    {
        cin >> x[i] >> y[i];
    }

    for (unsigned long long int k = 0; k < brojScenarija; ++k)
    {
        unsigned long long int blokX = x[k] - 1;
        unsigned long long int blokY = y[k] - 1;
    
        unsigned long long int i = 0, j = 0;

        unsigned long long int put = matrica[0][0];

        while (!(i == (n-1) && j == (m-1)))
        {
            if((i==blokX && (j+1)==blokY) && (i+1) > (n-1)){
                put -= matrica[i][j];
                --i;
                ++j;
                put += matrica[i][j];
            }
            else if(((i+1)==blokX && j==blokY) && ((j+1) > (m-1))){
                put -= matrica[i][j];
                --j;
                ++i;
                put += matrica[i][j];
            }
            else if((i==blokX && (j+1)==blokY) || ((j+1) > (m-1))){
                ++i;
                put += matrica[i][j];
            }
            else if(((i+1)==blokX && j==blokY) || (i+1) > (n-1)){
                ++j;
                put += matrica[i][j];
            }
            else{
                if(matrica[i+1][j] < matrica[i][j+1]){
                    ++i;
                    put += matrica[i][j];
                }
                else if(matrica[i][j+1] < matrica[i+1][j]){
                    ++j;
                    put += matrica[i][j];
                }
                else{
                    ++j;
                    put += matrica[i][j];
                }
            }
        }
        cout << put << endl;
    }
    

    return 0;
}