Tree of Pain

No attempts yetTime limit1sMemory limit256 MB

Problem

A consulting firm keeps its staff in an organization tree. One employee is one node, the head of the firm is the root, and every node carries a job title as its label.

A management journal printed a list of patterns that a study calls dangerous. One example is an engineer with an economist somewhere above him. A pattern is itself a rooted tree whose labels are job titles.

A pattern PP occurs in an organization tree TT when some map ff from the nodes of PP to the nodes of TT satisfies all of the following.

  • ff is injective, so f(u)=f(v)f(u) = f(v) implies u=vu = v.
  • ff keeps labels, so the label of uu equals the label of f(u)f(u) for every uu.
  • ff keeps ancestry in both directions, so uu is a proper ancestor of vv if and only if f(u)f(u) is a proper ancestor of f(v)f(v).

One edge of the pattern may stretch into a long path of the organization tree. Two pattern nodes with no ancestor relation between them must map to two nodes where neither is an ancestor of the other.

You are given the organization tree and a list of patterns. Decide for each pattern whether it occurs in the organization tree.

Input

The first line holds the organization tree TT as one string with no spaces. The number of nodes nn satisfies 1n100001 \le n \le 10000. A node is written as its label, followed, when it has children, by its child list in parentheses with the children separated by commas. A label is a string of 1 to 10 lowercase letters.

The second line holds the number of patterns qq, with 1q1001 \le q \le 100.

Each of the next qq lines holds one pattern tree in the same format. The number of nodes mm of a pattern satisfies 1m161 \le m \le 16.

Output

Print one line per pattern. Print disaster if the pattern occurs in the organization tree, and great success otherwise.