#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; (i) < (int)(n); (i)++) {
    cin >> a[i];
  }
  vector<vector<int>> g(n);
  map<pair<int, int>, int> mp;
  for (int i = 0; (i) < (int)(m); (i)++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g[a].push_back(b);
    g[b].push_back(a);
    mp[minmax(a, b)] = i;
  }
  vector<int> ss;
  for (int i = 0; (i) < (int)(n); (i)++) {
    for (auto v : g[i]) {
      if (a[i] == a[v]) {
        ss.push_back(i);
        break;
      }
    }
  }
  if (ss.size() == 0) {
    cout << -1 << endl;
    return 0;
  }
  vector<bool> used(n);
  vector<int> color(n, -1);
  vector<int> ans(m);
  function<void(int, int, int)> dfs = [&](int u, int c, int o) {
    color[u] = c;
    for (auto v : g[u])
      if (!used[v]) {
        if (a[v] == o && a[u] == o) {
          used[v] = true;
          ans[mp[minmax(u, v)]] = o;
          dfs(v, !c, o);
        } else if (a[v] > a[u]) {
          used[v] = true;
          ans[mp[minmax(u, v)]] = a[v] - a[u];
          dfs(v, c, o);
        }
      }
  };
  sort(ss.begin(), ss.end());
  for (auto s : ss)
    if (!used[s]) {
      used[s] = true;
      dfs(s, 0, a[s]);
    }
  bool ok = true;
  for (int i = 0; (i) < (int)(n); (i)++) ok = ok && used[i];
  if (!ok) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 0; (i) < (int)(m); (i)++) {
    if (ans[i] == 0) {
      ans[i] = 1e9;
    }
  }
  for (int i = 0; (i) < (int)(n); (i)++) cout << (color[i] == 1 ? 'B' : 'W');
  cout << endl;
  for (int i = 0; (i) < (int)(m); (i)++) cout << ans[i] << endl;
  return 0;
}
