Problem:
Score : 400 points

Problem Statement

You have a sequence A composed of N positive integers: A_{1}, A_{2}, \cdots, A_{N}.

You will now successively do the following Q operations:

In the i-th operation, you replace every element whose value is B_{i} with C_{i}.

For each i (1 \leq i \leq Q), find S_{i}: the sum of all elements in A just after the i-th operation.

Constraints

All values in input are integers.

1 \leq N, Q, A_{i}, B_{i}, C_{i} \leq 10^{5}

B_{i} \neq C_{i}

Input

Input is given from Standard Input in the following format:

N
A_{1} A_{2} \cdots A_{N}
Q
B_{1} C_{1}
B_{2} C_{2}
\vdots
B_{Q} C_{Q}

Output

Print Q integers S_{i} to Standard Output in the following format:

S_{1}
S_{2}
\vdots
S_{Q}

Note that S_{i} may not fit into a 32-bit integer.

Sample Input 1

4
1 2 3 4
3
1 2
3 4
2 4

Sample Output 1

11
12
16

Initially, the sequence A is 1,2,3,4.

After each operation, it becomes the following:

2, 2, 3, 4

2, 2, 4, 4

4, 4, 4, 4

Sample Input 2

4
1 1 1 1
3
1 2
2 1
3 5

Sample Output 2

8
4
4

Note that the sequence A may not contain an element whose value is B_{i}.

Sample Input 3

2
1 2
3
1 100
2 100
100 1000

Sample Output 3

102
200
2000

Solution in Rust (1.42.0) (judge verdict: Accepted)

use std::collections::HashMap;

fn main() {
    read::<String>();
    let a: Vec<i64> = read_vec();
    let q: i64 = read();
    let mut changes: Vec<(i64, i64)> = Vec::new();
    for _ in 0..q {
        let input: Vec<i64> = read_vec();
        changes.push((input[0], input[1]));
    }

    solve(&a, &changes).iter().for_each(|s| println!("{}", s));
}

fn read<T: std::str::FromStr>() -> T {
    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf).ok();
    buf.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|s| s.parse().ok().unwrap())
        .collect()
}

fn solve(a: &[i64], changes: &[(i64, i64)]) -> Vec<i64> {
    // 数列Aの中に含まれている数値ごとの個数をカウントし、初期値とする。
    // 例えば A = 1, 1, 2, 2, 3 であれば
    //   C = { 1: 2, 2: 2, 3: 1 }
    // とする。
    // この時、Aの和Sは
    //   S = Σ(k) {k * C(k)}
    // で求められる。
    //
    // Aに含まれる一部の数値aを別の数値bに入れ替えるということは、入れ替えた後の
    // SをS'とすれば
    //   S' = S - a*C(a) + b*C(a)
    //      = S + (b - a)*C(a)
    // で求められる。
    // また、全てのaがbに変わるので、入れ替えた後のAに含まれる数値の個数をC'と
    // すれば
    //   C' = { ..., a: 0, b: C(a) + C(b), ... }
    //   ※C(b)はもともとAに含まれていたbの個数
    // となる。
    let mut counts = count_nums(a);
    changes
        .iter()
        .scan(sum(&counts), |state, &(f, t)| {
            let &c1 = counts.get(&f).unwrap_or(&0);
            let &c2 = counts.get(&t).unwrap_or(&0);
            *state += (t - f) * c1 as i64;

            counts.insert(f, 0);
            counts.insert(t, c1 + c2);

            Some(*state)
        })
        .collect()
}

fn count_nums(a: &[i64]) -> HashMap<i64, i32> {
    a.iter().fold(HashMap::new(), |mut acc, &cur| {
        let c = acc.entry(cur).or_insert(0);
        *c += 1;
        acc
    })
}

fn sum(nums: &HashMap<i64, i32>) -> i64 {
    nums.iter().map(|(&n, &c)| n * c as i64).sum()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_count_nums_1() {
        let input = &[1, 2, 3, 4];
        let expected = vec![(1, 1), (2, 1), (3, 1), (4, 1)]
            .into_iter()
            .collect::<HashMap<i64, i32>>();
        assert_eq!(expected, count_nums(input));
    }

    #[test]
    fn test_count_nums_2() {
        let input = &[1, 1, 2, 2, 3];
        let expected = vec![(1, 2), (2, 2), (3, 1)]
            .into_iter()
            .collect::<HashMap<i64, i32>>();
        assert_eq!(expected, count_nums(input));
    }

    #[test]
    fn test_count_nums_3() {
        let input = &[2, 3, 2, 1, 3];
        let expected = vec![(1, 1), (2, 2), (3, 2)]
            .into_iter()
            .collect::<HashMap<i64, i32>>();
        assert_eq!(expected, count_nums(input));
    }

    #[test]
    fn test_sum_1() {
        let input = vec![(1, 1), (2, 1), (3, 1), (4, 1)]
            .into_iter()
            .collect::<HashMap<i64, i32>>();
        assert_eq!(10, sum(&input));
    }

    #[test]
    fn test_sum_2() {
        let input = vec![(1, 2), (2, 2), (3, 1)]
            .into_iter()
            .collect::<HashMap<i64, i32>>();
        assert_eq!(9, sum(&input));
    }

    #[test]
    fn test_sum_3() {
        let input = vec![(1, 1), (2, 2), (3, 2)]
            .into_iter()
            .collect::<HashMap<i64, i32>>();
        assert_eq!(11, sum(&input));
    }

    #[test]
    fn test_solve_1() {
        let input1 = &[1, 2, 3, 4];
        let input2 = &[(1, 2), (3, 4), (2, 4)];
        let expected = vec![11, 12, 16];
        assert_eq!(expected, solve(input1, input2));
    }

    #[test]
    fn test_solve_2() {
        let input1 = &[1, 1, 1, 1];
        let input2 = &[(1, 2), (2, 1), (3, 5)];
        let expected = vec![8, 4, 4];
        assert_eq!(expected, solve(input1, input2));
    }

    #[test]
    fn test_solve_3() {
        let input1 = &[1, 2];
        let input2 = &[(1, 100), (2, 100), (100, 1000)];
        let expected = vec![102, 200, 2000];
        assert_eq!(expected, solve(input1, input2));
    }
}