import sys
input = sys.stdin.readline
n, m = map(int,input().split())
D = list(map(int,input().split()))
U = [list(map(int,input().split())) for i in range(m)]

for i in range(m):
    for j in range(2):
        U[i][j] -= 1

M = [[] for i in range(n)]
for i in range(len(U)):
    M[U[i][0]].append([U[i][1], i])
    M[U[i][1]].append([U[i][0], i])
C = [-1] * n
d = min(D)

ANS = [0] * m

MI = []
MU = []
MV = [0] * n
for i in range(n):
    if D[i] == d:
        MI.append(i)
for i in range(m):
    if D[U[i][0]] == d and D[U[i][1]] == d:
        MU.append([U[i], i])

MM = [[] for i in range(n)]
for i in range(len(MU)):
    MM[MU[i][0][0]].append([MU[i][0][1], MU[i][1]])
    MM[MU[i][0][1]].append([MU[i][0][0], MU[i][1]])

import sys
if len(MI) <= 1:
    print(-1)
    sys.exit()

for i in MI:
    if C[i] == -1:
        Q = [[i,0]]
        C[i] = 0
        s = 0
        while len(Q) > s:
            for jj in MM[Q[s][0]]: 
                j = jj[0]
                if C[j] == -1:
                    Q.append([j, (Q[s][1]+1)%2])
                    C[j] = (Q[s][1]+1)%2
                    ANS[jj[1]] = d
            s += 1

for i in MI:
    if C[i] == -1:
        print(-1)
        sys.exit()

for i in MI:
    Q = [[i,C[i]]]
    s = 0
    while len(Q) > s:
        for jj in M[Q[s][0]]: 
            j = jj[0]
            if C[j] == -1 and D[j] > D[i]:
                Q.append([j, (Q[s][1]+1)%2])
                C[j] = (Q[s][1]+1)%2
                ANS[jj[1]] = D[j]
        s += 1

for i in range(len(ANS)):
    if ANS[i] == 0:
        ANS[i] = 10**9

if -1 in C:
    print(-1)
    sys.exit()

BW = []
for i in range(len(C)):
    if C[i] == 0:
        BW.append("B")
    else:
        BW.append("W")
print("".join(BW))

for i in range(len(ANS)):
    print(ANS[i])