#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}

struct Hen {
	int x;
	int y;
	int id;
};

int main() {
	int n = ri();
	int m = ri();
	std::pair<int, int> all[n];
	for (int i = 0; i < n; i++) all[i].first = ri(), all[i].second = i;
	std::sort(all, all + n);
	
	std::vector<Hen> hen[n];
	for (int i = 0; i < m; i++) {
		int x = ri() - 1;
		int y = ri() - 1;
		hen[x].push_back({x, y, i});
		hen[y].push_back({y, x, i});
	}
	std::vector<int> cost(m, -1);
	std::vector<int> color(n, -1);
	bool ava[n];
	memset(ava, 0, sizeof(ava));
	int start = 0;
	for (int i = 0; i < n; i++) {
		if (i + 1 < n && all[i].first == all[i + 1].first) continue;
		// [start, i]
		std::set<int> newed;
		for (int j = start; j <= i; j++) ava[all[j].second] = true, newed.insert(all[j].second);
		for (int j = start; j <= i; j++) {
			int next = -1;
			int id = -1;
			for (auto k : hen[all[j].second]) {
				if (ava[k.y]) {
					next = k.y;
					id = k.id;
					break;
				}
			}
			// std::cerr << all[j].second << "->" << next << std::endl;
			if (color[all[j].second] != -1) continue;
			if (next == -1) {
				puts("-1");
				return 0;
			}
			if (cost[id] == -1) {
				cost[id] = all[i].first;
				if (color[next] == -1) color[next] = 0;
				color[all[j].second] = !color[next];
			} else assert(0);
		}
		
		start = i + 1;
	}
	for (auto &i : cost) if (i == -1) i = 1000000000;
	for (int i = 0; i < n; i++) {
		std::cout << (color[i] ? "B" : "W");
	}
	std::cout << std::endl;
	for (int i = 0; i < m; i++) std::cout << cost[i] << std::endl;
	
	return 0;
}
