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

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[] k = new int[n - 1];
                int[] ij = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
                int blokirano = doha[ij[0] - 1, ij[1] - 1];
                doha[ij[0] - 1, ij[1] - 1] = -1;
                guzve[br] = long.MaxValue;
                while (true)
                {
                    long suma = 0;
                    int i = 0, j = 0;
                    for (int nz = 0; nz < n + m - 1; nz++)
                    {
                        if (doha[i, j] < 0)
                        {
                            suma = long.MaxValue;
                            break;
                        }
                        suma += doha[i, j];
                        if (i == n - 1) j++;
                        else if (j < k[i]) j++;
                        else i++;
                    }
                    guzve[br] = Math.Min(guzve[br], suma);
                    int indeks = -1;
                    for (int nz = k.Length - 1; nz >= 0; nz--)
                    {
                        if (k[nz] != m - 1)
                        {
                            indeks = nz;
                            k[nz]++;
                            break;
                        }
                    }
                    if (indeks == -1) break;
                    else for (int nz = indeks + 1; nz < k.Length; nz++)
                        {
                            k[nz] = k[indeks];
                        }
                }
                doha[ij[0] - 1, ij[1] - 1] = blokirano;
            }
            for (int i = 0; i < q; i++)
            {
                Console.WriteLine(guzve[i]);
            }
        }
    }
}