#include <bits/stdc++.h>
using namespace std;
int ng() {
  cout << -1 << "\n";
  return 0;
}
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> d(n);
  vector<pair<int, int>> ds;
  for (int i = 0; i < n; i++) {
    cin >> d[i];
    ds.emplace_back(d[i], i);
  }
  vector<pair<int, int>> e[n], uv;
  vector<int> c(m, 0), col(n, 0);
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    uv.emplace_back(u, v);
    e[u].emplace_back(v, i);
    e[v].emplace_back(u, i);
  }
  sort(ds.begin(), ds.end());
  for (int i = 0; i < n; i++) {
    auto &x = ds[i].second;
    auto &p = ds[i].first;
    bool ok = false;
    for (auto &ed : e[x]) {
      if (d[ed.first] <= p) {
        c[ed.second] = p;
        ok = true;
        break;
      }
    }
    if (!ok) return ng();
  }
  for (int i = 0; i < m; i++) {
    if (c[i]) {
      auto &u = uv[i].first;
      auto &v = uv[i].second;
      if (!col[u] && !col[v]) {
        col[u] = 1;
      }
    } else
      c[i] = 1e9;
  }
  for (int i = 0; i < n; i++) {
    cout << (col[i] ? 'B' : 'W');
  }
  cout << "\n";
  for (int i = 0; i < m; i++) {
    cout << c[i] << "\n";
  }
  return 0;
}
