#![allow(unused_imports)]
#![allow(unused_variables)]

use itertools::Itertools;
use proconio::{fastout, input};
use std::collections::HashMap;

#[fastout]
fn main() {
    input! {
        n: usize,
        a: [u64; n],
        q: usize,
        bc: [(u64, u64); q],
    }
    // annotate for rust-analyzer
    let n: usize = n;
    let a: Vec<u64> = a;
    let q: usize = q;
    let bc: Vec<(u64, u64)> = bc;

    let mut map = HashMap::new();
    for i in 0..n {
        *map.entry(a[i]).or_insert(0) += 1;
    }

    let mut anss = Vec::new();
    for (b, c) in bc {
        if map.contains_key(&b) {
            *map.entry(c).or_insert(0) += map[&b];
            *map.get_mut(&b).unwrap() = 0;
        }
        anss.push(map.iter().map(|(&k, &v)| k * v).sum::<u64>());
    }

    for ans in anss {
        println!("{}", ans);
    }
}
