use proconio::input;

fn d() {
    input! {
        n: usize,
        mut a: [i64; n],
        q: usize,
        mut bc: [(i32, i32); q],
    }
    let mut num = vec![0; 10_i32.pow(5) as usize];
    for i in &a {
        num[*i as usize] += 1;
    }
    let mut total: i64 = a.iter().sum();

    for (b, c) in bc {
        let bi = b as usize;
        let ci = c as usize;
        total += ((c - b) * num[bi]) as i64;
        num[ci] += num[bi];
        num[bi] = 0;
        println!("{}", total);
    }
}

fn main() {
    d();
}