using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Zadatak3
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split();
            int n = int.Parse(s[0]); int m = int.Parse(s[1]);
            int[,] a = new int[n, m];

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

            int q = int.Parse(Console.ReadLine());
            int[] x = new int[q]; int[] y = new int[q];

            for (int i = 0; i < q; i++)
            {
                s = Console.ReadLine().Split();
                x[i] = int.Parse(s[0]); y[i] = int.Parse(s[1]);
            }

            int suma = 0;

            for (int k = 0; k < q; k++)
            {
                bool ind = true;
                int i = 0, j = 0; suma = 0;

                while (i < n && j < m)
                {
                    if (j == m - 1)
                        ind = true;
                    else if (i == n - 1)
                        ind = false;
                    else if (i > x[k] - 1 && a[i, j + 1] < a[i + 1, j])
                        ind = false;
                    else if (i + 1 == x[k] - 1 && x[k] - 1 == n - 1)
                        ind = false;
                    else if (j + 1 == y[k] - 1 || a[i, j + 1] > a[i + 1, j])
                        ind = true;
                    else if (a[i, j + 1] < a[i + 1, j])
                        ind = false;

                    if (ind)
                    {
                        suma += a[i, j];
                        i++;
                    }
                    else
                    {
                        suma += a[i, j];
                        j++;
                    }
                }
                Console.WriteLine(suma);
            }
        }
    }
}