using System;
using System.Linq;
public class Guzva
{
    static long f(long[,] mat, int i, int j, int qi, int qj)
    {
        
        if (i == 0 && j == 0) { return mat[0, 0]; }
        if (j == qj && i - 1 == qi && j == 0) { return 999999999999999999; }
        if (i == qi && j - 1 == qj && i == 0) { return 999999999999999999; }
        if (i == 0 && (j != qj || i - 1 != qi)) { return f(mat, i, j - 1, qi, qj) + mat[i, j]; }
        if (j == 0 && (i != qi || j - 1 != qj)) { return f(mat, i - 1, j, qi, qj) + mat[i, j]; }

        if (j == qj && i - 1 == qi && j != 0) { return f(mat, i, j - 1, qi, qj) + mat[i, j]; }
        if (i == qi && j - 1 == qj && i != 0) { return f(mat, i - 1, j, qi, qj) + mat[i, j]; }

        
         return Math.Min(f(mat, i - 1, j, qi, qj), f(mat, i, j - 1, qi, qj)) + mat[i, j]; 
    }
    public static void Main(string[] args)
    {
        string r1 = Console.ReadLine();
        string[] u1 = r1.Split();
        int n = Convert.ToInt32(u1[0]);
        int m = Convert.ToInt32(u1[1]);
        long[,] mat = new long[n, m];
        for (int i = 0; i < n; i++)
        {
            string r3 = Console.ReadLine();
            string[] u3 = r3.Split();
            for (int j = 0; j < m; j++)
            {
                mat[i, j] = Convert.ToInt64(u3[j]);
            }
        }
        int q = Convert.ToInt32(Console.ReadLine());
        for (int i = 0; i < q; i++)
        {
            string r2 = Console.ReadLine();
            string[] u2 = r2.Split();
            Console.WriteLine(f(mat, n - 1, m - 1, Convert.ToInt32(u2[0]) - 1, Convert.ToInt32(u2[1]) - 1));
        }
        Console.ReadKey();
    }
}