#[allow(unused_imports)]
use proconio::{
    fastout, input,
};

#[fastout]
fn main() {
    input! {
        n: usize,
        a: [i32; n],
        q: usize,
        bc: [(i32, i32); q],
    }
    let mut counter = std::collections::HashMap::new();
    for &v in a.iter() {
        let e = counter.entry(v).or_insert(0);
        *e += 1;
    }
    let mut sum = a.iter().map(|&x| x as i64).sum::<i64>();
    for &(b, c) in bc.iter() {
        let increase = {
            let e = counter.entry(b).or_insert(0);
            sum += ((c - b) as i64) * (*e as i64);
            *e
        };
        let e = counter.entry(c).or_insert(0);
        *e += increase;
        println!("{}", sum);
    }
}
