#include <bits/stdc++.h>
using namespace std;
int N, M;
pair<int, int> p[100000];
map<pair<int, int>, int> edge_idx;
vector<int> G[100000];
int D[100000];
bool used[100000];
bool colored[100000];
bool color[100000];
int U[200000], V[200000], ans[200000];
void paint(int v, int col) {
  color[v] = col;
  colored[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    if (!colored[G[v][i]]) {
      int s = v;
      int t = G[v][i];
      if (s > t) swap(s, t);
      int idx = edge_idx[pair<int, int>(s, t)];
      if (ans[idx] == 0) continue;
      if (ans[idx] > 0) paint(G[v][i], col);
      if (ans[idx] < 0) paint(G[v][i], col ^ 1);
    }
  }
}
const int INF = 1e+9;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    cin >> D[i];
    p[i] = pair<int, int>(D[i], i);
  }
  for (int i = 0; i < M; i++) {
    cin >> U[i] >> V[i];
    U[i]--;
    V[i]--;
    G[U[i]].push_back(V[i]);
    G[V[i]].push_back(U[i]);
    if (U[i] > V[i]) swap(U[i], V[i]);
    edge_idx[pair<int, int>(U[i], V[i])] = i;
  }
  sort(p, p + N);
  for (int i = 0; i < N; i++) {
    int v = p[i].second;
    int w = p[i].first;
    if (used[v]) continue;
    for (int j = 0; j < G[v].size(); j++) {
      if (D[G[v][j]] < D[v]) {
        int s = v;
        int t = G[v][j];
        int w = D[s] - D[t];
        if (s > t) swap(s, t);
        ans[edge_idx[pair<int, int>(s, t)]] = w;
        used[v] = true;
        break;
      } else if (D[G[v][j]] == D[v]) {
        int s = v;
        int t = G[v][j];
        int w = D[s];
        if (s > t) swap(s, t);
        ans[edge_idx[pair<int, int>(s, t)]] = -w;
        used[s] = true;
        used[t] = true;
        break;
      }
      if (j + 1 == G[v].size()) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  paint(0, 0);
  for (int i = 0; i < M; i++) {
    if (ans[i] == 0) ans[i] = INF;
  }
  for (int i = 0; i < N; i++) {
    cout << (color[i] == 1 ? "W" : "B");
    if (i == N - 1) cout << endl;
  }
  for (int i = 0; i < M; i++) {
    cout << abs(ans[i]) << endl;
  }
}
