#include <bits/stdc++.h>
using namespace std;
int main() try {
  int n, m;
  scanf("%d%d", &n, &m);
  vector<int> ds(n + 1, 1010101010);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &ds[i]);
  }
  vector<vector<pair<int, int>>> G(n);
  for (int i = 0; i < m; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    G[a - 1].emplace_back(b - 1, i);
    G[b - 1].emplace_back(a - 1, i);
  }
  string res1(n, '?');
  vector<int> res2(m, 1000000000);
  int mind = *min_element(ds.begin(), ds.end());
  priority_queue<pair<int, int>> pq;
  for (int i = 0; i < n; ++i) {
    if (ds[i] != mind) {
      continue;
    }
    bool ok = false;
    for (const pair<int, int> &e : G[i]) {
      ok |= ds[e.first] == mind;
    }
    if (!ok) {
      throw 30;
    }
    pq.emplace(-ds[i], i);
  }
  while (!pq.empty()) {
    int u = pq.top().second;
    pq.pop();
    if (res1[u] != '?') {
      continue;
    }
    int mindv = n;
    int eidx = -1;
    for (const pair<int, int> &e : G[u]) {
      int v = e.first;
      if (res1[v] != '?' && ds[mindv] > ds[v]) {
        mindv = v;
        eidx = e.second;
      }
      pq.emplace(-ds[v], v);
    }
    if (eidx < 0) {
      res1[u] = 'B';
    } else if (ds[mindv] > ds[u]) {
      throw 53;
    } else {
      res1[u] = res1[mindv] ^ 'B' ^ 'W';
      res2[eidx] = ds[u];
    }
  }
  if (count(res1.begin(), res1.end(), '?') > 0) {
    throw 60;
  }
  puts(res1.c_str());
  for (int x : res2) {
    printf("%d\n", x);
  }
} catch (int ln) {
  fprintf(stderr, "catch %d\n", ln);
  puts("-1");
}
