#include <iostream>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <string>
#include <sstream>
#include <algorithm>
#include <random>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <cassert>
#include <climits>
#include <bitset>
#include <functional>
#include <iomanip>
#include <random>


#define FOR_LT(i, beg, end) for (int i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (int i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (int i = beg; end <= i; i--)

using namespace std;

struct P {
	int  i;
	int  s;
};

struct E {
	int a;
	int i;
};

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout << fixed << setprecision(20);

	int n, m; cin >> n >> m;
	vector<P> ps(n + 1);
	FOR_LE(i, 1, n) {
		ps[i].i = i;
		cin >> ps[i].s;
	}
	sort(ps.begin(), ps.end(), [](const auto& lhs, const auto& rhs) {
		return lhs.s < rhs.s;
	});

	vector<vector<E>> es(n + 1);
	FOR_LT(i, 0, m) {
		int a, b; cin >> a >> b;
		es[a].push_back({ b, i });
		es[b].push_back({ a,i });
	}

	if (ps[1].s != ps[2].s) {
		cout << -1 << endl;
		return 0;
	}

	vector<int> ess(m);
	vector<bool> pbs(n + 1);
	vector<bool> us(n + 1);

	pbs[ps[1].i] = false;
	us[ps[1].i] = true;
	queue<E> s;
	for (auto& e : es[ps[1].i]) {
		s.push(e);
	}

	FOR_LE(i, 2, n) {
		E ne;
		while (true) {
			ne = s.front();
			s.pop();
			if (!us[ne.a]) {
				break;
			}
		}
		P& np = ps[ne.a];

		ess[ne.i] = np.s;

		pbs[np.i] = !pbs[np.i];
		for (auto& e : es[ps[i].i]) {
			s.push(e);
		}
		us[np.i] = true;
	}

	FOR_LE(i, 1, n) {
		cout << (pbs[i] ? "B" : "W");
	}
	cout << endl;
	FOR_LT(i, 0, m) {
		cout << ((ess[i] != 0) ? ess[i] : 1000000000) << endl;
	}

	return 0;
}