#include <bits/stdc++.h>
using namespace std;

struct UnionFind {
  vector<int> t;
  UnionFind(int n) : t(n, -1) {}
  int find(int v) { return t[v] < 0 ? v : t[v] = find(t[v]); }
  void unite(int u, int v) {
    if ((u = find(u)) == (v = find(v))) return;
    if (-t[u] < -t[v]) swap(u, v);
    t[u] += t[v];
    t[v] = u;
  }
  bool same(int u, int v) { return find(u) == find(v); }
  int size(int v) { return -t[find(v)]; }
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> d(n);
  map<int, vector<int>> mp;
  for (int v = 0; v < n; ++v) {
    cin >> d[v];
    mp[d[v]].push_back(v);
  }
  struct Edge { int u, v; };
  vector<Edge> es(m);
  vector<vector<int>> g(n);
  for (int id = 0; id < m; ++id) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    es[id] = {u, v};
    g[u].push_back(id);
    g[v].push_back(id);
  }
  UnionFind uf(n);
  vector<bool> st(m), used(n);
  for (auto e : mp) {
    for (int v : e.second) {
      used[v] = true;
    }
    for (int v : e.second) {
      for (int id : g[v]) {
        int u = v ^ es[id].u ^ es[id].v;
        if (used[u]) {
          if (not uf.same(v, u)) {
            uf.unite(v, u);
            st[id] = true;
          }
        }
      }
    }
    for (int v : e.second) {
      if (uf.size(v) == 1) {
        cout << "-1\n";
        return 0;
      }
    }
  }
  UnionFind buf(2 * n);
  for (int id = 0; id < m; ++id) {
    if (st[id]) {
      buf.unite(es[id].u, n + es[id].v);
      buf.unite(n + es[id].u, es[id].v);
    }
  }
  string s(n, '?');
  for (int v = 0; v < n; ++v) {
    s[v] = buf.same(0, v) ? 'B' : 'W';
  }
  cout << s << '\n';
  for (int id = 0; id < m; ++id) {
    if (st[id]) {
      cout << max(d[es[id].u], d[es[id].v]) << '\n';
    } else {
      cout << (int)1e9 << '\n';
    }
  }
}
