using System;
using System.Collections.Generic;

namespace Takmicenje___2022_12_17
{
    internal class Program
    {
        static long NajmanjiZbir(int[,] tabla, int n, int m, long[,] memo, int x, int y)
        {
            if (memo[n, m] != -1) return memo[n, m];
            if ((x == n + 1 && y == m) || (x == n && y == m + 1)) memo[x, y] = int.MaxValue;
            if (n == tabla.GetLength(0) && m == tabla.GetLength(1)) return memo[n, m] = tabla[n - 1, m - 1];
            if (n == tabla.GetLength(0)) return memo[n, m] = tabla[n - 1, m - 1] + NajmanjiZbir(tabla, n, m + 1, memo, x, y);
            if (m == tabla.GetLength(1)) return memo[n, m] = tabla[n - 1, m - 1] + NajmanjiZbir(tabla, n + 1, m, memo, x, y);
            return memo[n, m] = Math.Min(tabla[n - 1, m - 1] + NajmanjiZbir(tabla, n + 1, m, memo, x ,y), tabla[n - 1, m - 1] + NajmanjiZbir(tabla, n, m + 1, memo, x, y));
        }


        static void Main(string[] args)
        {
            string[] temp = Console.ReadLine().Split(' ');
            int m = int.Parse(temp[0]);
            int n = int.Parse(temp[1]);
            int[,] tabla = new int[m, n];
            for (int i = 0; i < m; i++)
            {
                temp = Console.ReadLine().Split(' ');
                for (int j = 0; j < n; j++)
                {
                    tabla[i, j] = int.Parse(temp[j]);
                }
            }
            int q = int.Parse(Console.ReadLine());
            List<long> list = new List<long>();
            for (int i = 0; i < q; i++)
            {
                temp = Console.ReadLine().Split(' ');
                int x = int.Parse(temp[0]);
                int y = int.Parse(temp[1]);
                long[,] memo = new long[m + 1, n + 1];
                for (int j = 0; j <= m; j++)
                {
                    for (int k = 0; k <= n; k++)
                    {
                        memo[j, k] = -1;
                    }
                }
                list.Add(NajmanjiZbir(tabla, 1, 1, memo, x, y));
            }

            foreach (var item in list)
            {
                Console.WriteLine(item);
            }
        }
    }
}