//https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より
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)*}
    };
}

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)*}
    };
}

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, 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")
    };
}

//

fn run() {
    input! {
        n: usize,
        m: usize,
        d: [usize; n],
        e: [(usize1, usize1); m],
    }
    let mut a = vec![];
    for i in 0..n {
        a.push((d[i], i));
    }
    a.sort();
    let mut g = vec![vec![]; n];
    for i in 0..m {
        let (a, b) = e[i];
        g[a].push((b, i));
        g[b].push((a, i));
    }
    let inf = 1_000_000_000usize;
    let mut depth = vec![inf; n];
    let mut color = vec![2; n];
    let mut weight = vec![inf; m];
    for &(w, v) in a.iter() {
        if depth[v] < inf {
            continue;
        }
        let mut ok = false;
        for &(u, k) in g[v].iter() {
            if depth[u] < w {
                ok = true;
                color[v] = color[u];
                depth[v] = w;
                weight[k] = w - depth[u];
                break;
            }
        }
        if ok {
            continue;
        }
        for &(u, k) in g[v].iter() {
            if w == d[u] {
                if color[u] < 2 {
                    color[v] = color[u] ^ 1;
                } else {
                    color[v] = 0;
                    color[u] = 1;
                }
                depth[u] = w;
                depth[v] = w;
                weight[k] = w;
                ok = true;
                break;
            }
        }
        if !ok {
            println!("-1");
            return;
        }
    }
    let mut out = String::new();
    for i in 0..n {
        let c = if color[i] == 0 {'B'} else {'W'};
        out.push(c);
    }
    out.push('\n');
    for i in 0..m {
        let w = if weight[i] == inf {1_000_000_000} else {weight[i]};
        out.push_str(&format!("{}\n", w));
    }
    print!("{}", out);
}

fn main() {
    run();
}
