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

namespace Guzva
{
    class Program
    {
        static long[,] Ucitaj(long n, long m)
        {
            long[,] a = new long[n, m];
            for (int i = 0; i < n; i++)
            {
                string[] s = Console.ReadLine().Split();
                for (int j = 0; j < m; j++)
                {
                    a[i, j] = long.Parse(s[j]);
                }
            }
            return a;
        }
        static long Min(long[,] a, int x, int y)
        {
            int n = a.GetLength(0);
            int m = a.GetLength(1);
            long[,] dp = new long[n, m];
            bool[,] nemoguce = new bool[n, m];
            nemoguce[x, y] = true;
            dp[0, 0] = a[0, 0];
            for (int i = 1; i < n; i++)
            {
                if (y == 0)
                    nemoguce[i, 0] = true;
                else
                    dp[i, 0] = dp[i - 1, 0] + a[i, 0];
            }
            for (int j = 1; j < m; j++)
            {
                if (x == 0)
                    nemoguce[0, j] = true;
                else
                    dp[0, j] = dp[0, j-1] + a[0, j];
            }
            for (int i = 1; i < n; i++)
            {
                for (int j = 1; j < m; j++)
                {
                    if (nemoguce[i - 1, j])
                        dp[i, j] = dp[i, j - 1] + a[i, j];
                    else if (nemoguce[i, j - 1])
                        dp[i, j] = dp[i - 1, j] + a[i, j];
                    else
                        dp[i, j] = Math.Min(dp[i - 1, j], dp[i, j - 1]) + a[i, j];
                }
            }
            return dp[n - 1, m - 1];
        }
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split();
            int n = int.Parse(s[0]);
            int m = int.Parse(s[1]);
            long[,] a = Ucitaj(n, m);
            int q = int.Parse(Console.ReadLine());
            for (int i = 0; i < q; i++)
            {
                s = Console.ReadLine().Split();
                int x = int.Parse(s[0])-1;
                int y = int.Parse(s[1])-1;
                Console.WriteLine(Min(a,x,y));
            }
        }
    }
}