// https://atcoder.jp/contests/keyence2020/tasks/keyence2020_e
//
#![allow(unused_imports)]
use std::io::*;
use std::io::Write;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };

    ($iter:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, [ next / $t:tt ]) => {
        {
            let len = read_value!($iter, usize);
            (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, switch) => {
        {
            let ty = read_value!($iter, i32);
            if ty == 1 {
                vec![ty, read_value!($iter, i32), read_value!($iter, i32)]
            } else if ty == 2 {
                vec![ty, read_value!($iter, i32)]
            } else {
                vec![ty, read_value!($iter, i32)]
            }
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

// ===

const INF: i64 = 1000000000;

fn solve(d: Vec<(i64, usize)>, graph: Vec<Vec<(usize, usize)>>, m: usize) -> Option<(Vec<char>, Vec<i64>)> {
    let n = graph.len();
    if d[0].0 != d[1].0 {
        return None;
    }

    let mut node_value = vec![0; n];
    for i in 0..n {
        node_value[d[i].1] = d[i].0;
    }

    let mut color = vec![' '; n];
    let mut edge_len = dvec![0; m];

    let root_value = d[0].0;
    let root_black = d[0].1;
    let root_white = d[1].1;
    color[root_black] = 'B';
    color[root_white] = 'W';

    // debug!(root_white, root_black);

    let mut found_first = false;
    for &(to, eid) in &graph[root_black] {
        if to == root_white {
            found_first = true;
            edge_len[eid] = d[0].0;
            break;
        }
    }
    if !found_first {
        return None;
    }
    for i in 2..n {
        let val = d[i].0;
        let id = d[i].1;
        let mut best = (INF, 0);
        for &(to, eid) in &graph[id] {
            if val < node_value[to] {
                continue;
            }
            if best.0 >= node_value[to] {
                best = (node_value[to], to);
            }
        }
        if best.0 == INF {
            return None;
        }

        let mut ev = 0;
        let tt = best.1;
        if best.0 == val {
            ev = node_value[tt];
            if color[tt] == ' ' {
                color[tt] = 'B';
                color[id] = 'W';
            } else if color[tt] == 'B' {
                color[id] = 'W';
            } else {
                color[id] = 'B';
            }
        } else {
            ev = val - node_value[tt];
            color[id] = color[tt];
        }
        // debug!(id, best, val, ev);


        for &(to, eid) in &graph[id] {
            if best.1 == to {
                edge_len[eid] = ev;
            } else if val >= node_value[to] {
                edge_len[eid] = INF;
            }
        }
    }

    // debug!(edge_len);

    for i in 0..m {
        if edge_len[i] <= 0 {
            return None;
        }
    }
    Some((color, edge_len))
}

fn main() {
    input! {
        n: usize, m: usize,
        d: [i64; n],
        edges: [(usize1, usize1); m]
    };

    let mut g = vec![vec![]; n];
    for i in 0..m {
        let u = edges[i].0;
        let v = edges[i].1;
        g[u].push((v, i));
        g[v].push((u, i));
    }

    let mut d = d.into_iter().zip(0..n).collect::<Vec<_>>();
    d.sort();

    let out = stdout();
    let mut out = BufWriter::new(out.lock());

    if let Some(ans) = solve(d, g, m) {
        writeln!(out, "{}", join!(ans.0, ""));
        for i in 0..m {
            writeln!(out, "{}", ans.1[i]);
        }
    } else {
        writeln!(out, "-1");
    }
}
