#include <bits/stdc++.h>
using namespace std;
class UnionFind {
 public:
  vector<int> par;
  void init(int sz) { par.resize(sz, -1); }
  int root(int pos) {
    if (par[pos] == -1) return pos;
    par[pos] = root(par[pos]);
    return par[pos];
  }
  void unite(int u, int v) {
    u = root(u);
    v = root(v);
    if (u == v) return;
    par[u] = v;
  }
  bool same(int u, int v) {
    if (root(u) == root(v)) return true;
    return false;
  }
};
int N, C[1 << 18];
int M, A[1 << 18], B[1 << 18], col[1 << 18];
bool E[1 << 18], ID[1 << 18];
UnionFind Z;
vector<pair<int, int>> X[1 << 18];
vector<pair<int, int>> Y[1 << 18];
void dfs(int pos, int dep) {
  col[pos] = dep;
  for (pair<int, int> to : Y[pos]) {
    if (col[to.first] != -1) continue;
    dfs(to.first, dep ^ to.second);
  }
}
int main() {
  cin >> N >> M;
  Z.init(N + 2);
  for (int i = 1; i <= N; i++) cin >> C[i];
  queue<int> Q;
  for (int i = 1; i <= M; i++) {
    cin >> A[i] >> B[i];
    if (C[A[i]] == C[B[i]]) {
      if (Z.same(A[i], B[i]) == false) {
        Z.unite(A[i], B[i]);
        E[i] = true;
      }
      ID[A[i]] = true;
      ID[B[i]] = true;
    }
    if (C[A[i]] < C[B[i]]) X[A[i]].push_back(make_pair(B[i], i));
    if (C[B[i]] < C[A[i]]) X[B[i]].push_back(make_pair(A[i], i));
  }
  for (int i = 1; i <= N; i++) {
    if (ID[i] == true) Q.push(i);
  }
  while (!Q.empty()) {
    int pos = Q.front();
    Q.pop();
    for (pair<int, int> to : X[pos]) {
      if (ID[to.first] == true) continue;
      ID[to.first] = true;
      E[to.second] = true;
      Q.push(to.first);
    }
  }
  for (int i = 1; i <= N; i++) {
    if (ID[i] == false) {
      cout << "-1" << endl;
      return 0;
    }
  }
  for (int i = 1; i <= M; i++) {
    if (E[i] == false) continue;
    int cost = 0;
    if (C[A[i]] == C[B[i]]) cost = 1;
    Y[A[i]].push_back(make_pair(B[i], cost));
    Y[B[i]].push_back(make_pair(A[i], cost));
  }
  for (int i = 1; i <= N; i++) col[i] = -1;
  dfs(1, 0);
  for (int i = 1; i <= N; i++) {
    if (col[i] == 0)
      cout << "B";
    else
      cout << "W";
  }
  cout << endl;
  for (int i = 1; i <= M; i++) {
    if (E[i] == false)
      cout << "1000000000" << endl;
    else if (C[A[i]] == C[B[i]])
      cout << C[A[i]] << endl;
    else
      cout << abs(C[A[i]] - C[B[i]]) << endl;
  }
  return 0;
}
