PreQueL
In a simplified database management system called PreQueL, names are restricted to a single character of type CHAR(1), and must be uppercase English letters (from A to Z). Each table can have up to 9 columns, numbered from 1 to 9. Tables themselves are named using lowercase English letters (from a to z).
The only query you can perform on this database involves first joining all tables (see the explanation at the end of this statement) and then selecting rows based on specific conditions. There are two types of conditions: <column> = <value> or <column1> = <column2>, such as a2 = A or b1 = c4. All conditions must be satisfied simultaneously, as if they are combined using the AND operator.
Your task is to create a query processor for PreQueL that, given the tables and a set of conditions, outputs the rows that meet all the conditions. The output rows should be sorted lexicographically.
Input
The first line contains two integers: the number of tables t (1 ≤ t ≤ 26) and the number of conditions d (1 ≤ d ≤ 50).
Starting from the second line, t tables are described. The first line of each table description provides the table's dimensions - r_n and c_n (1 ≤ c_i ≤ 9, 1 ≤ r_i ≤ 1000). This is followed by r_n rows, each containing exactly c_n characters. After the table descriptions, there are d lines specifying the conditions.
Output
Output the resulting rows, with each row printed on a separate line. It is guaranteed that no query will produce more than 1000 rows.
Examples
Note
The output rows should be sorted in ascending order.
When joining tables A and B without any conditions, the resulting number of rows will be |A|·|B|.
For example, if there are three tables (each represented as a list of rows): {A, B}, {C, D}, {XX, YY, ZZ}, their join will result in a table with 12 rows: {ACXX, ACYY, ACZZ, ADXX, ADYY, ADZZ, BCXX, BCYY, BCZZ, BDXX, BDYY, BDZZ}.