// https://atcoder.jp/contests/abc171/tasks/abc171_d

use proconio::input;

fn main() {
    input! {
      n: i32,
      a_s: [usize; n],
      q: i32,
      bcs: [(usize, usize); q],
    }
    let mut acc = vec![0; 10001];
    for &a in a_s.iter() {
        acc[a] += 1;
    }
    for &(b, c) in bcs.iter() {
        let bb = acc[b];
        acc[c] += bb;
        acc[b] = 0;
        let sum = acc
            .iter()
            .enumerate()
            .fold(0_u32, |pre, (i, &a)| pre + i as u32 * a as u32);
        println!("{}", sum);
    }
}
