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

namespace C
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split(' ');
            long n= long.Parse(s[0]),m=long.Parse(s[1]);
            long[,] a = new long[n, m];
            for(int i = 0; i < n; i++)
            {
                s= Console.ReadLine().Split(' ');
                for(int j = 0; j < m; j++)
                {
                    a[i,j] = long.Parse(s[j]);
                }
            }
            int q = int.Parse(Console.ReadLine());
            long[] r = new long[q];
            int z = 0;
            for(int k= 0; k < q; k++)
            {
                s = Console.ReadLine().Split(' ');
                int i = 0, j = 0;
                long bi = long.Parse(s[0])-1, bj = long.Parse(s[1])-1, br = a[0,0];
                while (i != n - 1 || j != m - 1)
                {
                    if (j == m - 1)
                    {
                        if (bi == i + 1 && bj == j)
                        {
                            i += 2;
                            br = br + a[i, j] + a[i, j - 1] + a[i - 1, j - 1] - a[i-2,j];
                        }
                        else
                        {
                            i++;
                            br += a[i, j];
                        }
                    }
                    else if (i == bi && j + 1 == bj)
                    {
                        i++;
                        br += a[i, j];
                    }
                    else
                    {
                        j++;
                        br += a[i, j];
                    }
                }
                r[z] = br;
                z++;
            }
            for(int i = 0; i < z; i++)
            {
                Console.WriteLine(r[i]);
            }
        }
    }
}