#include <bits/stdc++.h>
using namespace std;
#define all(x)   (x).begin(),(x).end()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define isYES(x) printf("%s\n",(x) ? "YES" : "NO")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define isYes(x) printf("%s\n",(x) ? "Yes" : "No")
#define isIn(x,y,h,w) (x >= 0 && x < h && y >= 0 && y < w)

#define int long long
//using ll = long long;
using P = pair<int,int>;

ostream &operator<<(ostream &os,const P &p){ return os << "(" << p.first << "," << p.second << ")"; }

template<class T> T &chmin(T &a,const T &b){ return a = min(a,b); }
template<class T> T &chmax(T &a,const T &b){ return a = max(a,b); }
 
const int INF=1e+18;
const double EPS=1e-9;
const int MOD=1000000007;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

struct edge{ int to,num; };

signed main(){
	int n,m,d[100010],c[200010];
	bool s[100010] = {},used[100010] = {},botch[100010] = {};
	vector<edge> G[100010];
	cin >> n >> m;
	for(int i = 0;i < n;i++) cin >> d[i];
	vector<P> vec;
	for(int i = 0;i < n;i++){
		vec.emplace_back(d[i],i);
	}
	for(int i = 0;i < m;i++){
		int u,v;
		cin >> u >> v; u--;v--;
		G[u].push_back({v,i});
		G[v].push_back({u,i});
	}
	sort(all(vec));
	for(P p : vec){
		int ds = p.first,v = p.second;
		botch[v] = true;
		for(edge e : G[v]){
			if(!used[e.to]) continue;
			if(botch[e.to]){
				s[v] = true;
				if(d[e.to] != d[v]){
					cout << -1 << endl;
					return 0;
				}
				c[e.num] = d[v];
				botch[e.to] = false;
			}else if(botch[v]){
				s[v] = !s[e.to];
				c[e.num] = d[v];
			}else c[e.num] = 1000000000;
			botch[v] = false;
		}
		used[v] = true;
	}
	for(int i = 0;i < n;i++) cout << (s[i] ? 'B' : 'W');
	cout << endl;
	for(int i = 0;i < m;i++) cout << c[i] << endl;
}