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 :$base :expr } ) =>{read_value !($iter ,String ) .chars () .map (|c |(c as u8 -$base as u8 ) as usize ) .collect ::<Vec <usize >>() } ;($iter :expr ,{char :$base :expr } ) =>{read_value !($iter ,{chars :$base } ) [0 ] } ;($iter :expr ,chars ) =>{read_value !($iter ,String ) .chars () .collect ::<Vec <char >>() } ;($iter :expr ,char ) =>{read_value !($iter ,chars ) [0 ] } ;($iter :expr ,usize1 ) =>{read_value !($iter ,usize ) -1 } ;($iter :expr ,$t :ty ) =>{$iter .next () .unwrap () .parse ::<$t >() .unwrap () } ;}
macro_rules !input_inner {($iter :expr ) =>{} ;($iter :expr ,) =>{} ;($iter :expr ,mut $var :ident :$t :tt $($r :tt ) *) =>{let mut $var =read_value !($iter ,$t ) ;input_inner !{$iter $($r ) *} } ;($iter :expr ,mut $var :ident $($r :tt ) *) =>{input_inner !{$iter ,mut $var :usize $($r ) *} } ;($iter :expr ,$var :ident :$t :tt $($r :tt ) *) =>{let $var =read_value !($iter ,$t ) ;input_inner !{$iter $($r ) *} } ;($iter :expr ,$var :ident $($r :tt ) *) =>{input_inner !{$iter ,$var :usize $($r ) *} } ;}
macro_rules !input {(source =$s :expr ,$($r :tt ) *) =>{let mut iter =$s .split_whitespace () ;input_inner !{iter ,$($r ) *} } ;(iter =$iter :ident ,$($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 ) *} } ;($($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_imports)]
use std::io::Write;
pub fn with_bufwriter<F>(f: F) -> std::io::Result<()>
where
    F: FnOnce(&mut std::io::BufWriter<std::io::StdoutLock>) -> std::io::Result<()>,
{
    let stdout = std::io::stdout();
    let mut writer = std::io::BufWriter::new(stdout.lock());
    f(&mut writer)
}
fn fail() {
    println!("-1");
}
fn main() {
    input! { n, s:[usize;n], t:[usize;n], u:[usize;n], v:[usize;n] };
    const M: usize = std::usize::MAX;
    let mut ans = vec![vec![0; n]; n];
    for d in 0..64 {
        let b = 1 << d;
        let mut v1 = vec![];
        let mut v2 = vec![];
        for i in 0..n {
            if s[i] == 0 && u[i] & b != 0 {
                for j in 0..n {
                    ans[i][j] |= b;
                }
            } else if s[i] == 1 && u[i] & b == 0 {
            } else {
                v1.push(i);
            }
            if t[i] == 0 && v[i] & b != 0 {
                for j in 0..n {
                    ans[j][i] |= b;
                }
            } else if t[i] == 1 && v[i] & b == 0 {
            } else {
                v2.push(i);
            }
        }
        if std::cmp::min(v1.len(), v2.len()) > 0 {
            if v1.len() == 1 {
                if s[v1[0]] == 0 {
                    for &j in v2.iter() {
                        if t[j] == 1 {
                            ans[v1[0]][j] |= b;
                            for i in 0..n {
                                if i == v1[0] {
                                    continue;
                                }
                                if ans[i][j] & b != 0 {
                                    ans[v1[0]][j] &= !b;
                                    break;
                                }
                            }
                        }
                    }
                } else {
                    for &j in v2.iter() {
                        if t[j] == 0 {
                            for i in 0..n {
                                if i == v1[0] {
                                    continue;
                                }
                                if ans[i][j] & b == 0 {
                                    ans[v1[0]][j] |= b;
                                    break;
                                }
                            }
                        } else {
                            ans[v1[0]][j] |= b;
                        }
                    }
                }
            } else if v2.len() == 1 {
                if t[v2[0]] == 0 {
                    for &i in v1.iter() {
                        if s[i] == 1 {
                            ans[i][v2[0]] |= b;
                            for j in 0..n {
                                if j == v2[0] {
                                    continue;
                                }
                                if ans[i][j] & b != 0 {
                                    ans[i][v2[0]] &= !b;
                                    break;
                                }
                            }
                        }
                    }
                } else {
                    for &i in v1.iter() {
                        if s[i] == 0 {
                            for j in 0..n {
                                if j == v2[0] {
                                    continue;
                                }
                                if ans[i][j] & b == 0 {
                                    ans[i][v2[0]] |= b;
                                    break;
                                }
                            }
                        } else {
                            ans[i][v2[0]] |= b;
                        }
                    }
                }
            } else {
                for i in 0..std::cmp::max(v1.len(), v2.len()) {
                    ans[v1[i % v1.len()]][v2[i % v2.len()]] |= b;
                }
            }
        }
    }
    for i in 0..n {
        let mut and = M;
        let mut or = 0;
        for j in 0..n {
            and &= ans[i][j];
            or |= ans[i][j];
        }
        if s[i] == 0 {
            if u[i] != and {
                fail();
                return;
            }
        } else {
            if u[i] != or {
                fail();
                return;
            }
        }
        let mut and = M;
        let mut or = 0;
        for j in 0..n {
            and &= ans[j][i];
            or |= ans[j][i];
        }
        if t[i] == 0 {
            if v[i] != and {
                fail();
                return;
            }
        } else {
            if v[i] != or {
                fail();
                return;
            }
        }
    }
    with_bufwriter(|out| {
        for i in 0..n {
            for j in 0..n {
                write!(out, "{}{}", ans[i][j], if j + 1 == n { "\n" } else { " " })?;
            }
        }
        Ok(())
    })
    .unwrap();
}


Measured memory_bytes: 7420