#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void *wmem;
char memarr[96000000];
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void wt_L(char a){
  putchar_unlocked(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    putchar_unlocked('-');
  }
  while(s--){
    putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(const char c[]){
  int i=0;
  for(i=0;c[i]!='\0';i++){
    putchar_unlocked(c[i]);
  }
}
template <class T> struct LHeap{
  int *hp;
  int *place;
  int size;
  T *val;
  void malloc(int N){
    hp = (int*)std::malloc(N*sizeof(int));
    place=(int*)std::malloc(N*sizeof(int));
    val=(T*)std::malloc(N*sizeof(T));
  }
  void walloc(int N, void **mem=&wmem){
    walloc1d(&hp, N, mem);
    walloc1d(&place, N, mem);
    walloc1d(&val, N, mem);
  }
  void free(){
    std::free(hp);
    std::free(place);
    std::free(val);
  }
  void init(int N){
    int i;
    size=0;
    for(i=(0);i<(N);i++){
      place[i]=-1;
    }
  }
  void up(int n){
    int m;
    while(n){
      m=(n-1)/2;
      if(val[hp[m]]<=val[hp[n]]){
        break;
      }
      swap(hp[m],hp[n]);
      swap(place[hp[m]],place[hp[n]]);
      n=m;
    }
  }
  void down(int n){
    int m;
    for(;;){
      m=2*n+1;
      if(m>=size){
        break;
      }
      if(m+1<size&&val[hp[m]]>val[hp[m+1]]){
        m++;
      }
      if(val[hp[m]]>=val[hp[n]]){
        break;
      }
      swap(hp[m],hp[n]);
      swap(place[hp[m]],place[hp[n]]);
      n=m;
    }
  }
  void change(int n, T v){
    T f = val[n];
    val[n] = v;
    if(place[n]==-1){
      place[n] = size;
      hp[size++] = n;
      up(place[n]);
    }
    else{
      if(f < v){
        down(place[n]);
      }
      else if(f > v){
        up(place[n]);
      }
    }
  }
  int pop(void){
    int res = hp[0];
    place[res] = -1;
    size--;
    if(size){
      hp[0]=hp[size];
      place[hp[0]]=0;
      down(0);
    }
    return res;
  }
}
;
struct graph{
  int N;
  int *es;
  int **edge;
  void setEdge(int N__, int M, int A[], int B[], void **mem = &wmem){
    int i;
    N = N__;
    walloc1d(&es, N, mem);
    walloc1d(&edge, N, mem);
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      es[A[i]]++;
      es[B[i]]++;
    }
    for(i=(0);i<(N);i++){
      walloc1d(&edge[i], es[i], mem);
    }
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      edge[A[i]][es[A[i]]++] = B[i];
      edge[B[i]][es[B[i]]++] = A[i];
    }
  }
}
;
template<class T> struct wgraph{
  int N;
  int *es;
  int **edge;
  T **cost;
  graph g;
  void setEdge(int N__, int M, int A[], int B[], T C[], void **mem = &wmem){
    int i;
    N = N__;
    walloc1d(&es, N, mem);
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      es[A[i]]++;
      es[B[i]]++;
    }
    walloc1d(&edge, N, mem);
    for(i=(0);i<(N);i++){
      walloc1d(&edge[i], es[i], mem);
    }
    walloc1d(&cost, N, mem);
    for(i=(0);i<(N);i++){
      walloc1d(&cost[i], es[i], mem);
    }
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      edge[A[i]][es[A[i]]] = B[i];
      edge[B[i]][es[B[i]]] = A[i];
      cost[A[i]][es[A[i]]++] = C[i];
      cost[B[i]][es[B[i]]++] = C[i];
    }
    g.N = N;
    g.es = es;
    g.edge = edge;
  }
}
;
int N;
int M;
int D[100000];
int A[200000];
int B[200000];
int ind[200000];
char S[100000+2];
int C[200000];
int fg[100000];
char rev[256];
int main(){
  wmem = memarr;
  int i;
  int j;
  int k;
  int x;
  wgraph<int> g;
  LHeap<int> hp;
  rev['W'] = 'B';
  rev['B'] = 'W';
  rd(N);
  rd(M);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
      rd((D[Lj4PdHRW]));
    }
  }
  {
    int e98WHCEY;
    for(e98WHCEY=(0);e98WHCEY<(M);e98WHCEY++){
      rd(A[e98WHCEY]);A[e98WHCEY] += (-1);
      rd(B[e98WHCEY]);B[e98WHCEY] += (-1);
    }
  }
  for(i=(0);i<(M);i++){
    ind[i] = i;
  }
  g.setEdge(N,M,A,B,ind);
  hp.walloc(M);
  hp.init(M);
  for(i=(0);i<(M);i++){
    hp.change(i, 1073709056);
  }
  for(i=(0);i<(M);i++){
    if(D[A[i]] == D[B[i]]){
      hp.change(i, D[A[i]]);
    }
  }
  while(hp.size){
    int loop;
    k = hp.pop();
    if(hp.val[k]==1073709056){
      wt_L(-1);
      wt_L('\n');
      return 0;
    }
    if(S[A[k]] == 0 && S[B[k]] == 0){
      S[A[k]] = 'W';
    }
    if(S[A[k]] == 0){
      S[A[k]] = rev[S[B[k]]];
    }
    if(S[B[k]] == 0){
      S[B[k]] = rev[S[A[k]]];
    }
    C[k] = hp.val[k];
    for(loop=(0);loop<(2);loop++){
      if(loop==0){
        i =A[k];
      }
      else{
        i =B[k];
      }
      if(fg[i]++){
        continue;
      }
      for(j=(0);j<(g.es[i]);j++){
        x = g.edge[i][j];
        if(S[x]==0 && D[x] >= D[i]){
          hp.change(g.cost[i][j], D[x]);
        }
      }
    }
  }
  wt_L(S);
  wt_L('\n');
  {
    int jZyWAPpY;
    for(jZyWAPpY=(0);jZyWAPpY<(M);jZyWAPpY++){
      wt_L(C[jZyWAPpY]);
      wt_L('\n');
    }
  }
  return 0;
}
// cLay varsion 20200119-1

