#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;

int N,M;
vector<ll> D;
vector<int> g[100010];

const ll INF=1e15;
int col[100010];
const ll MA=1e9;

void dfs(int now,int c){
    col[now]=c;
    for(auto nex:g[now]){
        if(col[nex]==0) dfs(nex,-c);
    }
}

int main(){
  cin>>N>>M;
  D.resize(N);
  rep(i,N) cin>>D[i];
  map<pair<int,int>,int> mp;
  vector<vector<int>> v(N);
  for(int i=0;i<M;i++){
    int a,b;
    cin>>a>>b;
    a--;b--;
    if(a>b) swap(a,b);
    mp[mkp(a,b)]=i;
    v[a].push_back(b);
    v[b].push_back(a);
  }

  vector<int> ans(M,0);
  for(int i=0;i<N;i++){
      pair<ll,int> mi=mkp(INF,0);
      for(auto nex:v[i]){
          if(D[nex]>D[i]) continue;
          mi=min(mi,mkp(D[nex],nex));
      }
      if(mi.first==INF){
          cout<<-1<<endl;
          return 0;
      }
      int a=min(i,mi.second),b=max(i,mi.second);
      if(a>b) swap(a,b);
      ans[mp[mkp(a,b)]]=D[i];
      g[a].push_back(b);
      g[b].push_back(a);
  }

  for(int i=0;i<N;i++){
      if(col[i]==0) dfs(i,1);
  }

  for(int i=0;i<N;i++){
    if(col[i]==1) cout<<'B';
    else cout<<'W';
  }
  cout<<endl;
  for(int i=0;i<M;i++){
      if(ans[i]==0) cout<<MA<<endl;
      else cout<<ans[i]<<endl;
  }

  return 0;
}
