Drawing Polygons

Time limit1sMemory limit128 MB

Problem

You want to draw a polygon on grid paper. A shape sequence is a sequence of numbers from 1 to 4. Each number means drawing one unit in one direction: 1 to the right, 2 upward, 3 to the left, and 4 downward.

The same polygon may have different shape sequences if a different starting point is chosen or if the boundary is traced in the opposite direction. For example, polygon (2) in the figure starts at point A and follows the shape sequence 1411433322; polygon (3) starts at point B and follows 3221411433; polygon (4) starts at point C and follows 4411123323. All three sequences draw the same polygon as polygon (1).

A rotated or reflected polygon is not considered the same polygon. Therefore polygons (5) and (6) in the figure are different from polygon (1).

Given one reference shape sequence and several candidate shape sequences, write a program that finds every candidate sequence that can draw the same polygon as the reference.

Input

The first line contains N, the length of the reference shape sequence. The second line contains the reference shape sequence. The third line contains M, the number of candidate shape sequences. Each of the next M lines contains one shape sequence of the same length as the reference.

M is at most 100, and N is at most 50. In every shape sequence, adjacent numbers are separated by one space.

Output

Print the number of input shape sequences that draw the same polygon as the reference on the first line. Then print those sequences in input order, one per line. Separate adjacent numbers in each sequence with one space.