#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 200000 + 10;
const int MOD = 1000000007;
const int LOG = 20;
const int INF = 1000000010;
const int delta = 11353;

int n, D[N], m, mark[N], ans[N], ind[N];
vector<pii> G[N];

bool cmp(int x, int y){
	return D[x] < D[y];
}

int32_t main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> m;
	memset(mark, -1, sizeof mark);
	for (int i = 1; i <= n; i++){
		ind[i] = i;
		cin >> D[i];
	}
	for (int i = 1; i <= m; i++){
		int v, u;
		cin >> v >> u;
		G[v].pb({u, i}), G[u].pb({v, i});
	}
	for (int i = 1; i <= n; i++){
		bool f = 0;
		for (auto u:G[i]){
			if (D[u.F] <= D[i]) f = 1;
		}
		if (!f) return cout << -1, 0;
	}
	sort(ind + 1, ind + n + 1, cmp);
	for (int i = 1; i <= n; i++){
		if (mark[ind[i]] != -1) continue;
		for (auto u:G[ind[i]]){
			if (D[u.F] <= D[ind[i]]){
				if (mark[u.F] == -1){
					mark[u.F] = 0;
					mark[ind[i]] = 1;
					ans[u.S] = D[ind[i]];
				}else{
					mark[ind[i]] = 1 - mark[u.F];
					ans[u.S] = D[ind[i]];
				}
			//	cout << "Fuck " << u.S << '\n';
				break;
			}
		}
	}
	for (int i = 1; i <= n; i++)cout << (mark[i]?'W':'B');
	cout << '\n';
	for (int i = 1; i <= m; i++){
		cout << (ans[i] == 0?1000000000:ans[i]) << '\n';
		//cout << ans[i] << '\n';
	}
	return 0;
}
