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

namespace ConsoleApp1
{
    class Program
    {

        struct Par
        {
            public int x;
            public int y;
        }

        struct Resenje
        {
            public int x;
            public List<Par> l;
        }

        static void Bektrek(int[,] a, int i, int j, int s, int n, int m, int p, int[] x, int[] y, List<Resenje> r)
        {
            s += a[i, j];
            if (i == n && j == m)
            {
                Resenje t = new Resenje();
                t.x = s;
                t.l = new List<Par>();
                for (int k = 0; k < p; k++)
                {
                    Par c = new Par();
                    c.x = x[k];
                    c.y = y[k];
                    t.l.Add(c);
                }
                r.Add(t);
            }
            if (i < n)
            {
                x[p] = i + 1;
                y[p] = j;
                Bektrek(a, i + 1, j, s, n, m, p + 1, x, y, r);
            }
            if (j < m)
            {
                x[p] = i;
                y[p] = j + 1;
                Bektrek(a, i, j + 1, s, n, m, p + 1, x, y, r);
            }
        }

        static int Ispisi(int x, int y, List<Resenje> r)
        {
            for (int i = r.Count - 1; i >= 0; i--)
            {
                bool t = true;
                foreach (Par p in r[i].l)
                {
                    if (x == p.x && y == p.y)
                    {
                        t = false;
                        break;
                    }
                }
                if (t)
                {
                    return r[i].x;
                }
            }
            return 0;
        }

        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];
            List<Resenje> r = new List<Resenje>();
            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]);
                }
            }
            int[] x = new int[n + m + 3], y = new int[n + m + 3];
            Bektrek(a, 0, 0, 0, n - 1, m - 1, 0, x, y, r);
            r.OrderBy(Resenje => Resenje.x);
            int k = int.Parse(Console.ReadLine());
            for (int i = 0; i < k; i++)
            {
                s = Console.ReadLine().Split();
                int xi = int.Parse(s[0]) - 1, yi = int.Parse(s[1]) - 1;
                Console.WriteLine(Ispisi(xi, yi, r));
            }
        }
    }
}