#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> d(n);
  vector<pair<int, int>> a;
  for (int i = 0; i < n; ++i) {
    cin >> d[i];
    a.push_back(make_pair(d[i], i));
  }
  vector<pair<int, int>> G[n];
  vector<int> u(m), v(m);
  for (int i = 0; i < m; ++i) {
    cin >> u[i] >> v[i];
    --u[i];
    --v[i];
    G[u[i]].push_back(make_pair(v[i], i));
    G[v[i]].push_back(make_pair(u[i], i));
  }
  sort(a.begin(), a.end());
  vector<int> c(n, -1);
  const int M = 1000000000;
  vector<int> ans(m, M);
  for (int i = 0; i < n; ++i) {
    if (c[a[i].second] != -1) continue;
    bool flag = false;
    int foo = -1, tmp = -1;
    for (int j = 0; j < (int)G[a[i].second].size(); ++j) {
      int y = G[a[i].second][j].first;
      if (c[y] != -1 && d[y] != a[i].first) {
        flag = true;
        c[a[i].second] = c[y];
        ans[G[a[i].second][j].second] = a[i].first - d[y];
        break;
      } else {
        if (foo == -1 || d[foo] > d[y]) {
          foo = y;
          tmp = G[a[i].second][j].second;
        }
      }
    }
    if (flag == false) {
      if (d[foo] == a[i].first) {
        c[a[i].second] = 0;
        c[foo] = 1;
        ans[tmp] = d[foo];
        continue;
      } else {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  for (int i = 0; i < n; ++i)
    if (c[i] == 0)
      cout << 'W';
    else
      cout << 'B';
  cout << endl;
  for (int i = 0; i < m; ++i) cout << ans[i] << endl;
  return 0;
}
