Diamond Inheritance

Process class declarations in order, accepting each only if the name is fresh, all parents exist, and no diamond forms.

Medium7GraphDFSHash mapNo attempts yetTime limit2sMemory limit512 MB

Problem

You are watching class declarations in an object oriented language similar to C++. Every declaration has the form K : P1 P2 ... Pk ;, where KK is the name of the new class and P1,P2,,PkP_1, P_2, \dots, P_k are the names of the classes that class KK inherits. For example, shape : ; declares a class shape that inherits no class, while square : shape rectangle ; declares a class square that inherits the classes shape and rectangle.

If class K1K_1 inherits K2K_2, class K2K_2 inherits K3K_3, and so on up to class Km1K_{m-1} that inherits KmK_m, then the classes K1,K2,,Km1K_1, K_2, \dots, K_{m-1} are all derived from class KmK_m. The rules of the language forbid circular definitions, so no class is derived from itself. In other words, the class hierarchy is a directed acyclic graph. A diamond in the hierarchy is forbidden too. A diamond is a set of four different classes AA, BB, XX, YY that satisfies all of the following.

  • Classes XX and YY are derived from class AA.
  • Class BB is derived from class XX and also from class YY.
  • Class XX is not derived from YY, and class YY is not derived from XX.

A diamond

Figure 1: A diamond

The hierarchy of the first example

Figure 2: The hierarchy after all declarations of the first example are processed

You are given nn declarations to process in the given order, and you decide for each one whether it is declared correctly. A correct declaration is added to the hierarchy, an incorrect one is discarded. The declaration K : P1 P2 ... Pk ; is correct when all of the following hold.

  1. Class KK has not been declared yet.
  2. Classes P1,P2,,PkP_1, P_2, \dots, P_k have all been declared earlier. Because of this condition no class can ever be derived from itself and no cycle can appear in the hierarchy.
  3. Adding class KK that inherits P1,P2,,PkP_1, P_2, \dots, P_k keeps the hierarchy in order, that is, not a single diamond is formed.

Write a program that processes the declarations in order as described above and decides the correctness of each one.

Input

The first line contains the integer nn, the number of declarations (1n10001 \le n \le 1\,000).

Each of the next nn lines contains a single declaration in the form K : P1 P2 ... Pk ;, where P1,P2,,PkP_1, P_2, \dots, P_k is a list of zero or more classes that class KK inherits. All names K,P1,P2,,PkK, P_1, P_2, \dots, P_k in a single declaration are different. Each class name is a string of at most 10 lowercase letters of the English alphabet. All elements of a declaration (the class names and the characters : and ;) are separated by exactly one space. In each declaration the number of inherited classes kk satisfies 0k10000 \le k \le 1\,000.

Output

Output nn lines. The iith line contains ok if the iith declaration is correct, and greska if it is not.

Notes

The first example

  • The fourth declaration is incorrect because class circle was already defined in the third line.
  • The sixth declaration is incorrect because class object has not been defined yet.
  • The eighth declaration is correct. Class object is declared by now, and the sixth declaration was discarded, so class runnable is still undefined.
  • The tenth declaration is incorrect. Adding it makes the classes shape, applet, square, runnable form a diamond.

The second example

The hierarchy of the second example

  • The last declaration is incorrect. Adding it makes the classes x, g, y, d form a diamond. Several other diamonds appear along with it.