macro_rules! scan_sub {
    ( $itr:ident, $t:ty ) => {
        let mut buf = String::new();
        let $itr = {
            while buf.trim().len() == 0 {
                buf = String::new();
                std::io::stdin().read_line(&mut buf).unwrap();
            }
            buf.split_whitespace().map(|n| n.parse::<$t>().unwrap())
        };
    };
}

macro_rules! scan {
    ( $x:ident; Vec<$t:ty> ) => {
        scan_sub!(itr, $t);
        let $x = itr.collect::<Vec<_>>();
    };
    ( $( $x:ident ),*; $t:ty ) => {
        scan_sub!(itr, $t);
        let mut itr = itr;
        $(
            let $x = itr.next().unwrap();
        )*
    };
}

use std::collections::HashMap;

fn main() {
    scan!(_n; usize);
    scan!(array; Vec<usize>);

    let mut book = HashMap::new();
    let mut sum: usize = 0;
    for &a in array.iter() {
        sum += a;
        let elm = book.entry(a).or_insert(0);
        *elm += 1;
    }

    scan!(q; usize);

    for _ in 0..q {
        scan!(b, c; usize);

        let b_num = match book.get(&b) {
            Some(v) => *v,
            None => {
                println!("{}", sum);
                continue;
            },
        };
        sum -= b_num * b;
        sum += b_num * c;
        let c_num = match book.get(&c) {
            Some(v) => *v,
            None => 0,
        };
        book.insert(c, b_num + c_num);

        println!("{}", sum);
    }
}
