#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
int matrix[10000] [10000];
long long Check(int N, int M,int a, int b)
{ 
    int m = matrix[a][b];
    matrix[a][b] = 20000000;
    long long Solutions[N] [M];
    for(int i = 1; i < N; i++)
    { 
        Solutions[i][0] = Solutions[i - 1] [0] + matrix[i][0];
    }
    for(int i = 1; i < M; i++)
    { 
        Solutions[0][i] = Solutions[0] [i - 1] + matrix[0][i];
    }    
    for(int i = 1; i < N; i++)
    {

        for(int j = 1; j < M; j++)
        { 
            if(Solutions [i - 1][j] > Solutions[i][j - 1])
            { 
                Solutions[i][j - 1] = Solutions[i][j - 1] + matrix[i][j];
            }
            else
            { 
                Solutions[i - 1][j] = Solutions[i - 1][j] + matrix[i][j];
            }
        }
    }
    matrix[a][b]= m;
    return Solutions[N -1][M - 1];
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int N, M;
    cin >> N >> M;
    for(int i = 0; i < N; i++)
        for(int j = 0; j < M; j++)
            cin >> matrix[i][j];
    
    long long Solutions[N] [M];
    vector<int> Arry[N] [M];
    for(int i = 1; i < N; i++)
    { 
        Solutions[i][0] = Solutions[i - 1] [0] + matrix[i][0];
        Arry[i][0] = Arry[i-1][0];
        Arry[i][0].push_back(i * M);
    }
    for(int i = 1; i < M; i++)
    { 
        Solutions[0][i] = Solutions[0] [i - 1] + matrix[0][i];
        Arry[0][i] = Arry[0][i - 1];
        Arry[0][i].push_back(i);
    }    
    for(int i = 1; i < N; i++)
    {

        for(int j = 1; j < M; j++)
        { 
            if(Solutions [i - 1][j] > Solutions[i][j - 1])
            { 
                Solutions[i][j - 1] = Solutions[i][j - 1] + matrix[i][j];
                Arry[i][j] = Arry[i][j - 1];
                Arry[i][j].push_back(i * M + j);
            }
            else
            { 
                Solutions[i - 1][j] = Solutions[i - 1][j] + matrix[i][j];
                Arry[i][j] = Arry[i - 1][j];
                Arry[i][j].push_back(i * M + j);
            }
        }
    }
    vector<int> Solved (N * M, Solutions[N -  1] [M - 1]); 
    for(int i = 0; i < Arry[N - 1][M - 1].size(); i++)
    { 
        int a  = Arry[N -  1] [M - 1] [i] / M;
        int b = Arry[N -  1] [M - 1] [i] % M;
        Solved[Arry[N - 1][M - 1][i]] = Check(N , M, a, b);

    }



    int Q;
    cin >> Q;
    int ai, bi;
    for(int i = 0; i < Q; i++)
    { 
            cin >> ai >> bi;
            cout << Solved[ai * M + bi];
    }








    return 0;
}