const INF: i64 = 1e9 as i64;

fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());

    let n: usize = sc.read();
    let m: usize = sc.read();
    let d: Vec<i64> = sc.vec(n);
    let mut graph = vec![vec![]; n];
    for i in 0..m {
        let a = sc.read::<usize>() - 1;
        let b = sc.read::<usize>() - 1;
        graph[a].push((b, i));
        graph[b].push((a, i));
    }

    let mut v = (0..n).map(|i| (d[i], i)).collect::<Vec<_>>();
    v.sort();
    v.reverse();

    let mut color = vec![0; n];
    let mut edges = vec![INF; m];
    for (_, v) in v.into_iter() {
        if color[v] != 0 {
            continue;
        }
        color[v] = 1;
        dfs(v, &graph, &mut color, &mut edges, &d);
    }

    for i in 0..n {
        let min_cost = graph[i].iter().map(|&(_, edge)| edges[edge]).min().unwrap();
        if min_cost != d[i] {
            println!("-1");
            return;
        }
    }

    for i in 0..n {
        match color[i] {
            1 => sc.write("B"),
            2 => sc.write("W"),
            _ => unreachable!(),
        }
    }
    sc.write("\n");
    for i in 0..m {
        sc.write(edges[i]);
        sc.write("\n");
    }
}

fn dfs(
    v: usize,
    graph: &Vec<Vec<(usize, usize)>>,
    color: &mut Vec<usize>,
    edges: &mut Vec<i64>,
    d: &Vec<i64>,
) {
    let mut done = false;
    for &(next, edge_id) in graph[v].iter() {
        if color[next] != 0 {
            continue;
        }
        if d[next] > d[v] {
            continue;
        }

        edges[edge_id] = d[v];
        color[next] = if color[v] == 1 { 2 } else { 1 };
        dfs(next, graph, color, edges, d);
        done = true;
    }
    if done {
        return;
    }

    for &(next, edge_id) in graph[v].iter() {
        if color[next] == color[v] {
            continue;
        }
        if d[next] > d[v] {
            continue;
        }

        edges[edge_id] = d[v];
    }
}

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> Self {
        IO(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: ToString>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.to_string().as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}
