#P7369. Elder Wand Loyalty

    ID: 20567 Type: Default 1000ms 256MiB

Elder Wand Loyalty

Elder Wand Loyalty

After watching all eight Harry Potter movies in a week, Nikola was amazed by the power of the Elder Wand. It is known that if the Elder Wand is currently loyal to wizard \(A\), and wizard \(A\) is defeated by wizard \(B\), then the wand shifts its loyalty to wizard \(B\).

Given the initial loyal wizard and the results of \(N\) duels, you are to determine:

  1. The wizard to whom the Elder Wand is loyal after all \(N\) duels.
  2. The number of distinct wizards to whom the wand has ever been loyal.

For each duel, two names are given: the first is the wizard who loses the duel and the second is the wizard who wins. The wand changes its loyalty only if the defeated wizard is currently the wand's master.

Note: All formulas are given in \(\LaTeX\) format. For example, the number of duels is represented as \(N\).

inputFormat

The input consists of multiple lines:

  • The first line contains a string denoting the initial wizard to whom the Elder Wand is loyal.
  • The second line contains an integer \(N\) representing the number of duels.
  • Each of the following \(N\) lines contains two strings separated by a space. The first string is the wizard who loses the duel, and the second string is the wizard who wins the duel.

outputFormat

Output two lines:

  • The first line should contain the name of the wizard to whom the Elder Wand is loyal after all duels.
  • The second line should contain an integer representing the number of distinct wizards the wand has ever been loyal to.

sample

Harry
3
Harry Ron
Ron Draco
Lucius Harry
Draco

3

</p>