#include <bits/stdc++.h>
using namespace std;
int n, k;
int d[100002];
int edgeX[200002], edgeY[200002];
vector<pair<int, int> > link[100002];
priority_queue<pair<int, int>, vector<pair<int, int> >,
               greater<pair<int, int> > >
    pq;
bool visited[100002];
bool color[100002];
int weight[100002];
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &d[i]);
    pq.push(make_pair(d[i], i));
  }
  for (int i = 1; i <= k; i++) {
    scanf("%d %d", &edgeX[i], &edgeY[i]);
    link[edgeX[i]].push_back(make_pair(edgeY[i], i));
    link[edgeY[i]].push_back(make_pair(edgeX[i], i));
  }
  while (!pq.empty()) {
    int x = pq.top().second;
    pq.pop();
    if (visited[x]) continue;
    visited[x] = 1;
    vector<pair<int, int> > bv, wv;
    for (auto &y : link[x]) {
      if (visited[y.first]) {
        if (color[y.first] == 0)
          bv.push_back(y);
        else
          wv.push_back(y);
      }
    }
    if (bv.empty() && wv.empty()) {
      bool done = 0;
      for (auto &y : link[x]) {
        if (d[x] == d[y.first]) {
          weight[y.second] = d[x];
          color[x] = 0, color[y.first] = 1;
          visited[y.first] = 1;
          done = 1;
          break;
        }
      }
      if (!done) {
        printf("-1");
        return 0;
      } else
        continue;
    }
    bool done = 0;
    for (auto &y : bv) {
      if (d[y.first] <= d[x]) {
        weight[y.second] = d[x];
        color[x] = 1;
        done = 1;
        break;
      }
    }
    if (done) continue;
    for (auto &y : wv) {
      if (d[y.first] <= d[x]) {
        weight[y.second] = d[x];
        color[x] = 0;
        done = 1;
        break;
      }
    }
    if (done) continue;
    printf("-1");
    return 0;
  }
  for (int i = 1; i <= n; i++) printf("%c", color[i] ? 'W' : 'B');
  puts("");
  for (int i = 1; i <= k; i++) printf("%d\n", weight[i] ? weight[i] : (int)1e9);
}
