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

namespace _3.zadatak
{
    internal class Program
    {
        static void Main()
        {
            string[] s = Console.ReadLine().Split();
            int n = int.Parse(s[0]), m = int.Parse(s[1]);
            int[,] doha = new int[n, m];
            for (int i = 0; i < n; i++)
            {
                s = Console.ReadLine().Split();
                for (int j = 0; j < m; j++)
                {
                    doha[i, j] = int.Parse(s[j]);
                }
            }
            int q = int.Parse(Console.ReadLine());
            long[] guzve = new long[q];
            for (int br = 0; br < q; br++)
            {
                int[] ij = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
                long[,] matrica = new long[n, m];
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < m; j++)
                    {
                        matrica[i, j] = doha[i, j];
                    }
                }
                matrica[ij[0] - 1, ij[1] - 1] = long.MaxValue;
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < m; j++)
                    {
                        if (i != 0 && matrica[i, j] != long.MaxValue) 
                        {
                            if (j != 0) matrica[i, j] += Math.Min(matrica[i - 1, j], matrica[i, j - 1]);
                            else if (matrica[i - 1, j] != long.MaxValue) matrica[i, j] += matrica[i - 1, j];
                            else matrica[i, j] = long.MaxValue;
                        }
                        else if (j != 0 && matrica[i, j] != long.MaxValue)
                        {
                            if (matrica[i, j - 1] != long.MaxValue) matrica[i, j] += matrica[i, j - 1];
                            else matrica[i, j] = long.MaxValue;
                        }
                    }
                }
                guzve[br] = matrica[n - 1, m - 1];
            }
            for (int i = 0; i < q; i++)
            {
                Console.WriteLine(guzve[i]);
            }
        }
    }
}