#include <bits/stdc++.h>
using namespace std;
long long v_color[200005] = {0};
vector<pair<long long, long long> > knode[200005], node[200005];
const long long inf = 1e9;
long long ans[200005] = {0};
long long dn[200005] = {0};
void dfs(long long pos, long long val) {
  v_color[pos] = val;
  for (long long i = 0; i < node[pos].size(); i++) {
    long long to = node[pos][i].first;
    if (v_color[to] == 0) {
      ans[node[pos][i].second] = dn[to];
      dfs(to, val % 2 + 1);
    }
  }
}
signed main() {
  long long n, m;
  cin >> n >> m;
  vector<pair<long long, long long> > vec;
  for (long long i = 1; i <= n; i++) {
    long long val;
    cin >> val;
    dn[i] = val;
    vec.push_back(pair<long long, long long>(val, i));
  }
  sort(vec.begin(), vec.end());
  for (long long i = 1; i <= m; i++) {
    long long u, v;
    cin >> u >> v;
    knode[u].push_back(pair<long long, long long>(v, i));
    knode[v].push_back(pair<long long, long long>(u, i));
  }
  for (long long i = 1; i <= n; i++) {
    pair<long long, long long> min_v = pair<long long, long long>(inf, inf);
    for (long long j = 0; j < knode[i].size(); j++) {
      long long to = knode[i][j].first;
      min_v = min(min_v, pair<long long, long long>(dn[to], j));
    }
    node[knode[i][min_v.second].first].push_back(
        pair<long long, long long>(i, knode[i][min_v.second].second));
  }
  for (long long i = 1; i <= n; i++) {
    bool f = false;
    for (long long j = 0; j < knode[i].size(); j++) {
      long long to = knode[i][j].first;
      if (dn[to] <= dn[i]) f = true;
    }
    if (!f) {
      cout << -1 << endl;
      return 0;
    }
  }
  for (long long i = 1; i <= n; i++) {
    if (v_color[i] == 0) {
      dfs(i, 1);
    }
  }
  for (long long i = 1; i <= n; i++) {
    if (v_color[i] == 0)
      cout << "0";
    else if (v_color[i] == 1)
      cout << "B";
    else
      cout << "W";
  }
  cout << endl;
  for (long long i = 1; i <= m; i++) {
    cout << (ans[i] == 0 ? inf : ans[i]) << endl;
  }
}
