using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO.Pipes;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Citaj(int[,] a, int n, int m)
        {
            int i, j;
            for (i = 0; i < n; i++)
            {
                string[] s1 = Console.ReadLine().Split(" ");
                for (j = 0; j < m; j++)
                {
                    a[i, j] = int.Parse(s1[j]);
                }
            }
        }
        static void Main(string[] args)
        {
            
            int pom = 0;
            string str = "";
            string[] dimenzije = Console.ReadLine().Split(' ');
            int n = int.Parse(dimenzije[0]);
            int m = int.Parse(dimenzije[1]);
            int[,] a = new int[n, m];
            Citaj(a, n, m);
            int q = int.Parse(Console.ReadLine());
            int[,] b = new int[q - 1, pom];
            int[,] blokiranaPolja = new int[q, 2];
            for (int k = 0; k < q; k++)
            {
                string[] koordinateBlokirano = Console.ReadLine().Split(' ');
                blokiranaPolja[k, 0] = int.Parse(koordinateBlokirano[0]);
                blokiranaPolja[k, 1] = int.Parse(koordinateBlokirano[1]);
                int suma = 0;
                int j = 0;
                int i = 0;
                if (blokiranaPolja[k , 1] == 1)
                {
                    while(j != m - 1)
                    {
                        suma += a[i, j];
                        j++;                       
                        if (j == m - 1)
                        {
                            while(i != n - 1)
                            {
                                suma += a[i, j];
                                i++;                              
                            }
                        }
                    }
                   
                }
                else
                {
                    while (i < n - 1)
                    {
                        suma += a[i, j];
                        i++;
                    }
                    while (j < m - 1)
                    {
                        suma += a[i, j];
                        j++;
                    }
                }
                 str += suma + "\n";
            }
            Console.WriteLine(str);
        }
    }
}