// Etavioxy
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
#include<cmath>
#define il inline
#define ll long long
#define rep(i,s,t) for(register int i=(s);i<=(t);i++)
#define rev_rep(i,s,t) for(register int i=(s);i>=(t);i--)
#define each(i,u) for(int i=head[u];i;i=bow[i].nxt)
#define file(s) freopen(s".in" ,"r",stdin),freopen(s".out","w",stdout)
#define pt(x) putchar(x)
using namespace std;
il int ci(){
	register char ch;int f=1;
	while(!isdigit(ch=getchar()))f=ch=='-'?-1:1;
	register int x=ch^'0';
	while(isdigit(ch=getchar()))x=(x*10)+(ch^'0');
	return f*x;
}

enum{N=200023};

class UNI{
public:
	int fa[N];
	bool len[N];
	bool chk[N];
	il void init(int n){
		rep(i,1,n) fa[i]=i, len[i]=0;
	}
	int fd(int x){
		if( fa[x]==x ) return x;
		fd(fa[x]);
		len[x] ^= len[fa[x]];
		fa[x] = fa[fa[x]];
		return fa[x];
	}
	il int merge(int x,int y){
		chk[x]=1, chk[y]=1;
		if( fd(x)!=fd(y) ){
			fa[fa[x]]= fa[y];
			len[fa[x]]= len[x]^len[y]^1;
			return 0;
		}
		return -1;
	}
}uni;

struct Edge{ int nxt,to,id; } bow[N*4];
int head[N],tot_e;
il void add(int x,int y,int id){
//	printf("add %d %d\n",x,y);
	bow[++tot_e]=(Edge){head[x],y,id};
	head[x]=tot_e;
}

int d[N];
struct cmp1{
	bool operator()(int a,int b)const{ return d[a]<d[b]; }
};

int vis[N],ans[N];
void dfs(int u){
	each(i,u){
		int v=bow[i].to;
//		printf("%d -> %d\n",u,v);
		if( vis[v] ){
			ans[bow[i].id] = 1e9;
			continue;
		}
		vis[v] = vis[u];
		ans[bow[i].id] = d[v]-d[u];
		dfs(v);
	}
}

int main(){
	int n=ci(), m=ci();
	uni.init(n);
	rep(i,1,n) d[i]=ci();
	rep(i,1,m){
		int x=ci(), y=ci();
		if( d[x]!=d[y] ){
			if( d[x]>d[y] ) swap(x,y);
			add(x,y,i);
		}else{
			ans[i] = d[x];
//			printf("merge %d %d\n",x,y);
			if( uni.merge(x,y)==-1 ){
				ans[i] = 1e9;
			}
		}
	}
	rep(i,1,n) if( uni.chk[i] ) uni.fd(i), vis[i] = uni.len[i] ? 1:2 ;
	int tot_seq = 0;
	static int seq[N];
	rep(i,1,n) if( vis[i] ) seq[++tot_seq] = i;
	rep(i,1,tot_seq) dfs(seq[i]);
	bool fail = 0;
	rep(i,1,n) if( vis[i]==0 ) fail=1;
	if( fail ){
		puts("-1");
		return 0;
	}
	rep(i,1,n) pt(vis[i]==1 ? 'W':'B'); pt('\n');
	rep(i,1,m) printf("%d\n",ans[i]);
	return 0;
}
