// treci.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
using namespace std;

long min(long a, long b)
{
    if (a < b)
    {
        return a;
    }
    else
    {
        return b;
    }

}
long minLjudi(long* mat, long sx, long sy, long N, long M, long xi, long yi)
{
    if (sx == M-1 && sy == N)
    {
       // cout << "cilj_desno " << "sx: " << sx << " sy: " << sy << " xi: " << xi << " yi:" << yi << endl;
       // cout << mat[(sy - 1) * N + sx - 1] << endl;
       // cout << (mat[(sy - 1) * N + sx - 1] + mat[N * (sy - 1) + sx - 1/*-1*/ + 1]) << endl;
        long a = (mat[(sy - 1) * N + sx - 1] + mat[N * (sy - 1) + sx-1/*-1*/ + 1]);
        
        return a;
    }
    else if (sx == M && sy == N-1)
    {
       // cout << "cilj_ispod " << "sx: " << sx << " sy: " << sy << " xi: " << xi << " yi:" << yi << endl;
       // cout << mat[(sy - 1) * N + sx - 1] << endl;
        //cout << (mat[(sy - 1) * N + sx - 1] + mat[N * (sy - 1 +1/*-1*/) + sx - 1]) << endl;
        long a = (mat[(sy - 1) * N + sx - 1] + mat[N * (sy-1+1 /*-1*/)+sx - 1]);
       // cout << "sx: " << sx << " sy: " << "A: " << a << endl;
        return a;
    }
    else
    {
        if ((sx + 1 == xi&&sy == yi) && sy != N||sx==M)
        {
          //  cout <<"blok_desno " << "sx: " << sx << " sy: " << sy << " xi: "<<xi<<" yi:"<<yi << endl;
            //cout << mat[(sy - 1) * N + sx - 1] << endl;
         //   cout << (mat[(sy - 1) * N + sx - 1] + minLjudi(mat, sx, sy + 1, N, M, xi, yi)) << endl;
            long a = (mat[(sy - 1) * N + sx - 1] + minLjudi(mat, sx, sy+1, N, M, xi, yi));
         //   cout << "sx: " << sx << " sy: " << "A: " << a << endl;
            return a;
        }
        else if ((sx == xi && sy+1 == yi) &&sx != M || sy == N)
        {
         //   cout <<"blok_ispod " << "sx: " << sx << " sy: " << sy << " xi: " << xi << " yi:" << yi << endl;
          //  cout << mat[(sy - 1) * N + sx - 1] << endl;
         //   cout << (mat[(sy - 1) * N + sx - 1] + minLjudi(mat, sx + 1, sy, N, M, xi, yi))<<endl;
            long a = (mat[(sy - 1) * N + sx-1] + minLjudi(mat, sx+1, sy, N, M, xi, yi));
          //  cout << "sx: " << sx << " sy: " << "A: " << a << endl;
            return a;
        }
        else
        {
          //  cout << "sx: " << sx << " sy: " << sy << endl;
         //   cout << mat[(sy - 1) * N + sx - 1] << endl;
            long a= min(mat[(sy - 1) * N + sx - 1] + minLjudi(mat, sx + 1, sy, N, M, xi, yi), mat[(sy - 1) * N + sx - 1] + minLjudi(mat, sx, sy + 1, N, M, xi, yi)) ;
          //  cout << "sx: " << sx << " sy: " << "A: "<<a << endl;
            return a;
        }
    }

}


int main()
{
    long N, M;
    cin >> N;
    cin >> M;
    long* mat = new long(N * M);
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
            cin >> *(mat+i * N + j);
        }
    }
    int Q;
    cin >> Q;
    long *arr = new long[Q*2];
    for (int i = 0; i < Q; i++)
    {
        cin >> arr[i * 2 + 0];
        cin >> arr[i * 2 + 1];
    }
    for (int i = 0; i < Q; i++)
    {
        
        cout << minLjudi(mat, 1, 1, N, M, arr[i*2+0], arr[i*2+1]) << endl;


    }
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file