#include<bits/stdc++.h>
typedef long long ll;
ll gi(){
	ll x=0,f=1;
	char ch=getchar();
	while(!isdigit(ch))f^=ch=='-',ch=getchar();
	while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
	return f?x:-x;
}
std::mt19937 rnd(time(NULL));
#define rand rnd
#define pr std::pair<int,int>
#define fi first
#define se second
template<class T>void cxk(T&a,T b){a=a>b?a:b;}
template<class T>void cnk(T&a,T b){a=a<b?a:b;}
#ifdef mod
int pow(int x,int y){
	int ret=1;
	while(y){
		if(y&1)ret=1ll*ret*x%mod;
		x=1ll*x*x%mod;y>>=1;
	}
	return ret;
}
template<class Ta,class Tb>void inc(Ta&a,Tb b){a=a+b>=mod?a+b-mod:a+b;}
template<class Ta,class Tb>void dec(Ta&a,Tb b){a=a>=b?a-b:a+mod-b;}
#endif
int fir[100010],dis[400010],nxt[400010],id=1;
void link(int a,int b){nxt[++id]=fir[a],fir[a]=id,dis[id]=b;}
std::vector<int>G[100010];
int FA[100010],fw[100010],D[100010];
int col[100010],ans[200010];
void dfs(int x){
	for(int i:G[x]){
		ans[fw[i]]=D[i];
		if(!col[i])col[i]=3-col[x],dfs(i);
	}
}
int main(){
#ifdef LOCAL
	freopen("in.in","r",stdin);
	//freopen("out.out","w",stdout);
#endif
	int n=gi(),m=gi(),u,v;
	for(int i=1;i<=n;++i)D[i]=gi();
	for(int i=1;i<=m;++i)u=gi(),v=gi(),link(u,v),link(v,u),ans[i]=1000000000;
	D[0]=2e9;
	for(int x=1;x<=n;++x){
		int o=0,p;
		for(int i=fir[x];i;i=nxt[i])if(D[dis[i]]<D[o]||(D[dis[i]]==D[o]&&dis[i]<o))o=dis[i],p=i>>1;
		if(D[o]>D[x])puts("-1"),exit(0);
		G[FA[x]=o].push_back(x);fw[x]=p;
	}
	for(int i=1;i<=n;++i)if(FA[FA[i]]==i&&i<FA[i])col[i]=1,dfs(i);
	for(int i=1;i<=n;++i)putchar(col[i]==1?'B':'W');puts("");
	for(int i=1;i<=m;++i)printf("%d\n",ans[i]);
	return 0;
}
