Alchemy X26349


Statement
 

pdf   zip

html

Without Knowledge, Skill cannot be focused, without Skill, Strength cannot be brought to bear and without Strength, Knowledge may not be applied. - Alexandre the Great.

Alexander the Great, king of Macedonia, was a king of success leading his army and conquering every piece of land he trod. But in every story there is some hidden information. Alexander had in his troops a council of alchemists that helped him to create medicines from basic elements and transmute base metals into nobel metals like gold so they can continuosly paying the army fees and repairing shields and weapons.

The old alchemy documents show the process to transmute some basic elements into others, so your work as an historian is to recover this information and report for different transmutations, the required basic elements to finish the process.

Input

The input starts with the number of test cases T ≤ 100. For each test case, there is an intenger N ≤ 1000 that represents the number of elements in the document. The next N lines, show the requirements for the nth transmutation, and they are described with the number of required elements E ≤ 10, following E elements indexes. If an elements has E == 0 it means it is a basic element. You may assume that there are no cyclic dependencies between elements.

Output

For each test case, output N lines with the required basic elements for the nth transmutation. Leave an empty line after each test case.

In the first example, 0 and 1 are basic elements so the only requirement is themselves. But element 2 requires the elements 0 and 1 for the transmutation.

In the second example, elements 0, 1, 3, 4, 5 and 7 are basic; element 2 only requires the two basic 0 and 1; element 6 requires the basic elements 3, 4 and 5; element 8 only requires one basic that is 7; and element 9 requires three complex elements 2, 6 and 8 that are composed of the basic elements 0, 1, 3, 4, 5 and 7.

Public test cases
  • Input

    2
    3
    0
    0
    2 0 1
    10
    0
    0
    2 0 1
    0
    0
    0
    3 3 4 5
    0
    1 7
    3 2 6 8
    

    Output

    0
    1
    0 1
    
    0
    1
    0 1
    3
    4
    5
    3 4 5
    7
    7
    0 1 3 4 5 7
    
    
  • Information
    Author
    Javier Segovia
    Language
    English
    Official solutions
    C++
    User solutions
    C++