#include <bits/stdc++.h>
using namespace std;
template<typename T = int> vector<T> create(size_t n){ return vector<T>(n); }
template<typename T, typename... Args> auto create(size_t n, Args... args){ return vector<decltype(create<T>(args...))>(n, create<T>(args...)); }
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n, m; cin >> n >> m;
	vector<int> u(m), v(m), w(m, 1'000'000'000), d(n), cr(n, 0);
	vector<vector<int>> to(n);
	for(int i = 0; i < n; i++) cin >> d[i];
	int mi = *min_element(d.begin(), d.end());
	int good = -1;
	for(int i = 0; i < m; i++){
		cin >> u[i] >> v[i]; u[i]--; v[i]--;
		to[u[i]].push_back(i);
		to[v[i]].push_back(i);
		if(d[u[i]] == d[v[i]] && d[u[i]] == mi){
			good = i;
		}
	}
	if(good == -1){
		cout << -1 << endl;
		return 0;
	}
	for(int i = 0; i < n; i++){
		sort(to[i].begin(), to[i].end(), [&](int a, int b){
			return (a == good) > (b == good);
		});
	}
	vector<int> vis(n, 0);
	function<void(int)> dfs = [&](int r){
		vis[r] = 1;
		for(int eid : to[r]){
			int nxt = u[eid] ^ v[eid] ^ r;
			if(!vis[nxt]){
				w[eid] = d[nxt];
				cr[nxt] = cr[r] ^ 1;
				dfs(nxt);
			}
		}
	};
	dfs(u[good]);
	for(int i = 0; i < n; i++) cout << (cr[i] ? 'B' : 'W'); cout << endl;
	for(int i = 0; i < m; i++){
		cout << w[i] << '\n';
	}
	return 0;
}
