#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename T>
using posteriority_queue = priority_queue<T, vector<T>, greater<T> >;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
template <typename T, typename U>
inline bool chmax(T &a, U b) {
  return a < b ? (a = b, true) : false;
}
template <typename T, typename U>
inline bool chmin(T &a, U b) {
  return a > b ? (a = b, true) : false;
}
template <typename T>
void unique(vector<T> &a) {
  a.erase(unique((a).begin(), (a).end()), a.end());
}
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> d(n);
  for (int i = (0); i < (n); ++i) cin >> d[i];
  vector<int> u(m), v(m), s(n, 0), c(m, -1);
  vector<vector<pair<int, int> > > graph(n);
  queue<int> que;
  for (int i = (0); i < (m); ++i) {
    cin >> u[i] >> v[i];
    --u[i];
    --v[i];
    if (d[u[i]] > d[v[i]]) swap(u[i], v[i]);
    if (d[u[i]] == d[v[i]]) {
      s[u[i]] = -1;
      s[v[i]] = 1;
      c[i] = d[u[i]];
      que.emplace(u[i]);
      que.emplace(v[i]);
    } else {
      graph[u[i]].emplace_back(v[i], i);
    }
  }
  while (!que.empty()) {
    int ver = que.front();
    que.pop();
    for (auto pr : graph[ver]) {
      int e, idx;
      tie(e, idx) = pr;
      if (s[e] == 0) {
        s[e] = s[ver];
        c[idx] = d[e] - d[ver];
        que.emplace(e);
      }
    }
  }
  for (int i = (0); i < (n); ++i) {
    if (s[i] == 0) {
      cout << "-1\n";
      return 0;
    }
  }
  for (int i = (0); i < (n); ++i) cout << (s[i] == -1 ? 'B' : 'W');
  cout << '\n';
  for (int i = (0); i < (m); ++i)
    cout << (c[i] == -1 ? 1000000000 : c[i]) << '\n';
  return 0;
}
