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 Ruby (judge verdict: Accepted)

t = gets.chomp
q = gets.to_i
p = []
min = 10 # << optimization variable
q.times{|i|
  p[i] = gets.chomp
}


hash = Hash.new{|h,k| h[k] = {}}
0.upto(t.size-min){|i|
  hash[t[i,min]][i] = 1
  hash[t[i]][i] = 1
}
(t.size-min+1).upto(t.size){|i|
  hash[t[i]][i] = 1
}


checked = {}
p.size.times{|i|
  unless checked[p[i]]
    unless hash[p[i][0,min]] || hash[p[i][0]] then
      checked[p[i]] = 0
    else
      ans = 0
      if p[i].size >= min then
        hash[p[i][0,min]].each_key{|j|
          if t[j,p[i].size] == p[i] then
            ans = 1
            break
          end
        }
      else
        hash[p[i][0]].each_key{|j|
          if t[j,p[i].size] == p[i] then
            ans = 1
            break
          end
        }
      end
      checked[p[i]] = ans
    end
  end
  puts checked[p[i]]
}