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

namespace trazenje
{
    internal class Program
    {
        static int put(int x,int y,int[,] a,int n,int m,int zx,int zy)
        {
            if (x==n - 1 && y == m -1)
                return a[x,y];
            int k=0;
            int l=0;

            if (x<n-1 && (x != zx - 2 || y != zy-1))
                k = put(x+1, y, a, n, m, zx, zy);
            if (y< m - 1 && (y != zy - 2 || x != zx-1))
                l = put(x, y+1, a, n, m, zx, zy);

            if (l!=0 && l < k)
                k = l;
            if (k == 0)
            {
                k = l;
            }
            return a[x, y]+k;
        }
        static void nm(out int n, out int m)
        {
            n = 0;
            m = 0;
            int i = 0;
            string s = Console.ReadLine();
            while (s[i] != ' ')
            {
                n = n * 10 +int.Parse(s[i].ToString());
                i++;
            }
            i++;
            while (i<s.Length)
            {
                m = m * 10 + int.Parse(s[i].ToString());
                i++;
            }
        }
        static void ucitaj(int[,] a,int n,int m)
        {
            string s;
            int x,p;
            for(int i = 0; i < n; i++)
            {
                s = Console.ReadLine();
                x = 0;
                for (int j = 0; j < m; j++)
                {
                    p = 0;
                    while ( x < s.Length && s[x]!=' ')
                    {
                        p = p * 10 +int.Parse(s[x].ToString());
                        x++;
                    }
                    a[i,j]=p;
                    x++;
                }
            }
        }
        static void Main(string[] args)
        {
            int n, m;
            nm(out n, out m);
            int[,] a=new int[n,m];   
            ucitaj(a,n,m);
            int x = int.Parse(Console.ReadLine());
            for (int i=0;i<x;i++)
            {
                int o, p;
                nm(out o, out p);
                Console.WriteLine(put(0,0,a,n,m,o,p));
            }
        }
    }
}