#include <bits/stdc++.h>

using namespace std;

#define range(i, m, n) for(int i = m; i < n; i++)
#define husk(i, m, n) for(int i = m; i > n; i--)

int n, m;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  vector<pair<int, int>> edges;
  vector<vector<int>> g(n);
  vector<int> a(n);
  range(i, 0, n) {
    cin >> a[i];
  }
  range(i, 0, m) {
    int u, v;
    cin >> u >> v;
    u--; v--;
    g[u].push_back(v);
    g[v].push_back(u);
    edges.emplace_back(u, v);
  }

  vector<int> color(n, -1);

  range(i, 0, n) {
    bool ok = false;
    for(int j : g[i]) ok |= a[j] <= a[i];
    if(!ok) {
      cout << -1;
      return 0;
    }
  }

  for(auto x : edges) {
    int u = x.first;
    int v = x.second;
    if(a[u] == a[v]) {
      if(color[u] == -1 && color[v] == -1) {
        color[u] = 0;
        color[v] = 1;
      } else {
        if(color[u] == -1) color[u] = color[v] ^ 1;
        if(color[v] == -1) color[v] = color[u] ^ 1;
      }
    }
  }

  range(i, 0, n) if(color[i] == -1) color[i] = 0;

  range(i, 0, n) {
    cout << (color[i] ? 'B' : 'W');
  }
  cout << endl;
  for(auto x : edges) {
    int u = x.first, v = x.second;
    if(color[u] != color[v]) cout << max(a[u], a[v]);
    else {
      if(a[u] == a[v]) cout << 1000000000;
      else cout << abs(a[u] - a[v]);
    }
    cout << endl;
  }
  return 0;
}
