use proconio::input;
use std::collections::BTreeMap;


fn main() {
    input! {
        n: usize,
        v: [usize; n], // v_1 v_2 ... v_n
        q: usize,
        mat: [(usize,usize); q], // n*m行列
    }

    let mut map = BTreeMap::new();
    for &x in v.iter() {
        *map.entry(x).or_insert(0) += 1;
    }

    let mut sum = 0;
    for &x in v.iter() {
        sum += x;
    }

    for &(b,c) in mat.iter() {
        if let Some(&kosu) = map.get(&b) {
            if let Some(&kos) = map.get(&c) {
                map.insert(c,kosu+kos);
                sum = sum + c*kosu - b*kosu;
            } else {
                map.insert(c,kosu);
            sum = sum + c*kosu - b*kosu;
            }
        }
        println!("{}",sum)
    }

}
