#include <bits/stdc++.h>
using namespace std;
bool RELAXED = false;
template <int, typename T>
struct MINMAX {
  T val;
  MINMAX(T val) : val(val) {}
};
template <typename T>
MINMAX<1, T> MAX(T val) {
  return MINMAX<1, T>(val);
};
template <typename T>
MINMAX<2, T> MIN(T val) {
  return MINMAX<2, T>(val);
};
template <typename T, typename U>
inline T& operator|=(T& lhs, MINMAX<1, U> rhs) {
  return lhs = (rhs.val > lhs ? (RELAXED = true, rhs.val)
                              : (RELAXED = false, lhs));
}
template <typename T, typename U>
inline T& operator|=(T& lhs, MINMAX<2, U> rhs) {
  return lhs = (rhs.val < lhs ? (RELAXED = true, rhs.val)
                              : (RELAXED = false, lhs));
}
template <typename T>
inline vector<T> readVector(int n) {
  vector<T> vec(n);
  for (int i = 0; i < int(n); i++) cin >> vec[i];
  return vec;
}
template <typename T>
void PRINT(T container, bool withSize) {
  if (withSize) cout << container.size() << endl;
  for (auto it : container) cout << it << " ";
  cout << endl;
}
template <typename T>
void EXIT(T answer) {
  cout << answer << endl;
  exit(0);
}
template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T> >;
const int INF = 1e9 + 5;
vector<int> adj[100005];
vector<int> dist(100005, INF);
vector<pair<int, int> > edge;
map<pair<int, int>, int> weight;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<int> D = readVector<int>(n);
  for (int i = 0; i < int(m); i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    edge.push_back({u, v});
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  map<int, vector<int> > dv;
  for (int i = 0; i < int(n); i++) {
    dv[D[i]].push_back(i);
  }
  set<int> whites;
  for (auto el : dv) {
    int d = el.first;
    for (auto v : el.second) {
      int ok = false;
      int samed = -1;
      for (auto to : adj[v]) {
        if (dist[to] != INF && dist[to] < d) {
          weight[{v, to}] = d - dist[to];
          weight[{to, v}] = d - dist[to];
          ok = true;
          dist[v] = d;
        }
        if (D[v] == D[to]) samed = to;
      }
      if (ok) continue;
      if (samed < 0) EXIT(-1);
      whites.insert(v);
      dist[v] = 0;
    }
  }
  for (int i = 0; i < int(n); i++) cout << (whites.count(i) ? 'W' : 'B');
  cout << endl;
  for (int i = 0; i < int(m); i++) {
    if (weight.count(edge[i])) {
      cout << weight[edge[i]] << endl;
    } else
      cout << (int)1e9 << endl;
  }
  return 0;
}
