#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define repeat(i, n) for (int i = 0; (i) < (n); ++(i))
#define repeat_from(i, m, n) for (int i = (m); (i) < (n); ++(i))
using namespace std;
template <class T>
void setmax(T &a, T const &b) {
    if (a < b) a = b;
}

template <class T>
void setmin(T &a, T const &b) {
    if (a > b) a = b;
}

template <typename T, typename X>
auto vectors(T a, X x) { return vector<T>(x, a); }

template <typename T, typename X, typename Y, typename... Zs>
auto vectors(T a, X x, Y y, Zs... zs) {
    auto cont = vectors(a, y, zs...);
    return vector<decltype(cont)>(x, cont);
}

int main() {
    int N, M;
    cin >> N >> M;
    vector<int> D(N);
    repeat(i, N) cin >> D[i];
    auto G = vectors(-1, N, 0);
    vector<pair<int, int>> Edge(M);
    repeat(i, M) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        G[u].push_back(v);
        G[v].push_back(u);
        Edge[i] = make_pair(u, v);
    }
    auto newG = vectors(make_pair(0, 0), N, 0);
    repeat(v, N) {
        int edge_to = -1;
        int minD = INT32_MAX;
        for (int to : G[v]) {
            if (D[to] < minD) {
                minD = D[to], edge_to = to;
            }
        }
        if (edge_to == -1 || minD > D[v]) {
            cout << -1 << endl;
            return 0;
        }
        newG[v].push_back(make_pair(edge_to, D[v]));
        newG[edge_to].push_back(make_pair(v, D[v]));
    }

    /*     repeat(i, N) {
        for (auto e : newG[i]) {
            cout << i << " -> " << e.first << " : " << e.second << endl;
        }
    }
 */
    vector<int> BW(N, -1);
    auto dfs = [&](auto f, int cur, int col) -> bool {
        BW[cur] = col;
        for (auto e : newG[cur]) {
            int to = e.first;
            if (BW[to] != -1) {
                if (BW[to] == col)
                    return false;
                continue;
            }
            if (!f(f, to, 1 - col)) return false;
        }
        return true;
    };

    if (!dfs(dfs, 0, 0)) {
        //cout << "cannot make 2bu graph" << endl;
        cout << -1 << endl;
        return 0;
    }

    vector<int> C(M);
    repeat(i, M) {
        int u = Edge[i].first, v = Edge[i].second;
        for (auto e : newG[u]) {
            if (e.first == v) {
                C[i] = e.second;
                break;
            }
        }
    }

    repeat(i, N) {
        if (BW[i])
            cout << "B";
        else
            cout << "W";
    }
    cout << endl;
    repeat(i, M) {
        if (C[i] != 0) {
            cout << C[i] << endl;
        } else {
            cout << 1000000000 << endl;
        }
    }

    return 0;
}