#include <iostream>
#include <vector>
#include <queue>
using namespace std;

typedef pair<int, pair<int, int> > kord;

int main(){
    int n, m;
    cin >> n >> m;
    vector<vector<int> > v;
    vector<vector<int> > a;
    vector<vector<int> > temp;
    vector<int> p1, p2;
    for (int i = 0; i < n; i++)
    {
        vector<int> pom;
        p1.clear(); p2.clear();
        for (int j = 0; j < m; j++)
        {
            int broj;
            cin >> broj;
            pom.push_back(broj);
            p1.push_back(0);
            p2.push_back(INT_MAX);
        }
        v.push_back(pom);
    }

    int q;
    cin >> q;
    vector<int> sol;

    priority_queue<kord, vector<kord>, greater<kord> > t;

    for (int brojac = 0; brojac < q; brojac++)
    {
        int x, y;
        cin >> x >> y;
        a.clear(); temp.clear();
        for (int brojac2 = 0; brojac2 < n; brojac2++)
        {
            a.push_back(p1);
            temp.push_back(p2);
        }
        
        a[0][0] = v[0][0];
        a[x-1][y-1] = -1;
        temp[0][0] = -1;
        temp[x-1][y-1] = -1;
        int i = 0, j = 0;

        while(a[n-1][m-1]==0){
            if(i < n-1){
                int tr = a[i][j] + v[i+1][j];
                if(tr < temp[i+1][j]){
                    temp[i+1][j] = tr;
                    t.emplace(make_pair(tr, make_pair(i+1, j)));
                }
            }
            if(j < m-1){
                int tr = a[i][j] + v[i][j+1];
                if(tr < temp[i][j+1]){
                    temp[i][j+1] = tr;
                    t.emplace(make_pair(tr, make_pair(i, j+1)));
                }
            }
            kord minK = t.top();
            t.pop();
            if(a[minK.second.first][minK.second.second] == 0){
                a[minK.second.first][minK.second.second] = minK.first;
                i = minK.second.first; j = minK.second.second;
            }
                
        }

        sol.push_back(a[n-1][m-1]);
    }
    
    for(int x : sol)
        cout << x << endl;
    
}