#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define ld long double
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
template <typename T> using ord_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N = 2e5 + 123;
int n, m, d[N], used[N], c[N], good[N];
array <int, 3> e[N];
vector <int> g[N];

void dfs(int v) {
	used[v] = 1;
	for (int i : g[v]) {
		int to = e[i][0] ^ e[i][1] ^ v;
		if (used[to]) continue;
		good[i] = 1;
		c[to] = c[v] ^ 1;
		dfs(to);
	}
}

int main() {
	ios_base::sync_with_stdio(false), cin.tie(NULL);
	#ifdef LOCAL
		freopen("input.txt", "r", stdin);
	#endif
	
	cin >> n >> m;
	for (int i = 1; i <= n; i++) cin >> d[i];
	for (int i = 1; i <= m; i++) {
		cin >> e[i][0] >> e[i][1];
		g[e[i][0]].pb(i);
		g[e[i][1]].pb(i);
	}
	for (int i = 1; i <= n; i++) {
		int ok = 0;
		for (int j : g[i]) {
			int to = e[j][0] ^ e[j][1] ^ i;
			if (d[to] <= d[i]) ok = 1;
		}
		if (!ok) return cout << -1, 0;
	}
	dfs(1);
	for (int i = 1; i <= n; i++) {
		if (c[i] == 0) cout << 'B';
		else cout << 'W';
	}
	cout << '\n';
	for (int i = 1; i <= m; i++) {
		if (good[i])
			cout << max(d[e[i][0]], d[e[i][1]]) << '\n';
		else cout << (int)1e9 << '\n';
	}
}
