ADV04H - Join

no tags 

There are two tables. These tables are filled with numeric data. Each column of each table has a unique name. You need to implement JOIN operation for those tables. JOIN creates a new result table by combining column values of the two tables based upon the join-predicate. The query compares each row of the first table with each row of the second to find all pairs of rows which satisfy the join-predicate. When the join-predicate is satisfied, column values for each matched pair of rows of the table are combined into a result row. The result of the join can be defined as the outcome of first taking the Cartesian product of all records in the tables (combining every record in the first table with every record in the second table) - then return all the records which satisfy the join predicate. After that needed column are picked and printed as the result. Moreover the resulting rows should be output in the lexicographic order.

Input

The input starts with the description of the first table. The first line consists of k1 names of the columns of the table separated with spaces. All the names contain only small latin letters and digits. Next line contain number n1 – the amount of records in the table. In each of the next n1 lines there are k1 integers – data written in the table. The description of the second table in the same format follows. Next line is number q – the amount of queries. The description of each query follows in the following format:

SELECT <list of the names of columns>
JOIN ON <a name of the column from the first table>=<a name of the column of the second table>

Constraints

2 <= k1, k2 <= 10
1 <= n1, n2 <= 50000
All the integers in the tables are positive and less than 100000.
1 <= q <= 10

Output

For each query print the result of JOIN operation. You should print only the columns given in the query and in the appropriate order. The rows should be ordered lexicographically. The result of each query won't contain more than 50000 records. Print blank line after the answer for each query. See the example for more information.

Example

Input:
id1 a b
4
1 2 3
2 2 3
3 2 4
2 4 4
id2 c d
3
3 3 3
5 2 1
1 3 4
3
SELECT a,b,c
JOIN ON id1=d
SELECT id1,d
JOIN ON a=c
SELECT id2,id1,d
JOIN ON b=c

Output:
a b c
2 3 2
2 4 3

id1 d
1 1
2 1
3 1

id2 id1 d
1 1 4
1 2 4
3 1 3
3 2 3


hide comments

Added by:Spooky
Date:2010-11-14
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Advancement Autumn 2010, http://sevolymp.uuuq.com/