use std::collections::HashMap;
use std::error::Error;
use std::io;

pub fn main() {
    if let Err(_e) = run() {
        panic!("failed.");
    }
}

fn run() -> Result<(), Box<dyn Error>> {
    let mut input = String::new();
    io::stdin().read_line(&mut input)?;
    input.clear();
    io::stdin().read_line(&mut input)?;
    let a_s: Vec<i32> = input
        .trim()
        .split_whitespace()
        .map(|s| s.parse::<i32>().unwrap())
        .collect();
    input.clear();

    let mut map: HashMap<i32, i32> = HashMap::new();

    let mut result = 0 as i64;
    for x in a_s {
        result += x as i64;
        let count = map.entry(x).or_insert(0);
        *count += 1;
    }

    io::stdin().read_line(&mut input)?;
    let q = input.trim().parse::<i32>()?;
    input.clear();
    for _ in 0..q {
        io::stdin().read_line(&mut input)?;
        let b_c: Vec<i32> = input
            .trim()
            .split_whitespace()
            .map(|s| s.parse::<i32>().unwrap())
            .collect();
        let b = b_c[0];
        let c = b_c[1];
        input.clear();
        let temp = c - b;
        if let Some(&count) = map.get(&b) {
            result += temp as i64 * count as i64;
            *map.entry(c).or_insert(0) += count;
        }
        println!("{}", result);
    }

    Ok(())
}
