Wilbur and Strings

Wilbur the pig now wants to play with strings. He has found an n by m table consisting only of the digits from 0 to 9 where the rows are numbered 1 to n and the columns are numbered 1 to m. Wilbur starts at some square and makes certain moves. If he is at square ( xy) and the digit d (0 ≤ d ≤ 9) is written at position ( xy), then he must move to the square ( x + a dy + b d), if that square lies within the table, and he stays in the square ( xy) otherwise. Before Wilbur makes a move, he can choose whether or not to write the digit written in this square on the white board. All digits written on the whiteboard form some string. Every time a new digit is written, it goes to the end of the current string.

Wilbur has q strings that he is worried about. For each string s i, Wilbur wants to know whether there exists a starting position ( xy) so that by making finitely many moves, Wilbur can end up with the string s i written on the white board.

Input

The first line of the input consists of three integers nm, and q (1 ≤ n, m, q ≤ 200) — the dimensions of the table and the number of strings to process, respectively.

Each of the next n lines contains m digits from 0 and 9 giving the table itself.

Then follow 10 lines. The i-th of them contains the values a i - 1 and b i - 1 ( - 200 ≤ a i, b i ≤ 200), i.e. the vector that Wilbur uses to make a move from the square with a digit i - 1 in it.

There are q lines that follow. The i-th of them will contain a string s i consisting only of digits from 0 to 9. It is guaranteed that the total length of these q strings won’t exceed 1 000 000.

Output

For each of the q strings, print “YES” if Wilbur can choose x and y in order to finish with this string after some finite number of moves. If it’s impossible, than print “NO” for the corresponding string.

Examples

input

1 1 2
0
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
0000000000000
2413423432432

output

YES
NO

input

4 2 5
01
23
45
67
0 1
0 -1
0 1
0 -1
0 1
0 -1
0 1
0 -1
0 1
0 -1
0000000000
010101011101
32232232322
44343222342444324
6767

output

YES
YES
YES
NO
YES

Note

In the first sample, there is a 1 by 1 table consisting of the only digit 0. The only move that can be made is staying on the square. The first string can be written on the white board by writing 0 repeatedly. The second string cannot be written as there is no 2 on the table.

Solution:

#include <bits/stdc++.h>

using namespace std;

const int N = 1234567;

vector <int> rev[N];
char foo[N];
int on_cycle[N];
int digit[N], to[N];

bool found;

void dfs(int v, int pos) {
if (pos < 0) {
    found = true;
  }
  if (found) {
    return;
  }
  int sz = rev[v].size();
  for (int j = 0; j < sz; j++) {
    int u = rev[v][j];
    if (on_cycle[u] != -1) {
      continue;
    }
    dfs(u, pos - (digit[u] == foo[pos] - '0'));
  }
}

int dx[42], dy[42];
int last[42];
bool vis[N];

const int NNN = 444;

char s[NNN][NNN];

int main() {
  int n, m, q;
  scanf("%d %d %d", &n, &m, &q);
  for (int i = 0; i < n; i++) {
    scanf("%s", s[i]);
  }
  for (int d = 0; d <= 9; d++) {
    scanf("%d %d", dx + d, dy + d);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int d = s[i][j] - '0';
      int x = i + dx[d];
      int y = j + dy[d];
      if (x < 0 || y < 0 || x >= n || y >= m) {
x = i;
y = j;
}
digit[i * m + j] = d;
to[i * m + j] = x * m + y;
}
}
n = n * m;
for (int i = 0; i < n; i++) {
    vis[i] = false;
    rev[i].clear();
    on_cycle[i] = -1;
  }
  for (int i = 0; i < n; i++) {
    rev[to[i]].push_back(i);
  }
  for (int i = 0; i < n; i++) {
    if (vis[i]) {
      continue;
    }
    int p = i;
    vector <int> cycle;
while (!vis[p]) {
cycle.push_back(p);
vis[p] = true;
p = to[p];
}
int id = -1;
for (int i = 0; i < (int)cycle.size(); i++) {
      if (cycle[i] == p) {
        id = i;
        break;
      }
    }
    if (id == -1) {
      continue;
    }
    int mask = 0;
    for (int i = id; i < (int)cycle.size(); i++) {
      mask |= (1 << digit[cycle[i]]);
    }
    for (int i = id; i < (int)cycle.size(); i++) {
      on_cycle[cycle[i]] = mask;
    }
  }
  while (q--) {
    scanf("%s", foo);
    int len = strlen(foo);
    for (int d = 0; d <= 9; d++) {
      last[d] = -1;
    }
    for (int i = 0; i < len; i++) {
      last[foo[i] - '0'] = i;
    }
    found = false;
    for (int start = 0; start < n; start++) {
      if (on_cycle[start] == -1) {
        continue;
      }
      int pos = -1;
      for (int d = 0; d <= 9; d++) {
        if (on_cycle[start] & (1 << d)) {
          continue;
        }
        pos = max(pos, last[d]);
      }
      dfs(start, pos);
    }
    puts(found ? "YES" : "NO");
  }
  return 0;
}