#include <bits/stdc++.h>
using namespace std;
struct read_item {
  read_item(){};
  template <class T>
  operator T() {
    T t;
    std::cin >> t;
    return t;
  }
};
const int inf = (int)1.05e9;
int main() {
  const int n = read_item();
  const int m = read_item();
  vector<int> ds(n);
  vector<vector<pair<int, int>>> g(n);
  for (int i = 0; i < n; i++) {
    const int d = read_item();
    ds[i] = d;
  }
  for (int i = 0; i < m; i++) {
    const int u = read_item();
    const int v = read_item();
    g[u - 1].push_back({v - 1, i});
    g[v - 1].push_back({u - 1, i});
  }
  vector<int> color(n, -1);
  vector<int> length(m, 0);
  vector<int> index(n);
  iota(index.begin(), index.end(), 0);
  sort(index.begin(), index.end(),
       [&](const int l, const int r) { return ds[l] < ds[r]; });
  for (auto v : index) {
    bool ok = false;
    for (auto e : g[v]) {
      if (ds[e.first] <= ds[v]) {
        ok = true;
      }
    }
    if (!ok) {
      printf("-1\n");
      return 0;
    }
    color[v] = 0;
    for (auto e : g[v]) {
      const int w = e.first;
      const int e_idx = e.second;
      if (ds[w] <= ds[v]) {
        length[e_idx] = ds[v];
        if (color[w] >= 0) {
          color[v] = 1 - color[w];
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    int mini = inf;
    for (auto& e : g[i]) {
      if (color[e.first] != color[i]) {
        mini = min(mini, length[e.second]);
      }
    }
    assert(mini == ds[i]);
  }
  for (int i = 0; i < n; i++) {
    printf("%c", color[i] == 1 ? 'W' : 'B');
  }
  printf("\n");
  for (int i = 0; i < m; i++) {
    printf("%d\n", length[i]);
  }
  return 0;
}
