#abc171d. [abc171_d]Replacing
[abc171_d]Replacing
Problem Statement
You have a sequence composed of positive integers: .
You will now successively do the following operations:
- In the -th operation, you replace every element whose value is with .
For each , find : the sum of all elements in just after the -th operation.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print integers to Standard Output in the following format:
Note that may not fit into a -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 is .
After each operation, it becomes the following:
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 may not contain an element whose value is .
Sample Input 3
2
1 2
3
1 100
2 100
100 1000
Sample Output 3
102
200
2000