using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {

        static void Dinamicko(int n, int m, int[,] a, int[,] d)
        {
            d[0, 0] = a[0, 0];
            for (int i = 1; i < n; i++)
            {
                d[i, 0] = a[i, 0] + d[i - 1, 0];
            }
            for (int i = 1; i < m; i++)
            {
                d[0, i] = a[0, i] + d[0, i - 1];
            }
            for (int i = 1; i < n; i++)
            {
                for (int j = 1; j < m; j++)
                {
                    d[i, j] = a[i, j] + Math.Min(d[i, j - 1], d[i - 1, j]);
                }
            }
        }

        static int Ispisi(int n, int m, int x, int y, int[,] a, int[,] d)
        {
            int[,] b = new int[n, m];
            b[x, y] = int.MaxValue;
            if (x == 0)
            {
                for (int i = y + 1; i < m; i++)
                {
                    b[x, i] = int.MaxValue;
                }
            }
            else
            {
                for (int i = y + 1; i < m; i++)
                {
                    b[x, i] = a[x, i] + Math.Min(b[x, i - 1], d[x - 1, i]);
                }
            }
            if (y == 0)
            {
                for (int i = x + 1; i < n; i++)
                {
                    b[i, y] = int.MaxValue;
                }
            }
            else
            {
                for (int i = x + 1; i < n; i++)
                {
                    b[i, y] = a[i, y] + Math.Min(b[i - 1, y], d[i, y - 1]);
                }
            }
            for (int i = x + 1; i < n; i++)
            {
                for (int j = y + 1; j < m; j++)
                {
                    b[i, j] = a[i, j] + Math.Min(b[i, j - 1], b[i - 1, j]);
                }
            }
            return b[n - 1, m - 1];
        }

        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split();
            int n = int.Parse(s[0]), m = int.Parse(s[1]);
            int[,] a = new int[n, m], d = new int[n, m];
            for (int i = 0; i < n; i++)
            {
                s = Console.ReadLine().Split();
                for (int j = 0; j < m; j++)
                {
                    a[i, j] = int.Parse(s[j]);
                }
            }
            Dinamicko(n, m, a, d);
            int k = int.Parse(Console.ReadLine());
            for (int i = 0; i < k; i++)
            {
                s = Console.ReadLine().Split();
                int x = int.Parse(s[0]) - 1, y = int.Parse(s[1]) - 1;
                Console.WriteLine(Ispisi(n, m, x, y, a, d));
            }
        }
    }
}