This page is still under construction.

Parts of this page are still being built. What you see may change.

Fiber Network

Time limit1sMemory limit128 MB

Summary
For each query in a directed multigraph where every edge is labeled with a set of companies, list the companies that have a path from A to B using only their own edges.
Level

Medium6 of 10

Topics
Graph, BFS, DFS, Bit manipulation
Solved
No attempts yet

Problem

Several startup companies have decided to build a better Internet called the "FiberNet". They have already installed many nodes that act as routers all around the world. Unfortunately, they started to quarrel about the connecting lines, and ended up with every company laying its own set of cables between some of the nodes.

Now, service providers who want to send data from node AA to node BB are curious which company is able to provide the necessary connections. Help the providers by answering their queries.

Input

The input contains several test cases. Each test case starts with the number of nodes of the network nn. The input is terminated by n=0n = 0. Otherwise 1≤n≤2001 \le n \le 200, and the nodes are numbered 1,…,n1, \dots, n.

Then follows a list of connections. Every connection starts with two numbers AA, BB. The list of connections is terminated by A=B=0A = B = 0. Otherwise 1≤A,B≤n1 \le A, B \le n, and they denote the start and the endpoint of a one-directional (directed) connection, respectively. For every connection, the two nodes are followed by the companies that have a connection from node AA to node BB. A company is identified by a lower-case letter, and the set of companies having such a connection is given as a single word composed of lower-case letters.

After the list of connections, each test case is completed by a list of queries. Each query consists of two numbers AA, BB. The list (and with it the test case) is terminated by A=B=0A = B = 0. Otherwise 1≤A,B≤n1 \le A, B \le n, and they denote the start and the endpoint of the query. You may assume that no connection and no query has identical start and end nodes.

Output

For each query in every test case, output a single line containing the identifiers of all companies that can route data packets from the start node to the end node of the query using only their own connections. Print the identifiers in ascending alphabetical order with no separators. If there are no such companies, output - instead. Print a blank line after each test case.

Examples1

  1. Example 1

    Input
    3
    1 2 abc
    2 3 ad
    1 3 b
    3 1 de
    0 0
    1 3
    2 1
    3 2
    0 0
    2
    1 2 z
    0 0
    1 2
    2 1
    0 0
    0
    
    Expected output
    ab
    d
    -
    
    z
    -