Under construction

Still being built. Things may change

Technology Planning

Interview

Time limit5sMemory limit512 MB

Summary
Find every goal technology plus its prerequisites and print the smallest valid research order, breaking ties lexicographically.
Level

Medium5 of 10

Topics
Topological sort, Graph
Solved
No attempts yet

Problem

In a culture simulation game your culture develops technologies. Some technologies depend on others. If technology A depends on technology B, you can develop A only after B is developed. Your culture works on one technology at a time.

The game gives you goals that require particular technologies. Given the goal technologies, compute the smallest number of technologies you have to research and the order in which to research them.

Input

The first line holds the number of test cases, TT. TT test cases follow, each one shaped like this.

  • One line with an integer MM, the number of dependencies.
  • MM lines, each with two technology names separated by a colon (:). The first technology depends on the second one.
  • One line with an integer QQ, the number of goal technologies.
  • QQ lines, each naming one goal technology.

A technology name is a sequence of letters and digits. Names are case sensitive.

Limits

  • 1≤T≤251 \le T \le 25
  • 1≤M≤101 \le M \le 10
  • 1≤Q≤101 \le Q \le 10
  • The dependency graph has no cycles.
  • The same dependency or the same goal technology can appear more than once.
  • A goal technology can be absent from every dependency.

Output

For each test case, first print a line of the form Case #C: D, where CC is the test case number starting from 1 and DD is the smallest number of technologies that have to be researched. Then print DD lines, each holding one technology, in the order they are researched.

When several orders satisfy the rules, print only the lexicographically smallest one. To compare two orders, find the first position where the technology names differ and compare those names by Unicode code point, so digits come before uppercase letters and uppercase letters come before lowercase letters.

Examples2

  1. Example 1

    Input
    4
    9
    Metalworking:Fire
    Pottery:Fire
    Iron:Metalworking
    Steel:Iron
    Battleships:Steel
    Alchemy:Fire
    Chemistry:Alchemy
    Explosives:Chemistry
    Battleships:Explosives
    1
    Battleships
    9
    Metalworking:Fire
    Pottery:Fire
    Iron:Metalworking
    Steel:Iron
    Battleships:Steel
    Alchemy:Fire
    Chemistry:Alchemy
    Explosives:Chemistry
    Battleships:Explosives
    2
    Metalworking
    Pottery
    2
    b:a
    c:b
    1
    d
    1
    A:a
    1
    A
    
    Expected output
    Case #1: 8
    Fire
    Alchemy
    Chemistry
    Explosives
    Metalworking
    Iron
    Steel
    Battleships
    Case #2: 3
    Fire
    Metalworking
    Pottery
    Case #3: 1
    d
    Case #4: 2
    a
    A
    
  2. Example 2

    Input
    1
    4
    b:a
    c:b
    d:c
    e:d
    1
    e
    
    Expected output
    Case #1: 5
    a
    b
    c
    d
    e