#include <bits/stdc++.h>
using namespace std;
struct node {
  int v, nxt, data;
  bool can;
};
node edge[1000001];
bool cn;
int fa[100001], FA[1000001], x, y, n, m, head[100001], len, depth[100001],
    d[100001], D[100001], RD[100001], minnD[100001];
void add(int x, int y, int z) {
  edge[++len].v = y;
  edge[len].data = z;
  edge[len].nxt = head[x];
  head[x] = len;
  return;
}
void bfs(int x) {
  int top;
  depth[x] = 1;
  queue<int> q;
  q.push(x);
  while (!q.empty()) {
    top = q.front();
    q.pop();
    for (int i = head[top]; i > 0; i = edge[i].nxt)
      if (!depth[edge[i].v] && edge[i].can) {
        depth[edge[i].v] = depth[top] + 1;
        FA[i] = top;
        fa[edge[i].v] = i;
        q.push(edge[i].v);
      }
  }
  return;
}
int main() {
  len = 1;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) cin >> D[i];
  for (int i = 1; i <= m; ++i) {
    cin >> x >> y;
    add(x, y, i);
    add(y, x, i);
  }
  for (int i = 1; i <= n; ++i) {
    minnD[i] = 1e9;
    for (int j = head[i]; j > 0; j = edge[j].nxt)
      minnD[i] = min(minnD[i], D[edge[j].v]);
    cn = 0;
    for (int j = head[i]; j > 0; j = edge[j].nxt) {
      if (cn == 0 && minnD[i] == D[edge[j].v]) {
        cn = 1;
        edge[j].can = 1;
        edge[j ^ 1].can = 1;
      }
    }
  }
  for (int i = 1; i <= n; ++i)
    if (!depth[i]) bfs(i);
  for (int i = 1; i <= n; ++i) {
    RD[i] = 1e9;
    if (D[i] < D[FA[fa[i]]])
      d[edge[fa[i]].data] = D[FA[fa[i]]];
    else
      d[edge[fa[i]].data] = D[i];
  }
  for (int i = 1; i <= n; ++i)
    for (int j = head[i]; j > 0; j = edge[j].nxt)
      if (d[edge[j].data] != 0) RD[i] = min(RD[i], d[edge[j].data]);
  for (int i = 1; i <= n; ++i)
    if (RD[i] != D[i]) {
      cout << -1 << endl;
      return 0;
    }
  for (int i = 1; i <= n; ++i) {
    if (depth[i] % 2 == 0)
      cout << 'B';
    else
      cout << 'W';
  }
  cout << endl;
  for (int i = 1; i <= m; ++i) {
    if (d[i] == 0)
      cout << 1000000000 << endl;
    else
      cout << d[i] << endl;
  }
  return 0;
}
