#include <cstdio>
#include <cstdint>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <queue>

using edge = std::tuple<size_t, size_t, size_t>;

int main() {
  size_t n, m;
  scanf("%zu %zu", &n, &m);

  std::vector<int> d(n);
  for (auto& di: d) scanf("%d", &di);

  int const inf = 1e9;
  std::vector<int> mind(n, inf);

  std::vector<std::vector<edge>> g(n);
  for (size_t i = 0; i < m; ++i) {
    size_t u, v;
    scanf("%zu %zu", &u, &v);
    --u, --v;
    g[u].emplace_back(u, v, i);
    g[v].emplace_back(v, u, i);
    mind[u] = std::min(mind[u], d[v]);
    mind[v] = std::min(mind[v], d[u]);
  }

  std::string color(n, '.');
  std::vector<int> res(m, inf);

  edge cand(n, n, m);
  for (auto const& gi: g) {
    for (auto const& e: gi) {
      size_t u, v, i;
      std::tie(u, v, i) = e;
      if (mind[u] == mind[v] && d[u] == d[v]) {
        cand = e;
        break;
      }
    }
    if (std::get<0>(cand) < n) break;
  }

  if (std::get<0>(cand) == n) return puts("-1"), 0;

  std::queue<size_t> q;
  size_t u, v, ei;
  std::tie(u, v, ei) = cand;
  color[u] = 'W';
  color[v] = 'B';
  res[ei] = d[u];

  q.push(u);
  q.push(v);
  while (!q.empty()) {
    size_t v = q.front();
    // fprintf(stderr, "v: %zu\n", v);
    q.pop();
    int wok = true;
    int bok = true;
    for (auto const& e: g[v]) {
      size_t u, u_, ei;
      std::tie(u, u_, ei) = e;
      u = u ^ u_ ^ v;
      if (color[u] != '.') continue;
      if (d[u] < d[v]) {
        if (color[v] == 'W') bok = false;
        if (color[u] == 'B') wok = false;
      }
      if (!wok && !bok) return puts("-1"), 0;
      if (wok && color[v] == 'W') {
        color[u] = 'B';
      } else if (bok && color[v] == 'B') {
        color[u] = 'W';
      }
      res[ei] = d[u];
      // fprintf(stderr, "q <- %zu\n", u);
      // fprintf(stderr, "d[%zu]: %d\n", u, d[u]);
      q.push(u);
    }
  }

  if (color.find('.') != color.npos)
    return puts("-1"), 0;

  printf("%s\n", color.c_str());
  for (auto wi: res) printf("%d\n", wi);
}
