New cults

It is year 5000 A.D. Against all odds, mankind has survived, multiplied,
and spread across the Solar System. New faiths, cults and alike coexist
with religions that were considered traditional thousands of years ago.
Among them, the cult to Masao, the legendary hero of the 21st century.
(For his believers, now it is year 3017 A.M.) The problem is, the cult
to Masao has so many factions and subfactions, that it is difficult to
keep track of which was originated from which.

Consider the following example, which is just a tiny simplification,
with two main trees of the cult to Masao:

$$\pstree[levelsep=40pt,treesep=20pt,arrows=->]{\Toval{\mbox{\normalsize Masaoism}}}{
  \Toval{\mbox{\normalsize Masochism}}
  \Toval{\mbox{\normalsize Garrulism}}
}
\hspace{2cm}
\pstree[levelsep=40pt,treesep=20pt,arrows=->]{\Toval{\mbox{\normalsize Longlongism}}}{
  \Toval{\mbox{\normalsize Charism}}
  \pstree{\Toval{\mbox{\normalsize Longism}}}
  {
    \Toval{\mbox{\normalsize Intism}}
  }
}$$

Among other relations, we can see that Garrulism was directly originated
from Masaoism, Intism was originated from Longlongism after two steps,
Charism and Longism belong to different branches of the same tree, and
Masochism and Intism belong to different trees.

Please write a program that, given the origin of the factions of the
cult to Masao, computes the relation among several pairs of factions.

Input

Input consists of several cases. Every case begins with a number m,
followed by m pairs of names n₁ and n₂, which indicate that n₂ was
directly originated from n₁. Next comes the number of queries of the
case q, followed by q pairs of names s₁ and s₂, with s₁ ≠ s₂. Assume
0 ≤ m ≤ 10⁵, that q is similar to m for the large private test cases,
that the relations correctly define one or more trees, and that no tree
has more than 10⁴ levels.

Output

For every query pair, print the relation between s₁ and s₂. Suppose that
a name not present in the description of the factions is an isolated
tree by itself. Print a line with 10 dashes after the output for every
case.

Problem information

Author: Salvador Roura

Generation: 2026-01-25T11:36:32.318Z

© Jutge.org, 2006–2026.
https://jutge.org
