A company logo is designed on a rectangular grid of unit squares by blackening some of them. A sign is built by gluing gold patches onto a rectangular background so that the resulting golden figure has exactly the shape of the logo (the set of black squares). Patches may be rotated and flipped (used either side up), but they may not overlap. Each unit square of the design corresponds to one gold square of size 1m×1m.
The manufacturer uses patches of a few shapes. Each patch is a single connected piece of unit squares (you can move between any two of its squares through squares that share an edge), stamped from a 3×3 matrix by cutting out some of the nine squares.
For each order (design), decide whether it can be built using only the available patch shapes, and if so find the minimum number of patches required.
Write a program that:
The first line contains an integer n (1≤n≤5), the number of patch shapes used.
Then follow n shape descriptions. Each shape spans 3 lines of 3 characters each. # means the square belongs to the patch, . means the square is cut out of the matrix.
Then a line contains the number of designs m (1≤m≤3).
Then follow m design descriptions. Each design begins with two integers x, y (1≤x≤55, 1≤y≤5), the width and height of the sign in meters. Each of the next y lines contains x characters. # means the square must be gold, . means it must stay empty.
Consecutive blocks in the input (the count lines, each shape description, each design description) are separated by a single blank line.
Print exactly m lines. On the i-th line print the minimum number of patches needed to produce the i-th design, or the single word NIE if the design cannot be built.