#![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 mut 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(Vec::new())).push(i);
    }

    let mut anss = Vec::new();
    for (b, c) in bc {
        if map.contains_key(&b) {
            let v = map[&b].clone();
            for i in v {
                a[i] = c;
                (*map.entry(c).or_insert(Vec::new())).push(i);
            }
        }
        anss.push(a.iter().sum::<u64>());
    }

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