#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Settlement {
	public:
	template < typename T > static T input() {
		T value;
		std::cin >> value;
		return value;
	}
};
class Main
{
    public:
    static std::vector<int> findPaths(std::vector<std::vector<int> > &mat, std::vector<int> &path, int i, int j, int a, int b, std::vector<int> &sums)
    {
        int p = 0;
        int maxsum = 0;
        int M = mat.size();
        int N = mat[0].size();
        if (i == M - 1 && j == N - 1)
        {
            int sum = 0;
            path.push_back(mat[i][j]);
            for (int k = 0; k < path.size(); k++)
            {
                sum += path[k];
            }
            if (maxsum == 0)
            {
                maxsum = sum;
            }
            else if (maxsum > sum)
            {
                maxsum = sum;
            }
            sums.push_back(sum);
            p++;
            path.erase(path.begin() + (path.size() - 1));
        }
        path.push_back(mat[i][j]);
        if ((i >= 0 && i < M && j + 1 >= 0 && j + 1 < N && (j + 1 != b || i != a)))
        {
            Main::findPaths(mat, path, i, j + 1, a, b, sums);
        }
        if ((i + 1 >= 0 && i + 1 < M && j >= 0 && j < N && (i + 1 != a || j != b)))
        {
            Main::findPaths(mat, path, i + 1, j, a, b, sums);
        }
        path.erase(path.begin() + (path.size() - 1));
        return sums;
    }
    static void main(std::vector<std::string> &args)
    {
        int a = Settlement::input<int>();
        int b = Settlement::input<int>();
        std::vector<std::vector<int> > mat(a,std::vector<int>(b));
        for (int i = 0; i < a; i++)
        {
            for (int j = 0; j < b; j++)
            {
                mat[i][j] = Settlement::input<int>();
            }
        }
        std::vector<int> path;
        std::vector<int> sums;
        int maxsum = 0;
        int q = Settlement::input<int>();
        std::vector<int> qa(q);
        std::vector<int> qb(q);
        for (int i = 0; i < q; i++)
        {
            qa[i] = Settlement::input<int>();
            qb[i] = Settlement::input<int>();
        }
        for (int i = 0; i < q; i++)
        {
            qa[i]--;
            qb[i]--;
            int min = 0;
            sums = Main::findPaths(mat, path, 0, 0, qa[i], qb[i], sums);
            for (int j = 0; j < sums.size(); j++)
            {
                if (min == 0)
                {
                    min = sums[j];
                }
                else if (min > sums[j])
                {
                    min = sums[j];
                }
            }
            sums.clear();
            std::cout << min << std::endl;
        }
    }
};
int main(int argc, char **argv){
	std::vector<std::string> parameter(argv + 1, argv + argc);
	Main::main(parameter); 
	return 0;
};