#include <bits/stdc++.h>
using namespace std;
void fail() { cout << -1 << endl; exit(0); }
int main() {
    int N, M; cin >> N >> M;
    vector<int> D(N); for (auto &d : D) cin >> d;
    vector<pair<int, int>> E(M);
    vector<int> C(M, -1);
    for (int i = 0; i < M; i++) {
        int u, v; cin >> u >> v; u--, v--;
        E[i] = make_pair(u, v);
    }
    vector<vector<pair<int, int>>> G(N);
    for (int i = 0; i < M; i++) {
        int u, v; tie(u, v) = E[i];
        G[u].emplace_back(v, i);
        G[v].emplace_back(u, i);
    }
    vector<int> done(N);
    vector<vector<int>> H(N);
    for (int i = 0; i < M; i++) {
        int u, v; tie(u, v) = E[i];
        if (D[u] == D[v] && !done[u] && !done[v]) {
            H[u].push_back(v);
            H[v].push_back(u);
            C[i] = D[u];
            done[u] = done[v] = D[u];
        }
    }
    vector<int> I(N); iota(begin(I), end(I), 0);
    sort(begin(I), end(I), [&](int i, int j) { return D[i] < D[j]; });
    for (int i : I) if (!done[i]) {
        for (auto p : G[i]) {
            int next, idx; tie(next, idx) = p;
            if (done[next] && done[next] <= D[i]) {
                H[i].push_back(next);
                H[next].push_back(i);
                C[idx] = D[i];
                done[i] = D[i];
                break;
            }
        }
        if (!done[i]) fail();
    }
    string S(N, '?');
    function<void(int, int)> recur = [&](int node, int t) {
        if (S[node] != '?') return;
        S[node] = "BW"[t];
        for (int next : H[node]) recur(next, !t);
    };
    for (int i = 0; i < N; i++) recur(i, 0);
    cout << S << '\n';
    for (auto c : C) {
        cout << (c == -1 ? (int)1e9 : c) << '\n';
    }
    return 0;
}
