#include <bits/stdc++.h>
using namespace std;
const int mn = 2e5 + 10;
pair<int, int> e[mn];
vector<int> g[mn];
inline int gy(int x, int i) { return x ^ e[i].first ^ e[i].second; }
int d[mn], o[mn], ro[mn], ans[mn];
int col[mn];
void die() {
  printf("-1");
  exit(0);
}
set<int> s;
void dfs(int x) {
  s.erase(x);
  for (int i : g[x]) {
    int y = gy(x, i);
    if (s.find(y) != s.end()) {
      ans[i] = d[x];
      col[y] = col[x] ^ 1;
      dfs(y);
    }
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", d + i);
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &e[i].first, &e[i].second);
    g[e[i].first].push_back(i);
    g[e[i].second].push_back(i);
    ans[i] = 1e9;
  }
  for (int i = 1; i <= n; i++) o[i] = i;
  sort(o + 1, o + 1 + n, [](int a, int b) { return d[a] < d[b]; });
  for (int i = 1; i <= n; i++) ro[o[i]] = i;
  memset(col, -1, sizeof(col));
  for (int i = 1; i <= n;) {
    int r;
    for (r = i; d[ro[r]] == d[ro[i]]; r++)
      ;
    r--;
    s.clear();
    for (int j = i; j <= r; j++) s.insert(ro[j]);
    stack<int> ed;
    for (int j = i; j <= r; j++) {
      int x = ro[j];
      for (int k : g[x]) {
        int y = gy(x, k);
        if (o[y] >= i && o[y] <= r) ed.push(k);
      }
    }
    for (int j = i; j <= r; j++) {
      int x = ro[j];
      for (int k : g[x]) {
        int y = gy(x, k);
        if (col[y] != -1) {
          ans[k] = d[x];
          col[x] = col[y] ^ 1;
          dfs(x);
          break;
        }
      }
    }
    while (s.size()) {
      if (s.size() == 1) die();
      while (ed.size() && !(s.find(e[ed.top()].first) != s.end() &&
                            s.find(e[ed.top()].second) != s.end()))
        ed.pop();
      if (ed.size() == 0) die();
      int x = e[ed.top()].first;
      col[x] = 0;
      dfs(x);
    }
    i = r + 1;
  }
  for (int i = 1; i <= n; i++) printf("%c", col[i] ? 'B' : 'W');
  for (int i = 0; i < m; i++) printf("\n%d", ans[i]);
}
