use proconio::*;

fn main() {
    input! {
        n: usize,
        a: [i64; n],
        q: usize,
        bc: [i64; 2*q]
    }

    const MAX_VAL: usize = 10001;
    let mut counter: [i64; MAX_VAL] = [0; MAX_VAL];
    let mut sum: i64 = 0;
    for i in 0..n {
        counter[a[i] as usize] += 1;
        sum += a[i];
    }

    for i in 0..q {
        let b = bc[2 * i];
        let c = bc[2 * i + 1];

        sum += (c - b) * counter[b as usize];
        println!("{}", sum);

        counter[c as usize] += counter[b as usize];
        counter[b as usize] = 0;
    }
}
