Write a logical inference program.
We assume that in this logical inference system there are at most 26 events, and each event is represented by an uppercase letter.
We also assume that if one event is true, then several other events will also become true.
If an event triggers three events, we write it as:
This means that if A is true, then B, C, and D are also true.
That is, A triggers B, C, and D.
Given the logical triggering relationships among the events, we want to determine which events will be triggered when a certain event occurs.
0 < n ≤ 26
0 ≤ m ≤ 100
The input consists of two parts.
Part 1: Trigger relationship
The first line contains an integer n, representing the number of logical trigger relations.
Each of the next n lines has the following format:
First, an event (the triggering event),
Followed by the events it triggers, separated by spaces.
For example, the following means that if A is true, then B, C, and D will become true;
Additionally, each event can trigger at most three events.
An event cannot trigger itself, and no cyclic triggers exist.
Each event appears at most once on the left-hand side.
Part 2: Queries
The next input section contains queries.
The first line is an integer m, representing the number of queries.
Each of the next m lines contains two events.
Ex. 2
A 3 B C D
C 2 Z F
2
A Z
Z A
For each query, if the first event can trigger the second event (directly or indirectly), print "yes"; otherwise print "no".
Ex. yes
no