New cults P70868


Statement
 

pdf   zip

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 mm, followed by mm pairs of names n1n_1 and n2n_2, which indicate that n2n_2 was directly originated from n1n_1. Next comes the number of queries of the case qq, followed by qq pairs of names s1s_1 and s2s_2, with s1s2s_1 \ne s_2. Assume 0m1050 \le m \le 10^5, that qq is similar to mm for the large private test cases, that the relations correctly define one or more trees, and that no tree has more than 10410^4 levels.

Output

For every query pair, print the relation between s1s_1 and s2s_2. 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.

Public test cases
  • Input

    5
    Longism Intism
    Masaoism Garrulism
    Longlongism Charism
    Masaoism Masochism
    Longlongism Longism
    4
    Masaoism Garrulism
    Intism Longlongism
    Charism Longism
    Masochism Intism
    
    0
    1
    z y
    

    Output

    Masaoism is 1 level(s) above Garrulism
    Longlongism is 2 level(s) above Intism
    Charism and Longism : same tree, different branches
    Masochism and Intism : different trees
    ----------
    z and y : different trees
    ----------
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Official solutions
    C++
    User solutions
    C++