← Home
from sys import stdin, exit
from math import gcd
 
def wrong():
    print("No solution")
    exit()
 
G = {}
S = {'L': -1, 'R': 1, 'T': -2, 'B': 2}
 
n, m = (int(s) for s in stdin.readline().split())
for _ in range(n + m):
    a, b, p, q = stdin.readline().split()
    p = int(p)
    q = int(q)
    a = S[a]
    b = S[b]
    G[a, p] = (b, q)
    G[b, q] = (a, p)
 
C = []
T = []
dd = dx = dy = 0
while G:
    t = {1: 0, 2: 0, -1: 0, -2: 0}
    (a, p), (b, q) = G.popitem()
    G.pop((b, q))
    c = [(b, q)]
    t[b] += 1
    while (-b, q) != (a, p):
        b, q = G.pop((-b, q))
        G.pop((b, q))
        c.append((b, q))
        t[b] += 1
    C.append(c)
    if t == {1: 1, 2: 1, -1: 1, -2: 1}:
        T.append((0, 0))
        continue
    if (t[1] > 0 and t[-1] > 0) or (t[2] > 0 and t[-2] > 0):
        wrong()
    if dd == 0:
        dx = t[1] - t[-1]
        dy = t[2] - t[-2]
    else:
        if (dx != t[1] - t[-1] or dy != t[2] - t[-2]) and (-dx != t[1] - t[-1] or -dy != t[2] - t[-2]):
            wrong()
    T.append((1 if t[1] > 0 else -1 if t[-1] > 0 else 0, 1 if t[2] > 0 else -1 if t[-2] > 0 else 0))
    dd += 1
 
dx = abs(dx)
dy = abs(dy)
if dd > 0 and gcd(dx, dy) > 1:
    wrong()
 
X = [0] * m
Y = [0] * n
x1 = 1
x2 = m
y1 = 1
y2 = n
 
for i in range(len(C)):
    if T[i] == (0, 0):
        c = C[i]
        for j in range(4):
            a, p = c[j]
            b, q = c[(j + 1) % 4]
            if a == 1:
                X[x1 - 1] = q
                x1 += 1
            elif a == -1:
                X[x2 - 1] = q
                x2 -= 1
            elif a == 2:
                Y[y1 - 1] = q
                y1 += 1
            else:
                Y[y2 - 1] = q
                y2 -= 1
#        print('*', x1, x2, y1, y2)
 
for i in range(len(C)):
    if T[i] != (0, 0):
        c = C[i]
#        print(c)
        z = z1 = z2 = j0 = 0
        for j in range(dx + dy):
#            print(z)
            a, p = c[j]
            if abs(a) == 1:
                z -= dy
            else:
                z += dx
            if z < z1:
                z1 = z
                j0 = j + 1
            elif z > z2:
                z2 = z
        if z2 - z1 != dx + dy - 1:
            wrong()
        tx, ty = T[i]
        x = x1 if tx > 0 else x2 - dd + 1
        x1 += 1
        x2 += 1
        y = (y2 - dd + 1 + dd * dy if ty > 0 else y1 - dd * dy) + (dd - 1 if tx == ty else 0)
        y1 -= 1 if tx == ty else -1
        y2 -= 1 if tx == ty else -1
#        print(x, y, tx, ty, x1, x2, y1, y2, dx, dy, j0, c[j0-1])
        for j in range(dx + dy):
            a, p = c[(j0 + j) % len(c)]
#            print(x, y, a)
            if abs(a) == 1:
                Y[y - 1] = p
                x -= tx * dd * dy
                y += ty * dd * dy
            else:
                X[x - 1] = p
                x += tx * dd * dx
                y -= ty * dd * dx
 
print(' '.join(str(i) for i in Y))
print(' '.join(str(i) for i in X))