using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;

namespace takmicenjeKvalifikacije
{
    internal class Program
    {
        static int min(int x, int y)
        {
            if (x < y)
                return ((x < y) ? x : y);
            else
                return ((y < x) ? y : x);
        }
        static int minCost(int[,] cost, int m, int n)
        {
            if (n < 0 || m < 0)
                return int.MaxValue;
            else if (m == 0 && n == 0)
                return cost[m, n];
            else
                return cost[m, n]
                    + min(
                          minCost(cost, m - 1, n),
                          minCost(cost, m, n - 1));
        }
        static void Main(string[] args)
        {
            string[] sr = Console.ReadLine().Split();
            int n = int.Parse(sr[0]);
            int m = int.Parse(sr[1]);

            int[,] a = new int[n, m];
            for (int i = 0; i < n; i++)
            {
                string[] r = Console.ReadLine().Split();
                for (int j = 0; j < m; j++)
                {
                    a[i, j] = int.Parse(r[j]);
                }
            }

            int[,] min = new int[n, m];

            sr = Console.ReadLine().Split();
            int q = int.Parse(sr[0]);
            int[,] g = new int[q,q];
            for (int i = 0; i < q; i++)
            {
                
                sr = Console.ReadLine().Split();
                int x = int.Parse(sr[0]);
                int y = int.Parse(sr[1]);
                int pom = a[x, y];
                min[x, y] = int.MaxValue;
                Console.WriteLine(minCost(a, m, n));
            }

        }
    }
}