N-Step Syllogism

Each premise says all a are b; for each conclusion x is y, decide whether following the implication chain from x reaches y.

Medium4GraphDFSHash mapImplementationInterviewNo attempts yetTime limit2sMemory limit128 MB

Problem

All humans are mortal.

Socrates is a human.

Therefore Socrates is mortal.

The argument above is the standard example of a syllogism. A syllogism is a deductive argument made of two premises and one conclusion. The same reasoning carries over when there are more than two premises.

Every premise has the form a is b and means "all a are b". Given nn premises, decide for each of mm conclusions whether it is true. A conclusion x is y is true when chaining the premises leads from x to y. For example, if a is b and b is c are premises, then a is c is true.

Input

The first line contains an integer nn (2n262 \le n \le 26).

Each of the next nn lines contains one premise. Every premise has the form a is b, where a and b are two different lowercase letters. The premise a is b means "all a are b" and never means "all b are a". No letter is the subject of two premises, so a cannot be b and c at the same time. Two different letters a and b can both be c.

Line n+2n + 2 contains an integer mm (1m101 \le m \le 10). Each of the next mm lines contains one conclusion in the same format as a premise.

Output

Print mm lines. For each conclusion print T if it is true and F if it is false. A conclusion that cannot be decided counts as false. Print the answers in uppercase.