Task ordering P16249


Statement
 

pdf   zip

There are nn tasks to be done, but some tasks must be done before others. Given the names of the tasks and the direct dependencies among them, print the lexicographically smallest ordering of the tasks.

Input

Input consists of several cases. Each case begins with nn, followed by nn names of tasks. Follow the number of direct dependencies mm, followed by mm pairs of names xx yy, to indicate that xx must be done before yy. Assume 1n1041 \le n \le 10^4, 0m10n0 \le m \le 10n, that the names are made up of between one and six lowercase letters, that no name is a prefix of another name (in particular, this excludes equal names), that there are no repeated dependencies, and that there are no dependencies of the kind xx xx.

Output

For every case, print the alphabetically smaller string that can be produced by concatening a valid ordering of the tasks. If there is no valid ordering, tell so.

Public test cases
  • Input

    4  a sample this is
    3
    this is
    is sample
    is a
    
    3  and this too
    0
    
    2  a b
    2
    a b
    b a
    

    Output

    thisisasample
    andthistoo
    NO VALID ORDERING
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Official solutions
    C++
    User solutions
    C++