Alice found an old monolith in a forest. The sentence carved on it is written in an old language. A sentence is made of glyphs and of the rectangles that surround them, and some glyphs in a sentence are mirrored horizontally.
Alice transliterates a sentence with ASCII letters. Each glyph of the dictionary gets one lowercase letter, and a rectangle is written as [ and ]. When a sentence contains a mirrored glyph, she reads that sentence from right to left.
A sentence follows this structure.
<seq> is a sequence of zero or more <term>.<term> is either a glyph or a <box>. A glyph may be mirrored.<box> is a rectangle surrounding a <seq>. The height of a box is larger than any glyph inside it.The sentence carved on the monolith is a nonempty <seq>. Each term of the sequence fits in a rectangular bounding box, though the bounding boxes of glyphs are not drawn. The bounding boxes of two adjacent terms never overlap.
Let f be the transliteration function. A sequence s=t1t2…tm is written either from left to right or from right to left. In both cases t1 is the leftmost term of the sentence, t2 the second term from the left, and so on.
Write g˙ for the mirror image of a glyph g. A sequence is written from right to left when it holds at least one single glyph term that cannot be read without mirroring, that is, when an integer i exists such that ti is a single glyph g, g is missing from the glyph dictionary, and g˙ is in it. For such a sequence f(s)=f(tm)f(tm−1)…f(t1), and otherwise f(s)=f(t1)f(t2)…f(tm). Every glyph of a sequence is mirrored whenever that sequence holds at least one glyph that cannot be read without mirroring.
If the term ti is a box enclosing a sequence s′, then f(ti)= [ f(s′) ]. If the term ti is a glyph g, then f(ti) is the letter assigned to g, or the letter assigned to g˙ when the sequence holding g is written from right to left.
Transliterate the sentences carved on the monoliths.
The input holds several datasets. Two zeros separated by one space end the input.
Each dataset has this format.
n m
glyph1
...
glyphn
string1
...
stringm
n (1≤n≤26) is the number of glyphs and m (1≤m≤10) is the number of monoliths. Each glyphi has this format.
c h w
b11...b1w
...
bh1...bhw
c is the lowercase letter Alice assigned to the glyph. h and w (1≤h≤15, 1≤w≤15) are the height and the width of the bitmap of the glyph. The matrix b is the bitmap, where a white cell is . and a black cell is *.
Every glyph gets a distinct letter. Every column of a glyph bitmap holds at least one black cell, and the first row and the last row of a glyph bitmap hold at least one black cell. All glyph bitmaps differ from each other, but the mirror image of one bitmap can equal another bitmap, and a bitmap can be symmetric.
Each stringi has this format.
h w
b11...b1w
...
bh1...bhw
h and w (1≤h≤100, 1≤w≤1000) are the height and the width of the bitmap of the sentence. As in the glyph dictionary, b is the bitmap, where a white cell is . and a black cell is *.
The bitmap carries no noise: every black cell belongs to one glyph or to one rectangle. The height of a rectangle is at least 3 and greater than the height of the tallest glyph in the dictionary. The width of a rectangle is at least 3. A box keeps a margin of one white pixel inside its edge.
Glyphs are never stacked vertically. If two rectangles, or a rectangle and a glyph, share a column, then one of them contains the other. Between every two of the bounding boxes of glyphs and the black cells of rectangles there is at least one white cell. At least one cell of the bitmap is black.
For each monolith print the transliterated sentence on one line. After the output for one dataset print # on one line.