string[] s = Console.ReadLine().Split();
int N = int.Parse(s[0]);
int M = int.Parse(s[1]);
int[,] Doha = new int[N, M];
int[] niz = new int[N];
for (int i = 0; i< N; i++)
{
    for (int j = 0; j < M; j++)
    {
        Doha[i, j] = int.Parse(Console.ReadLine());
    }
}
int[,] Doha1 = (int[,])Doha.Clone();
Array.Fill(niz, Doha[0, 0]);
int[] niz1 = (int[])niz.Clone();
int Q = int.Parse(Console.ReadLine());
if (N<2||M<2||N*M>1000000||Q>5000000)
{
    Environment.Exit(0);
}
for (int o = 0; o<Q; o++)
{

    int x = int.Parse(Console.ReadLine());
    int y = int.Parse(Console.ReadLine());
    if (x == 1 || y == 1 || y==N-1||x==N-1||x>N||y>M)
    {
        Environment.Exit(0);
    }

    Doha[x-1, y-1] = 1000000001;
    for (int i = 1; i < N; i++)
    {
        niz[i] = niz[i - 1] + Doha[i, 0];
    }
    for (int j = 1; j < M; j++)
    {
        niz[0] += Doha[0, j];
        for (int i = 1; i < N; i++)
        {
            niz[i] = Math.Min(niz[i - 1], niz[i]) + Doha[i, j];
        }
    }

    Console.WriteLine(niz[N-1]);
    niz = (int[])niz1.Clone();
    Doha = (int[,])Doha1.Clone();
}