use input::Input;
use std::collections::HashMap;

fn main() {
    let mut input = Input::default();

    let mut m = HashMap::new();

    let n = input.parse();
    for _ in 0..n {
        let a = input.parse::<u64>();
        let e = m.entry(a).or_insert(0u64);
        *e += 1;
    }

    let q = input.parse();
    for _ in 0..q {
        let b = input.parse();
        let c = input.parse();
        if let Some(x) = m.remove(&b) {
            let e = m.entry(c).or_insert(0u64);
            *e += x;
        }
        let mut ans = 0u64;
        for (k, v) in m.iter() {
            ans += k * v;
        }
        println!("{}", ans);
    }
}

#[allow(dead_code)]
pub mod input {
    #[derive(Default)]
    pub struct Input(Vec<String>);

    impl Input {
        pub fn read_line(&mut self) -> String {
            let mut s = String::new();
            std::io::stdin().read_line(&mut s).ok().expect("read error");
            s
        }
        pub fn parse<T: std::str::FromStr>(&mut self) -> T {
            let x = if let Some(x) = self.0.pop() {
                x
            } else {
                let s = self.read_line();
                self.0 = s.split_whitespace().map(String::from).rev().collect();
                self.0.pop().expect("reached EOF")
            };
            x.parse().ok().expect("parse error")
        }
    }
}
