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

namespace trecizadatakGuzva
{
    internal class Program
    {
        static void ispis(int[,] matrica, int n, int m)
        {
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= m; j++)
                {
                    Console.Write(matrica[i, j] + " ");
                }
                Console.WriteLine();
            }
        }
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split();
            int n = int.Parse(s[0]);
            int m = int.Parse(s[1]);
            int x;
            int y;
            int[,] matrica = new int[n + 1, m + 1];
            for (int i = 1; i <= n; i++)
            {
                s = Console.ReadLine().Split();
                for (int j = 1; j <= m; j++)
                {
                    matrica[i, j] = int.Parse(s[j - 1]);
                }
            }
            int q = int.Parse(Console.ReadLine());
            for (int p = 0; p < q; p++)
            {
                s = Console.ReadLine().Split();
                x = int.Parse(s[0]);
                y = int.Parse(s[1]); ;
                int z = 0;

                ispis(matrica, n, m);
                for (int i = 1; i <= n; i++)
                {
                    for (int j = 1; j <= m; j++)
                    {
                        if (matrica[x, y] == matrica[i, j] && i != n)
                        {
                            if (j <= m)
                            {
                                i += 1;

                            }
                            else if (!(j <= m))
                            {
                                i += 1;
                                j -= 1;
                            }
                        }
                        else if (matrica[x, y] != matrica[i, j])
                        {
                            z += matrica[i, j];
                        }
                        if (y == m)
                        {
                            if (matrica[x, y] == matrica[i, j] && i != n)
                            {
                                if (j <= m)
                                {
                                    i += 1;

                                }
                                else if (!(j <= m))
                                {
                                    i += 1;
                                    j -= 1;
                                }
                            }
                            else if (matrica[x, y] != matrica[i, j])
                            {
                                z += matrica[i, j];
                            }
                        }
                    }
                }
                Console.WriteLine(z);
            }
            Console.ReadLine();
        }
    }
}