Problem:
String Search

Determine whether a text T includes a pattern P. Your program should answer for given queries consisting of P_i.

Input

In the first line, a text T is given. In the second line, an integer Q denoting the number of queries is given. In the following Q lines, the patterns P_i are given respectively.

Output

For each question, print 1 if the text includes P_i, or print 0 otherwise.

Constraints

1 ≤ length of T ≤ 1000000

1 ≤ length of P_i ≤ 1000

1 ≤ Q ≤ 10000

The input consists of alphabetical characters and digits

Sample Input 1

aabaaa
4
aa
ba
bb
xyz

Sample Output 1

1
1
0
0

Solution in JavaScript (judge verdict: Runtime Error)

//config = { input: 'tmp', newline: '\r\n' }; // win
config = { input: '/dev/stdin', newline: '\n' }; // linux

line = require('fs').readFileSync(config.input, 'ascii')
  .split(config.newline);
T = line[0];
Q = Number(line[1]);
P = line.slice(2, 2+Q);
line = null; // dispose

function rabinKarpMatcher(T, P, d, q) {
  var T, n, P, m, h, p, t, i, s;
  n = T.length;
  T = "_" + T; // start0 -> start1
  m = P.length;
  P = "_" + P; // start0 -> start1
  h = Math.pow(d, m-1) % q;
  p = 0;
  t = {};
  t[0] = 0;
  for (i = 1; i <= m; i++) { // preprocess
    p = (d * p + P[i].charCodeAt()) % q;
    t[0] = (d * t[0] + T[i].charCodeAt()) % q;
  }
  for (s = 0; s <= n - m; s++) { // matching
    if (p == t[s])
      if (P.slice(1, m+1) === T.slice(s+1, s+m+1))
        //console.log("pattern match at shift " + s);
        return 1;
    if (s < n - m)
      t[s+1] =
        (d * (t[s] - T[s+1].charCodeAt() * h) + T[s+m+1].charCodeAt()) % q;
  }
  return 0;
}


for (i in P) console.log(rabinKarpMatcher(T, P[i], 256, Math.pow(2, 31) - 1));