#include <bits/stdc++.h>

using namespace std;


int main()
{
    int n, m = 0;
    cin >> n >> m;
    int matrica[n + 1][m + 1];
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> matrica[i][j];
        }
    }

    int dp[n + 1][m + 1];
    int q = 0;
    cin >> q;
    int x;
    int y;

    for (int i = 0; i < q; i++)
    {
        cin >> x >> y;

        for (int j = 0; j <= n; j++)
        {
            for (int k = 0; k <= m; k++)
            {
                dp[j][k] = 0;
            }
        }

        dp[1][1] = matrica[1][1];

        for (int j = 2; j <= m; j++)
        {
            dp[1][j] = dp[1][j - 1] + matrica[1][j];
        }
        for (int j = 2; j <= n; j++)
        {
            dp[j][1] = dp[j - 1][1] + matrica[j][1];
        }


        for (int j = y; j <= m; j++)
        {
            dp[x][j] = INT_MAX;
        }

        for (int j = 2; j <= n; j++)
        {
            for (int k = 2; k <= m; k++)
            {
                if (j == x && k == y)
                {
                    dp[j][k] = INT_MAX;
                }
                else
                {
                    dp[j][k] = min(dp[j - 1][k], dp[j][k - 1]) + matrica[j][k];
                }
            }
         }

         cout << dp[n][m] << endl;
    }




    return 0;
}