use defaultmap::*;
use proconio::{fastout, input};
// use std::collections::HashMap;

#[fastout]
fn main() {
    input! {
        n: usize,
        mut a: [i64; n],
        q: usize,
    }
    a.sort();

    let mut b = vec![];
    let mut c = vec![];
    // let mut counter: HashMap<&i64, i64> = HashMap::new();
    let mut counter: DefaultHashMap<i64, i64> = DefaultHashMap::new(0);

    for _i in 0..q {
        input! {
            bi: i64,
            ci: i64,
        }
        b.push(bi);
        c.push(ci);
    }

    let mut asum: i64 = a.iter().sum();
    for i in 0..n {
        counter[a[i]] += 1;
        // (*counter.entry(&a[i]).or_insert(0)) += 1;
        // // if counter.get(&a[i]) != None {
        // //     counter.insert(&a[i], counter.get(&a[i]).unwrap() + 1);
        // // } else {
        // //     counter.insert(&a[i], 1);
        // // }
    }

    for i in 0..q {
        if counter[b[i]] != 0 {
            asum = asum - b[i] * counter[b[i]] + c[i] * counter[b[i]];
            counter[c[i]] += counter[b[i]];
            counter[b[i]] = 0;
        }
        println!("{}", asum);

        // let mut s = asum;

        // if counter.get(&b[i]) != None || counter.get(&b[i]).unwrap() == &0 {
        //     let num: i64 = counter[&b[i]];
        //     asum = asum - b[i] * num + c[i] * num;
        //     // counter[&b[i]] = 0;
        //     *counter.get_mut(&b[i]).unwrap() = 0;
        //     println!("num: {}", num);

        //     if counter.get(&c[i]) != None {
        //         // counter[&c[i]] = num;
        //         counter.insert(&c[i], num);
        //     } else {
        //         *counter.get_mut(&c[i]).unwrap() = num;
        //     }
        //     println!("{} {} {} {}", asum, num, counter[&b[i]], counter[&c[i]]);
        // }

        // println!("{}", asum);
    }
}
