#include <bits/stdc++.h>
using i64 = long long;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  std::vector<int> ds;
  std::vector<std::pair<int, int>> vs;
  for (int i = 0; i < n; i++) {
    int d;
    scanf("%d", &d);
    ds.push_back(d);
    vs.emplace_back(d, i);
  }
  std::vector<std::vector<int>> es(n);
  std::map<std::pair<int, int>, int> eindex;
  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    --a;
    --b;
    es[a].push_back(b);
    es[b].push_back(a);
    eindex[std::minmax(a, b)] = i;
  }
  std::sort(vs.begin(), vs.end());
  std::vector<int> cs(n, -1), elen(m);
  for (int i = 0; i < n; i++) {
    int j = i, cd = vs[i].first;
    while (j < n && vs[j].first == cd) j++;
    for (int k = i; k < j; k++) {
      int v = vs[k].second;
      if (cs[v] >= 0) continue;
      for (int to : es[v]) {
        if (ds[to] == cd) {
          elen[eindex[std::minmax(v, to)]] = cd;
          if (cs[to] >= 0)
            cs[v] = !cs[to];
          else {
            cs[v] = 0;
            cs[to] = 1;
          }
          break;
        } else if (cs[to] >= 0) {
          cs[v] = cs[to];
          elen[eindex[std::minmax(v, to)]] = ds[v] - ds[to];
          break;
        }
      }
      if (cs[v] == -1) {
        std::cout << -1 << std::endl;
        return 0;
      }
    }
  }
  for (int i = 0; i < n; i++) printf("%c", cs[i] ? 'B' : 'W');
  printf("\n");
  for (int i = 0; i < m; i++) printf("%d\n", elen[i] ? elen[i] : 1000000000);
  return 0;
}
