Ancient Commemorative Monolith

No attempts yetTime limit8sMemory limit256 MB

Problem

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.

  • A sentence <seq> is a sequence of zero or more <term>.
  • A term <term> is either a glyph or a <box>. A glyph may be mirrored.
  • A <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 ff be the transliteration function. A sequence s=t1t2tms = t_1 t_2 \dots t_m is written either from left to right or from right to left. In both cases t1t_1 is the leftmost term of the sentence, t2t_2 the second term from the left, and so on.

Write g˙\dot{g} for the mirror image of a glyph gg. 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 ii exists such that tit_i is a single glyph gg, gg is missing from the glyph dictionary, and g˙\dot{g} is in it. For such a sequence f(s)=f(tm)f(tm1)f(t1)f(s) = f(t_m) f(t_{m-1}) \dots f(t_1), and otherwise f(s)=f(t1)f(t2)f(tm)f(s) = f(t_1) f(t_2) \dots f(t_m). Every glyph of a sequence is mirrored whenever that sequence holds at least one glyph that cannot be read without mirroring.

If the term tit_i is a box enclosing a sequence ss', then f(ti)=f(t_i) = [ f(s)f(s') ]. If the term tit_i is a glyph gg, then f(ti)f(t_i) is the letter assigned to gg, or the letter assigned to g˙\dot{g} when the sequence holding gg is written from right to left.

Transliterate the sentences carved on the monoliths.

Input

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

nn (1n261 \leq n \leq 26) is the number of glyphs and mm (1m101 \leq m \leq 10) is the number of monoliths. Each glyphiglyph_i has this format.

c h w
b11...b1w
...
bh1...bhw

cc is the lowercase letter Alice assigned to the glyph. hh and ww (1h151 \leq h \leq 15, 1w151 \leq w \leq 15) are the height and the width of the bitmap of the glyph. The matrix bb 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 stringistring_i has this format.

h w
b11...b1w
...
bh1...bhw

hh and ww (1h1001 \leq h \leq 100, 1w10001 \leq w \leq 1000) are the height and the width of the bitmap of the sentence. As in the glyph dictionary, bb 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.

Output

For each monolith print the transliterated sentence on one line. After the output for one dataset print # on one line.