import sys
readline = sys.stdin.readline
from collections import Counter

N, M = map(int, readline().split())
D = list(map(int, readline().split()))
Edge = [[] for _ in range(N)]
Edgenum = Counter()
for i in range(M):
    u, v = map(int, readline().split())
    u -= 1
    v -= 1
    Edge[u].append(v)
    Edge[v].append(u)
    if u > v:
        u, v = v, u
    Edgenum[(u, v)] = i

table = [10**9]*M

md = min(D)

st = [i for i in range(N) if D[i] == md]
Col = [None]*N
def calc():
    if len(st) == 1:
        return False

    Do = sorted(range(N), key = lambda x: D[x])
    Col[Do[0]] = 1
    for vn in Do:
        if D[vn] == md:
            for vf in Edge[vn]:
                if D[vf] == md:
                    if Col[vf] is not None:
                        Col[vn] = not Col[vf]
                        a, b = vn, vf
                        if a > b:
                            a, b = b, a
                        table[Edgenum[(a, b)]] = D[vn]
                    else:
                        Col[vn] = 1
                        a, b = vn, vf
                        if a > b:
                            a, b = b, a
                        table[Edgenum[(a, b)]] = D[vn]
                    break
            else:
                return False
        else:
            for vf in Edge[vn]:
                if D[vf] <= D[vn]:
                    Col[vn] = not Col[vf]
                    a, b = vn, vf
                    if a > b:
                        a, b = b, a
                    table[Edgenum[(a, b)]] = D[vn]
                    break
                else:
                    return False
    return True

if not calc():
    print(-1)
else:
    print(''.join(['B' if Col[i] else 'W' for i in range(N)]))
    print('\n'.join(map(str, table)))
