#!/usr/bin/env python3
import sys
input = sys.stdin.readline
INF = 10**9

n, m = map(int, input().split())
costs = [int(item) for item in input().split()]
costs_sorted = sorted([(item, i) for i, item in enumerate(costs)], reverse=True)

edge = [[] for _ in range(n)]
edge_dict = dict()
for i in range(m):
    u, v = map(int, input().split())
    u -= 1; v -= 1
    edge[u].append(v)
    edge[v].append(u)
    if u > v:
        u, v = v, u
    edge_dict[(u, v)] = i

visited = [0] * n
color = [-1] * n
costs_on_edge = [INF] * m

while costs_sorted:
    c, v = costs_sorted.pop()
    if visited[v]:
        continue
    visited[v] = True
    ok = False
    for nv in edge[v]:
        if costs[nv] <= c:
            if color[nv] == -1 and color[v] == -1:
                color[nv] = True
                color[v] = False
                visited[nv] = True
            else:
                color[v] = not color[nv]
            if nv < v:
                costs_on_edge[edge_dict[nv, v]] = c
            else:
                costs_on_edge[edge_dict[v, nv]] = c
            ok = True
            break
    # Construction Failed
    if not ok:
        print(-1)
        exit()
color_str = ""
for item in color:
    if item == 1:
        color_str += "W"
    else:
        color_str += "B"
print(color_str)
for item in costs_on_edge:
    print(item)