#include <iostream>
#include <algorithm>
using namespace std;

int main(){


    int n,m;
    cin>>n>>m;
    int mat1[n][m];
    for(int i=0;i<n;i++)
    {
      for(int j=0;j<m;j++)
      {
        cin>>mat1[i][j];
      }
    }
    int q;
    cin>>q;
  int niz[q];
    for(int k=0;k<q;k++){
    int x,y;
    cin>>x>>y;
    int mat[n][m];
    for(int i=0;i<n;i++)
    {
      for(int j=0;j<m;j++)
      {
        mat[i][j]=mat1[i][j];
      }
    }
    mat[x-1][y-1]=-1;

    int putanja[n][m];
    putanja[0][0] = mat[0][0];
      
    for (int i = 1; i < m; i++){
         if (mat[0][i] == -1 || putanja[0][i-1]==-1){
            putanja[0][i] = -1;
          }
      else{
        putanja[0][i] = putanja[0][i-1] + mat[0][i];
        }
    }
    for (int i = 1; i < n; i++){
      
      if (mat[i][0] == -1 || putanja[i-1][0]==-1){
          putanja[i][0] = -1;
      }
      else{
        putanja[i][0] = putanja[i-1][0] + mat[i][0];
      }
    }
      
    for (int i = 1; i < n; i++){
        for (int j = 1; j < m; j++){
          
          if(putanja[i-1][j]!=-1 && putanja[i][j-1]!=-1 && mat[i][j]!=-1){
            putanja[i][j] = min(putanja[i-1][j],putanja[i][j-1]) + mat[i][j];
            }
          
          else
          {
            if(putanja[i-1][j]==-1){putanja[i][j]=mat[i][j]+putanja[i][j-1];}
            if(putanja[i][j-1]==-1){putanja[i][j]=mat[i][j]+putanja[i-1][j];}
            if(mat[i][j]==-1){putanja[i][j]=-1;}
          }
        }
      }
      
      niz[k]=putanja[n-1][m-1];
    }

    for(int i=0;i<q;i++){cout<<niz[i]<<endl;}
    
  
    return 0;
  }