use proconio::input;

fn main() {
    input! {
        n: usize,
        mut a: [u64; n],
        q: usize,
        bc: [(u64, u64); q],
    }
    for i in 0..q {
        let mut r = 0;
        for j in 0..n {
            if a[j] == bc[i].0 {
                a[j] = bc[i].1;
            }
            r += a[j];
        }
        println!("{}", r);
    }
}
