#include <bits/stdc++.h>
using namespace std;
const int maxn = 110000;
const int maxm = 210000;
const int inf = 1e9 + 7;
struct edge {
  int to, nxt;
} e[maxm];
int h[maxn], E;
int n, m;
int d[maxn];
int w[maxm];
int use[maxn];
int col[maxn];
int root[maxn];
int fa[maxn];
int find(int x) {
  if (fa[x] == x) return x;
  fa[x] = find(fa[x]);
  return fa[x];
}
void add(int u, int v) {
  E++;
  e[E].to = v;
  e[E].nxt = h[u];
  h[u] = E;
  return;
}
void mer(int u, int v) {
  u = find(u);
  v = find(v);
  if (u == v) return;
  fa[u] = v;
}
void dfs(int u, int fa) {
  col[u] = col[fa] ^ 1;
  for (int i = h[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (v == fa) continue;
    dfs(v, u);
  }
  return;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= n; i++) scanf("%d", &d[i]);
  for (int i = 1; i <= n; i++) root[i] = -1;
  int u, v;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &u, &v);
    if (find(u) == find(v)) continue;
    if (d[u] > d[v]) swap(u, v);
    if (d[u] == d[v])
      use[u] = use[v] = 1;
    else
      use[v] = 1;
    w[i] = max(d[u], d[v]);
    mer(u, v);
    add(u, v);
    add(v, u);
  }
  for (int i = 1; i <= n; i++) {
    if (!use[i]) {
      printf("-1\n");
      return 0;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (root[find(i)] == -1 || d[root[find(i)]] > d[i]) root[find(i)] = i;
  }
  for (int i = 1; i <= n; i++) {
    if (fa[i] == i) {
      dfs(root[i], 0);
      cout << root[i] << endl;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (col[i])
      printf("W");
    else
      printf("B");
  }
  printf("\n");
  for (int i = 1; i <= m; i++) {
    if (w[i] == 0)
      printf("1000000000\n");
    else
      printf("%d\n", w[i]);
  }
  return 0;
}
