use std::io::*;
use std::str::FromStr;
use std::cmp;
use std::collections::HashMap;

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char) 
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

fn read_tuple2<T: FromStr, S: FromStr>() -> (T, S) {
    let a = read();
    let b = read();
    (a, b)
}



fn main() {
    let n:i32 = read();
    let ax:Vec<i32> = (0..n).map(|_|read()).collect();
    let q:i32 = read();
    let bcs:Vec<(i32, i32)> = (0..q).map(|_|read_tuple2()).collect();
    let mut counter = HashMap::new();
    let mut sum:i32 = ax.iter().sum();
    for a in ax {
        let count = counter.entry(a).or_insert(0);
        *count += 1;
    }
    for (b, c) in bcs {
        let num = counter.get(&b);
        if let Some(n) = num {
            let n = *n;
            sum += (c - b) * n;
            println!("{}", sum);
            counter.insert(b, 0);
            let c = counter.entry(c).or_insert(0);
            *c += n;
        } else {
            println!("{}", sum);
        }
    }
}
