#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
const int mod = 1000000007;
int n, m;
int d[maxn];
int dis[maxn];
vector<int> e[maxn], id[maxn];
void bfs(int st) {
  memset(dis, -1, sizeof(dis));
  dis[st] = 0;
  queue<int> q;
  q.push(st);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = 0; i < e[x].size(); i++) {
      int v = e[x][i];
      if (dis[v] == -1) {
        dis[v] = dis[x] + 1;
        q.push(v);
      }
    }
  }
}
int a[maxn];
bool cmp(int x, int y) {
  if (d[x] != d[y]) return d[x] < d[y];
  return dis[x] < dis[y];
}
bool vis[maxn];
int ans[maxn];
bool color[maxn];
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> d[i];
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    e[x].push_back(y);
    id[x].push_back(i);
    e[y].push_back(x);
    id[y].push_back(i);
  }
  int mi = d[1], idx = 1;
  for (int i = 2; i <= n; i++)
    if (d[i] < mi) {
      mi = d[i];
      idx = i;
    }
  bfs(idx);
  for (int i = 1; i <= n; i++) a[i] = i;
  sort(a + 1, a + n + 1, cmp);
  bool sign = 0;
  memset(vis, 0, sizeof(vis));
  vis[idx] = 1;
  bool same = 0;
  for (int i = 2; i <= n; i++) {
    int mi = mod, pos = -1, posw;
    for (int j = 0; j < e[a[i]].size(); j++) {
      int v = e[a[i]][j];
      int w = id[a[i]][j];
      if (vis[v]) {
        if (mi > d[v]) {
          mi = d[v];
          pos = v;
          posw = w;
        }
      }
    }
    if (pos == -1) {
      sign = 1;
      break;
    }
    if (d[pos] > d[a[i]]) {
      sign = 1;
      break;
    }
    if (d[pos] == d[a[i]]) {
      if (pos == idx) {
        same = 1;
      }
      color[a[i]] = 1 - color[pos];
      ans[posw] = d[pos];
    } else {
      color[a[i]] = color[pos];
      ans[posw] = d[a[i]] - d[pos];
    }
    vis[a[i]] = 1;
  }
  if (same && !sign) {
    for (int i = 1; i <= n; i++)
      if (color[i])
        cout << 'B';
      else
        cout << 'W';
    cout << endl;
    for (int i = 1; i <= m; i++)
      if (ans[i])
        cout << ans[i] << endl;
      else
        cout << mod - 7 << endl;
  } else
    cout << -1 << endl;
  return 0;
}
