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

namespace Guzva
{
    class Program
    {
        static int BekTrek(int i, int j, int p, int q, int [,] matrica)
        {
            if(i == matrica.GetLength(0) && j == matrica.GetLength(1))
            {
                return matrica[i,j];
            }
            else
            {
                if(i + 1 == p)
                {
                    return matrica[i, j] += BekTrek(i, j + 1, p, q, matrica);
                }
                else if (j + 1 == q)
                {
                    return matrica[i, j] += BekTrek(i + 1, j, p, q, matrica);
                }
                else
                {
                    return matrica[i, j] += Math.Min(BekTrek(i + 1, j, p, q, matrica), BekTrek(i, j + 1, p, q, matrica));
                }
            }
        }
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split();
            int n = Convert.ToInt32(s[0]);
            int m = Convert.ToInt32(s[1]);
            int[,] matrica = new int[n, m];
            for(int i = 0; i < n; i++)
            {
                s = Console.ReadLine().Split();
                for (int j = 0; j < m; j++)
                {
                    matrica[i, j] = Convert.ToInt32(s[j]);
                }
            }
            int q = Convert.ToInt32(Console.ReadLine());
            int[,] slucajevi = new int[q, 2];
            for(int i = 0; i < q; i++)
            {
                s = Console.ReadLine().Split();
                slucajevi[i, 0] = Convert.ToInt32(s[0]);
                slucajevi[i, 1] = Convert.ToInt32(s[1]);
            }
            for(int i = 0; i < q; i++)
            {
                Console.WriteLine(BekTrek(0, 0, slucajevi[i, 0], slucajevi[i, 1], matrica));
            }
        }
    }
}