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

namespace _3i3ii3i3i
{
    internal class Program
    {
        static void Main(string[] args)
        {
             string[] abel= Console.ReadLine().Split();
            int N = Convert.ToInt32(abel[0]);
            int M = Convert.ToInt32(abel[1]);

            int[,] matrica;
            matrica = new int[N, M];

            for (int i = 0; i < N; i++)
            {
                string[]a=Console.ReadLine().Split();

                for(int j = 0; j < M; j++)
                {
                    matrica[i,j] = Convert.ToInt32(a[j]);
                    
                }
                Console.Write("");
            }
            int T = Convert.ToInt32(Console.ReadLine());


            // DA SE NAPRAVI NIZ ZA UNOS RESENJA!!!
            int[] resenja;
            resenja = new int[T];

            int p = 0;
            for (int k = 0; k < T; k++)
            {
                string[]kord = Console.ReadLine().Split();
                int x = Convert.ToInt32(kord[0]);
                int y = Convert.ToInt32(kord[1]);
                int ppp = int.MaxValue;

                for (int i = 0; i < 2; i++)

                {
                    int mmmmm = 0;
                    int[] najmanji;
                    najmanji = new int[T];
                    
                    int kk = i;
                    
                    for(int kkkk=i;kkkk>0;kkkk--)
                    {
                        

                        if (i == x && kkkk == y)
                        {
                            mmmmm++;
                            break;
                        }
                        else
                        {


                            p = p + matrica[1, kkkk];
                            
                        }
                    }
                    for(int j = 0; j<M; j++)
                    {
                        if(mmmmm!=0)
                        {
                            break;
                        }
                        for(int s=M-i; s>0; s--)
                        {
                            kk = i;
                            if (i == x && k == y)
                            {

                                if (mmmmm != 0)
                                {
                                    break;
                                }
                            }
                            else
                            {
                                p = p + matrica[i, kk];
                                kk++;
                            }

                        }
                        if (T == 0)
                        {
                            if(mmmmm!=0)
                            {
                                break;
                            }
                            ppp = p;
                        }
                        else
                        {
                            if (ppp >= p)
                            {
                                ppp = p;
                            }
                        }
                        
                    }
                }



                resenja[T - 1] = ppp;
                p = 0;
            }
            for(int sim =0; sim<T;sim++)
            {
                Console.WriteLine(resenja[T-1]);
            }

            





            
        }
    }
}