// --- original code ---
// int N, M, D[1d5], A[2d5], B[2d5], ind[2d5];
// char S[1d5+2];
// int C[2d5];
// int fg[1d5];
// char rev[256];
// {
//   int i, j, k, x;
//   wgraph<int> g;
//   LHeap<int> hp;
// 
//   rev['W'] = 'B';
//   rev['B'] = 'W';
// 
//   rd(N,M,(D(N)),(A--,B--)(M));
//   rep(i,M) ind[i] = i;
//   g.setEdge(N,M,A,B,ind);
// 
//   hp.walloc(M);
//   hp.init(M);
//   rep(i,M) hp.change(i, int_inf);
//   rep(i,M) if(D[A[i]] == D[B[i]]) hp.change(i, D[A[i]]);
// 
//   while(hp.size){
//     k = hp.pop();
//     if(hp.val[k]==int_inf) wt(-1), return 0;
// 
//     if(S[A[k]] == 0 && S[B[k]] == 0) S[A[k]] = 'W';
//     if(S[A[k]] == 0) S[A[k]] = rev[S[B[k]]];
//     if(S[B[k]] == 0) S[B[k]] = rev[S[A[k]]];
//     C[k] = hp.val[k];
// 
//     rep(loop,2){
//       i = if[loop==0, A[k], B[k]];
//       if(fg[i]++) continue;
//       rep(j,g.es[i]){
//         x = g.edge[i][j];
//         if(S[x]==0 && D[x] >= D[i]) hp.change(g.cost[i][j], D[x]);
//       }
//     }
//   }
//   wtLn(S,C(M));
// }


Measured memory_bytes: 16640