Puzzlestan

No attempts yetTime limit1sMemory limit128 MB

Problem

There are NN kinds of items and MM guests. Each guest owns exactly one item of every kind (for example, each guest checks in one overcoat, one hat, and so on). Hence there are N×MN \times M items in total, and each item has a distinct single-letter name (letters are case-sensitive).

The MM items of kind ii (1iN1 \le i \le N) are written as a string of length MM, called the ii-th group. The jj-th character of this string is the jj-th item of group ii.

You are given several statements telling, for two items, whether they belong to the same guest or to different guests. Each statement is one of:

  • The two items belong to the same guest.
  • The two items belong to different guests.

Using these statements, determine which guest owns each item. The statements always determine the assignment uniquely.

Input

The first line contains the number of test cases (at most 2020).

The first line of each test case contains two positive integers NN and MM, where NN (1N71 \le N \le 7) is the number of item kinds and MM (1M71 \le M \le 7) is the number of guests. Each of the next NN lines contains one string of length MM representing a group (the distinct items of one kind).

After that come several statement lines. Each statement has the form i j X k r, meaning that the jj-th item of group ii and the rr-th item of group kk belong to the same guest if XX is R, or to different guests if XX is N. The last line of each test case is the dummy statement 0 0 R 0 0.

Output

For each test case, print MM lines. Line gg (1gM1 \le g \le M) describes the guest identified by the gg-th item of the first group: print that guest's item from group 1, then from group 2, ..., then from group NN, as letters with no separator. Thus the first character of each line matches the first group in order.

Separate the output of consecutive test cases by exactly one blank line.