#include <bits/stdc++.h>
using namespace std;
void fail() {
  cout << -1 << endl;
  exit(0);
}
struct UnionFind {
  vector<int> par;
  vector<int> sz;
  UnionFind(int n = 0) {
    if (n > 0) initialize(n);
  }
  void initialize(int n) {
    par.resize(n);
    sz.resize(n);
    for (int i = 0; i < n; i++) {
      par[i] = i;
      sz[i] = 1;
    }
  }
  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return false;
    if (sz[x] > sz[y]) swap(x, y);
    par[x] = y;
    sz[y] += sz[x];
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return sz[find(x)]; }
};
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> D(N);
  map<int, vector<int>> d2p;
  for (int i = 0; i < N; i++) {
    cin >> D[i];
    d2p[D[i]].push_back(i);
  }
  vector<pair<int, int>> edges[100000];
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edges[a].emplace_back(b, i);
    edges[b].emplace_back(a, i);
  }
  vector<int> ans(M), col(N, -1);
  UnionFind uf(N);
  bitset<100000> olds, news;
  bitset<2> exist;
  for (auto& ppp : d2p) {
    int d = ppp.first;
    auto& v = ppp.second;
    for (int i : v) {
      news[i] = 1;
      for (auto& p : edges[i]) {
        int j = p.first;
        if (news[j]) {
          uf.unite(i, j);
          if (col[i] == -1) col[i] = 1 - col[j];
          ans[p.second] = (col[i] == col[j] ? 1e9 : d);
        }
      }
      if (col[i] == -1) {
        exist.reset();
        for (auto& p : edges[i]) {
          int j = p.first;
          if (olds[j]) exist[col[j]] = 1;
        }
        col[i] = (exist[0] ? 1 : 0);
      }
      for (auto& p : edges[i]) {
        int j = p.first;
        if (olds[j]) {
          uf.unite(i, j);
          ans[p.second] = (col[i] == col[j] ? 1e9 : d);
        }
      }
    }
    for (int i : v) {
      if (uf.size(i) == 1) fail();
      news[i] = 0;
      olds[i] = 1;
    }
  }
  for (int i = 0; i < N; i++) cout << (col[i] ? "W" : "B");
  cout << endl;
  for (int a : ans) cout << a << endl;
  return 0;
}
