import strutils, sequtils, tables, algorithm

let get = iterator: string {.closure.} =
  for s in stdin.readAll.split: yield s
proc read: int = get().parseInt

###############################################################################

type edge = tuple[u, v: int]
proc opp(col: char): char = 
  if col == 'B': 'W' else: 'B'

let n, m = read()
let d = newSeqWith(n, read())
let e = newSeqWith(m, (u: read() - 1, v: read() - 1))

var adj = newSeqWith(n, newSeq[int]())
for e in e:
  adj[e.u].add(e.v)
  adj[e.v].add(e.u)

var col = newSeqWith(n, '_')
var wgt = initTable[edge, int]()
for i, adj in adj:
  let to = adj.foldl( if d[a] > d[b]: b else: a )
  if d[to] > d[i]:
    echo "-1"
    quit()
  wgt[(i, to)] = d[i]
  wgt[(to, i)] = d[i]
  if col[i] == '_':
    col[i] = col[to].opp()
    col[to] = col[i].opp()
  elif col[to] == '_':
    col[to] = col[i].opp()
    col[i] = col[to].opp()

echo col.mapIt($it).join()
echo e.mapIt(
  if wgt.hasKey(it): $wgt[it]
  else: "1000000000"
).join("\n")

