#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}
#define MAX_N 200005

vector<P> G[MAX_N];

int main() {
    int n, m;
    cin >> n >> m;
    int d[n];
    rep(i,n)cin >> d[i];
    int a[m];
    rep(i,m)a[i] = 1000000000;
    int x[n] = {};
    int u[m], v[m];
    rep(i,m){
        cin >> u[i] >> v[i];
        u[i]--; v[i]--;
        G[u[i]].push_back(P(v[i], i));
        G[v[i]].push_back(P(u[i], i));
    }

    // 座標圧縮
    vector<int> b, pre_b, z;
    rep(i,n)pre_b.push_back(d[i]);
    sort(pre_b.begin(), pre_b.end());
    rep(i,n){
        if(i == 0){
            b.push_back(pre_b[i]);
        }else{
            if(pre_b[i] != pre_b[i-1]){
                b.push_back(pre_b[i]);
            }
        }
    }
    rep(i,n){
        int tmp_z = lower_bound(b.begin(), b.end(), d[i]) - b.begin() + 1;
        z.push_back(tmp_z);
    }

    int f[n] = {};
    int flag = 1;
    int cnt = 0;
    queue<int> que;
    rep(i,m){
        if(z[u[i]] == 1 && z[v[i]] == 1){
            flag = 0;
            f[u[i]] = 1; f[v[i]] = 1;
            x[u[i]] = 0; x[v[i]] = 1;
            a[i] = d[u[i]];
            que.push(u[i]);
            que.push(v[i]);
            cnt = 2;
            break;
        }
    }

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

    while(true){
        if(cnt == n)break;
        while(que.size() > 0){
            int y = que.front();
            que.pop();
            rep(i, G[y].size()){
                if(f[G[y][i].first] == 0 && d[G[y][i].first] >= d[y]){
                    f[G[y][i].first] = 1;
                    x[G[y][i].first] = (x[y] + 1) % 2;
                    a[G[y][i].second] = d[G[y][i].first];
                    que.push(G[y][i].first);
                    cnt++;
                }
            }
        }
        if(cnt == n)break;
        flag = 1;
        rep(i,m){
            if(f[u[i]] == 0 && f[v[i]] == 0 && z[u[i]] == z[v[i]]){
                flag = 0;
                f[u[i]] = 1; f[v[i]] = 1;
                x[u[i]] = 0; x[v[i]] = 1;
                a[i] = d[u[i]];
                que.push(u[i]);
                que.push(v[i]);
                cnt += 2;
                break;
            }
        }
        if(flag){
            cout << -1 << endl;
            return 0;
        }
    }
    
    rep(i,n){
        if(x[i] == 0)cout << 'B';
        else cout << 'W';
    }
    cout << endl;
    rep(i,m)cout << a[i] << endl;

    return 0;
}
 
 
