using System;

namespace Takmicenje
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] st = Console.ReadLine().Split();
            int n = int.Parse(st[0]);
            int m = int.Parse(st[1]);
            long[,] a = new long[n, m];
            for (int i = 0; i < n; i++)
            {
                st = Console.ReadLine().Split();
                for (int j = 0; j < m; j++)
                {
                    a[i, j] = int.Parse(st[j]);
                }
            }
            long[,] b = new long[n, m];
            b[0, 0] = a[0, 0];
            for (int i = 1; i < n; i++)
            {
                b[i, 0] = b[i - 1, 0] + a[i, 0];
            }
            for (int j = 1; j < m; j++)
            {
                b[0, j] = b[0, j - 1] + a[0, j];
            }
            for (int i = 1; i < n; i++)
            {
                for (int j = 1; j < m; j++)
                {
                    b[i, j] = (b[i, j - 1] > b[i - 1, j] ? b[i - 1, j] : b[i, j - 1]) + a[i, j];
                }
            }
            int q = int.Parse(Console.ReadLine());
            long[,] c = new long[n, m];
            for (int i = 0; i < q; i++)
            {
                st = Console.ReadLine().Split();
                int x = int.Parse(st[0]) - 1;
                int y = int.Parse(st[1]) - 1;
                long z = 0;
                c[x, y] = long.MaxValue;
                for (int j = x + 1; j < n; j++)
                {
                    if (y == 0)
                    {
                        c[j, y] = long.MaxValue;
                    }
                    else
                    {
                        c[j, y] = (b[j, y - 1] < c[j - 1, y] ? b[j, y - 1] : c[j - 1, y]) + a[j, y];
                    }
                }
            
                for (int j = y + 1; j < m; j++)
                {
                    if (x == 0)
                    {
                        c[x, j] = long.MaxValue;
                    }
                    else
                    {
                        c[x, j] = (b[x - 1, j] < c[x, j - 1] ? b[x - 1, j] : c[x, j - 1]) + a[x, j];
                    }
                }
                for (int j = x + 1; j < n; j++)
                {
                    for (int k = y + 1; k < m; k++)
                    {
                        c[j, k] = (c[j, k - 1] > c[j - 1, k] ? c[j - 1, k] : c[j, k - 1]) + a[j, k];
                    }
                }
                Console.WriteLine(c[n - 1, m - 1]);
            }
        }
    }
}