use std::io::*;
use std::str::FromStr;

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

fn main() {
    let n: i64 = read();
    let a: Vec<usize> = (0..n).map(|_| read()).collect();
    let q: i64 = read();
    let bc: Vec<(usize, usize)> = (0..q).map(|_| (read(), read())).collect();

    let mut sum: i64 = 0;
    const W: usize = 10000;
    let mut freq = vec![0; W];
    for s in a {
        freq[s] += 1;
        sum += s as i64;
    }

    for (b, c) in bc {
        sum += (c  as i64- b as i64) * freq[b];
        freq[c] += freq[b];
        freq[b] = 0;
        print!("{}\n", sum);
    }
}
