#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define F first
#define S second
//#define int long long
#define ll long long
//#define int  unsigned long long
#define pb push_back
#define double long double

using namespace std;
using namespace __gnu_pbds;
typedef  tree< int , null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int N = 55;
const int K = 19;
int used[N], c[N], d[N], ans[N], u[N], v[N], in[N], deg[N];
vector < pair < int, pair < int, int > > > g[N];
void dfs(int v, int color = 0){
    used[v] = 1;
    c[v] = color;
    for (auto i: g[v]){
        int u = i.F;
        if (used[u]) continue;
        if (i.S.S == 0) ans[i.S.F] = d[u] - d[v];
        dfs(u, color ^ i.S.S);
    }
}
main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("input.txt", "r", stdin);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> d[i];
    vector < pair < pair < int, int >, int > > e;
    for (int i = 1; i <= m; i++){
        cin >> u[i] >> v[i];
        if (d[u[i]] < d[v[i]]){
            g[u[i]].pb({v[i], {i, 0}});
            in[v[i]]++;
        }
        else
        if (d[v[i]] > d[u[i]]){
            g[v[i]].pb({u[i], {i, 0}});
            in[u[i]]++;
        }
        else{
            e.pb({{u[i], v[i]}, i});
            ans[i] = d[u[i]];
            deg[u[i]]++;
            deg[v[i]]++;
        }
    }
    for (int i = 1; i <= n; i++){
        if (!used[i] && in[i] == 0){
            if (deg[i] == 0){
                cout << -1;
                return 0;
            }
        }
    }
    for (auto i: e){
        if (in[i.F.S] == 0) g[i.F.F].pb({i.F.S, {i.S, 1}});
        if (in[i.F.F] == 0) g[i.F.S].pb({i.F.F, {i.S, 1}});
    }
    for (int i = 1; i <= n; i++) used[i] = 0;
    for (int i = 1; i <= n; i++){
        if (!used[i] && in[i] == 0){
            dfs(i);
        }
    }
    for (int i = 1; i <= n; i++){
        if (c[i] == 0) cout << 'W'; else cout << 'B';
    }
    cout << endl;
    for (int i = 1; i <= m; i++){
        if (ans[i] == 0) cout << 1000000000 << endl; else cout << ans[i] << endl;
    }
}
