Problem G: Water clock

An unusually shaped water clock was discovered from the bottom of Lake Biwa.

The water clock is placed on a wide horizontal plane and consists of a cubic aquarium with one or more sides of 30 cm. The aquariums are placed on pedestals of various heights arranged in a grid pattern. The thickness of the aquarium is negligible. When water tanks placed on a table of the same height are adjacent to each other in the front, back, left and right, holes are made between the water tanks to keep the water heights equal. (Figure 1)

<image>
---
Figure 1

According to the literature found together, water was continuously poured into one aquarium to check the water level in another particular aquarium.

When water is poured into an aquarium as shown in Fig. 2 and the water overflows, the overflowed water flows in the same amount in the direction adjacent to the aquarium. If water flows to a place where there is no water tank, it will be automatically drained to the outside of the water clock. Moreover, even if there is no direction in which water overflows, more water than the capacity of the water tank does not stay in the grid. In such a case, the amount of water that exceeds the capacity of the aquarium disappears with a mysterious force. Note that if the adjacent aquarium is placed on a platform that is even slightly higher than the original aquarium, water will not flow in that direction. For example, in the case of Fig. 3, 1/4 of the overflowing amount of water flows from the central tank to the left and right tanks and the back tank.

<image> | <image>
--- | ---
Figure 2 | Figure 3

Your job is to write a program that simulates this water clock.

Input

The input consists of multiple datasets. The number of data sets is 300 or less, and each has the following format.


w h
fx fy fq
p0,0 p0,1 ... p0, w-1
p1,0 p1,1 ... p1, w-1
...
ph-1,0 ph-1,1 ... ph-1, w-1
l
t1 x1 y1
...
tl xl yl


w and h are positive integers representing the number of columns and rows in the grid, respectively, and satisfy 1 ≤ w ≤ 10 and 1 ≤ h ≤ 10, respectively. fx, fy, fq represent the location (fx, fy) and flow rate (fq) of the grid where the water tank that flows water is placed. The unit of flow rate is cm ^ 3. These satisfy 0 ≤ fx <w, 0 ≤ fy <h, 1 ≤ fq ≤ 30000. The following h lines are each composed of w numbers separated by spaces, and represent the height of the table on which the aquarium is placed in the grid {i, j}. The unit is cm, which is an integer that satisfies 0 ≤ pi, j ≤ 1000. When pi, j = 0, there is no water tank in that place. The next line consists of a positive integer l that represents the number of measurements of the height of the water in the aquarium. This satisfies 1 ≤ l ≤ 10. The following l line contains three space-separated integer values, tk, xk, and yk. tk represents the measurement time, and xk and yk indicate the grid of the measurement location. It can be assumed that xk and yk always indicate a grid with an aquarium. It also satisfies 0 ≤ t ≤ 10000.

The end of the input is represented by a line containing two zeros.

Output

Output l integers for each dataset. The integer outputs the height of the water in the aquarium on the grid xk, yk at the specified time tk. The height of water after the decimal point is rounded down.

Sample Input


3 3
1 1 900
0 10 0
10 45 10
10 0 0
3
5 1 1
50 1 0
100 0 1
5 5
2 0 9000
0 0 4 0 0
0 3 3 3 0
2 2 2 2 2
0 0 1 0 0
0 0 1 0 0
Four
5 2 0
10 2 1
100 2 2
250 2 3
0 0



Output for Sample Input


Five
Five
8
30
Five
13
Four






Example

Input

3 3
1 1 900
0 10 0
10 45 10
10 0 0
3
5 1 1
50 1 0
100 0 1
5 5
2 0 9000
0 0 4 0 0
0 3 3 3 0
2 2 2 2 2
0 0 1 0 0
0 0 1 0 0
4
5 2 0
10 2 1
100 2 2
250 2 3
0 0


Output

5
5
8
30
5
13
4
#include<iostream>
#include<vector>
#include<cstdio>
#include<cmath>

#define f first
#define s second

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define IN(x,s,g) ((x) >= (s) && (x) < (g))
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))
#define print(x) printf("%d\n",x)

using namespace std;

const int _dx[] = {0,1,0,-1};
const int _dy[] = {-1,0,1,0};

int getInt(){
  int a; scanf("%d", &a); return a;
}

int p[10][10];
int id[10][10];
int w, h;
int cnt[100];
int all[100];
int g[100][100];
int idcnt;
double cap[100];

void dfs(int y, int x, int ids){
  id[y][x] = ids;

  REP(i,4){
    int yy = y + _dy[i];
    int xx = x + _dx[i];

    if(ISIN(xx, yy, w, h) && id[yy][xx] == -1 && p[yy][xx] == p[y][x]){
      dfs(yy, xx, ids);
    }
  }
}

const double eps = 1e-10;
void doflow(int i, double f){
  if(f < eps) return;
  double rest = (30.0 - cap[i]) * (cnt[i] * 30.0 * 30.0);

  if(rest < f){
    cap[i] = 30.0;
    f -= rest;
  }else{
    cap[i] += f / (cnt[i] * 30.0 * 30.0);
    f = 0.0;
  }

  if(f > eps){
    REP(j,idcnt) if(g[i][j] > 0){
      doflow(j, f * g[i][j] / all[i]);
    }
  }
}

int main(){
  while(true){
    w = getInt();
    h = getInt();

    if(w + h == 0) break;

    int fx = getInt();
    int fy = getInt();
    int fp = getInt();

    REP(i,h) REP(j,w){
      p[i][j]  = getInt();
      id[i][j] = -1;
    }

    int l = getInt();

    vector<pair<int, pair<int, int> > > q(l);

    REP(i,l){
      q[i].f   = getInt();
      q[i].s.f = getInt();
      q[i].s.s = getInt();
    }

    // ID
    idcnt = 0;
    REP(i,h) REP(j,w) if(id[i][j] == -1 && p[i][j] != 0){
      dfs(i,j,idcnt++);
    }

    REP(i, idcnt){
      cnt[i] = 0;
      all[i] = 0;
      REP(j, idcnt){
        g[i][j] = 0;
      }
    }

    REP(y,h) REP(x,w) if(id[y][x] != -1){
      cnt[id[y][x]]++;

      REP(k,4){
        int yy = y + _dy[k];
        int xx = x + _dx[k];

        if(ISIN(xx, yy, w, h) && id[yy][xx] == id[y][x]){
        }else{
          if(ISIN(xx, yy, w, h) && id[yy][xx] != -1){
            if(p[y][x] > p[yy][xx]){
              all[id[y][x]]++;
              g[id[y][x]][id[yy][xx]]++;
            }else{
            }
          }else{
            all[id[y][x]]++;
          }
        }
      }
    }

    int sid  = id[fy][fx];
    REP(i,l){
      int t = q[i].f;
      int x = q[i].s.f;
      int y = q[i].s.s;

      REP(i,idcnt) cap[i] = 0.0;
      doflow(sid, fp * t);

      printf("%d\n", (int)floor(cap[id[y][x]] + eps));
    }
  }

  return 0;
}