#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define all(c) begin(c),end(c)

template<typename T1, typename T2>
inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }

template<typename T1, typename T2>
inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }

typedef long long int ll;
using ll = long long int;
using ull = long long unsigned int;
using Int = long long int;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007


//edit
class Solve {
public:
    void solve() {
        Int N, M;
        cin >> N >> M;
        vector<Int> D(N);
        for (int i = 0; i < N; ++i) cin >> D[i];

        vector<pair<Int, Int>> edge(M);
        vector<vector<Int>> G(N);
        map<pair<Int, Int>, Int> eid;
        for (int i = 0; i < M; ++i) {
            int u, v;
            cin >> u >> v;
            u--, v--;
            edge[i] = {u, v};

            G[u].push_back(v);
            G[v].push_back(u);
            eid[make_pair(u, v)] = i;
            eid[make_pair(v, u)] = i;
        }

        vector<pair<Int, Int>> dv(N);
        for (int i = 0; i < N; ++i) {
            dv[i] = {D[i], i};
        }

//        sort(all(D));

        vector<bool> used(N, false);
        vector<Int> wb(N, -1);
        vector<Int> C(M, 1000000000);

        using P = pair<Int, Int>;
        priority_queue<P, vector<P>, greater<>> starts;
        for (int i = 0; i < N; ++i) {
            starts.emplace(D[i], i);
        }

        while (!starts.empty()) {
            while (!starts.empty() && used[starts.top().second]) {
                starts.pop();
            }
            if (starts.empty()) break;

            int r = starts.top().second;
            starts.pop();
            //rをrootとしたrooted tree

            queue<int> que;
            que.push(r);
            wb[r] = 0;
            used[r] = true;

            bool flag = false;
            for (auto e : G[r]) {
                if (D[r] == D[e]) {
                    flag = true;
                }
            }

            if (!flag) {
                cout << -1 << endl;
                return;
            }

            while (!que.empty()) {
                int top = que.front();
                que.pop();

                for (auto e : G[top]) {
                    if (!used[e] && D[top] <= D[e]) {
                        que.push(e);

                        //top, e間に色を塗る
                        wb[e] = !wb[top];
                        Int id = eid[make_pair(e, top)];
                        C[id] = max(D[top], D[e]);
                        used[e] = true;
                    }
                }
            }
        }

        string s;
        for (auto e : wb) {
            if (e) s += 'W';
            else s += 'B';
        }

        cout << s << endl;

        for (auto e : C) {
            cout << e << endl;
        }




//        for (int i = 0; i < M; ++i) {
//            if (D[edge[i].first] == D[edge[i].second]) {
//                wb[edge[i].first] = 0;
//                wb[edge[i].second] = 1;
//                C[i] = D[edge[i].first];
//                for (auto e : G[edge[i].first]) {
//                    pq.emplace(C[i], e);
//                }
//            }
//        }


    }
};


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    Solve().solve();


    return 0;
}


Measured memory_bytes: 45040