#![allow(
    non_snake_case,
    unused_variables,
    unused_assignments,
    unused_mut,
    dead_code
)]
use proconio::input;
//use proconio::marker::Usize1;
//use proconio::marker::Chars;
//use std::collections::HashSet;
use std::cmp::*;
fn main() {
    input! {
        N:i32,
        A:[i32;N],
        Q:usize,
        BC:[[i32;2];Q]
    }
    let mut elements: Vec<i32> = vec![0; 100001];
    let mut s: Vec<i32> = vec![0; Q + 1];
    for a in A {
        elements[a as usize] += 1;
        s[0] += a;
    }
    let mut i = 0;
    for bc in BC {
        let t = elements[bc[0] as usize];
        elements[bc[0] as usize] = 0;
        elements[bc[1] as usize] += t;
        s[i + 1] = s[i] + t * (bc[1] - bc[0]);
        i += 1;
    }
    for j in 1..Q + 1 {
        println!("{}", s[j]);
    }
}
