#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <functional>
#include <tuple>
#include <cassert>

template <class T, class U>
inline bool chmin(T& lhs, const U& rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U>
inline bool chmax(T& lhs, const U& rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

// [l, r) from l to r
struct frep {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { ++i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr frep(int l_, int r_): l(std::min(l_, r_)), r(r_) {}
  constexpr itr begin() const { return l; }
  constexpr itr end() const { return r; }
};

// [l, r) from r to l
struct brep {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { --i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr r, l;
  constexpr brep(int r_, int l_): r(std::max(l_, r_) - 1), l(l_ - 1) {}
  constexpr itr begin() const { return r; }
  constexpr itr end() const { return l; }
};

template <class T>
inline T scan() {
  T res;
  std::cin >> res;
  return res;
}

int main() {
  constexpr int inf = 1000000000;
  int N, M;
  std::cin >> N >> M;
  std::vector<int> value(N);
  for (auto& x: value) {
    std::cin >> x;
  }
  std::vector<std::vector<std::pair<int, int>>> graph(N);
  for (const int i: frep(0, M)) {
    int u = scan<int>() - 1;
    int v = scan<int>() - 1;
    graph[u].emplace_back(v, i);
    graph[v].emplace_back(u, i);
  }
  std::vector<int> answer(M);
  std::vector<std::pair<int, int>> newgraph(N);
  for (const int v: frep(0, N)) {
    std::sort(graph[v].begin(), graph[v].end());
    int to = -1, val = inf;
    for (const auto e: graph[v]) {
      if (chmin(val, value[e.first])) {
        to = e.first;
      }
    }
    if (val > value[v]) {
      std::cout << "-1\n";
      return 0;
    }
    for (const auto e: graph[v]) {
      if (e.first == to) {
        newgraph[v] = e;
      } else {
        answer[e.second] = inf;
      }
    }
  }
  std::vector<int> color(N);
  for (const int v: frep(0, N)) {
    if (color[v] > 0) {
      continue;
    }
    int u, id;
    std::tie(u, id) = newgraph[v];
    if (newgraph[u].first == v) {
      color[v] = 1;
      color[u] = 2;
      answer[id] = value[v];
    }
  }
  std::function<void(int)> dfs = [&](int v) {
    if (color[v] > 0) {
      return;
    }
    int u, id;
    std::tie(u, id) = newgraph[v];
    dfs(u);
    color[v] = 3 - color[u];
    answer[id] = value[v];
  };
  for (const int v: frep(0, N)) {
    dfs(v);
    assert(color[v] == 1 || color[v] == 2);
    std::cout << (color[v] == 1 ? 'B' : 'W');
  }
  std::cout << '\n';
  for (const int i: frep(0, M)) {
    assert(1 <= answer[i] && answer[i] <= 1000000000);
    std::cout << answer[i] << '\n';
  }
  return 0;
}
