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 (Python):
N = int(input())
A = list(map(int, input().split()))
D = {}
for i in range(N):
    if A[i] in D:
        D[A[i]] += 1
    else:
        D[A[i]] = 1
Q = int(input())
S = sum(A)
for i in range(Q):
    B, C = map(int, input().split())
    diff = C - B
    cnt = 0
    if B in D:
        S += diff * D[B]
        print(S)
        if C in D:
            D[C] += D[B]
        else:
            D[C] = D[B]
        D[B] = 0
    else:
        print(S)

Solution (Python):
from collections import defaultdict

N = int(input())
a = list(map(int, input().split()))
d = defaultdict(int)
total = 0
for i in a:
    d[i] += 1
    total += i
Q = int(input())

for i in range(Q):
    b, c = map(int, input().split())
    cnt = d[b]
    total -= cnt * b
    d[b] = 0
    d[c] += cnt
    total += c * cnt
    print(total)

Solution (Python):
from collections import defaultdict, Counter
from bisect import bisect_right, bisect_left

N = int(input())
A = list(map(int, input().split()))
C = Counter(A)
ans = sum(A)
Q = int(input())
for i in range(Q):
    b, c = map(int, input().split())
    C[c] += C[b]
    ans -= b * C[b]
    ans += c * C[b]
    C[b] = 0
    print(ans)

Solution (Python):

from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    a = LI()
    q = I()
    m = 10**5+1
    f = [0]*m
    s = 0
    for i in a:
        s += i
        f[i] += 1
    for _ in range(q):
        b,c = LI()
        ns = s+(c-b)*f[b]
        f[c] += f[b]
        f[b] = 0
        print(ns)
        s = ns
    return


if __name__ == "__main__":
    solve()

Solution (Python):
from collections import defaultdict

N = int(input())
a = list(map(int, input().split()))
d = defaultdict(int)
total = 0
for i in a:
    d[i] += 1
    total += i
Q = int(input())

for i in range(Q):
    b, c = map(int, input().split())
    cnt = d[b]
    total -= cnt * b
    d[b] = 0
    d[c] += cnt
    total += c * cnt
    print(total)

Solution (Python):
n = int(input())
As  = list(map(int, input().split()))
from collections import defaultdict
d = defaultdict(int)
for a in As:
    d[a] += 1
S = sum(As)

q =int(input())
for _ in range(q):
    b,c = map(int, input().split())
    tmp = d[b]
    S -= b*tmp
    S += c*tmp
    d[b] = 0
    d[c] += tmp
    print(S)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
dic={i:0 for i in range(100002)}
ans=0
for i in range(n):
    dic[a[i]]+=1
    ans+=a[i]
for i in range(q):
    b,c=map(int,input().split())
    dic[c]+=dic[b]
    ans+=dic[b]*(c-b)
    dic[b]=0
    print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

N=int(input())
A=list(map(int,input().split()))
from collections import Counter
C=Counter(A)
Q=int(input())

ANS=0
for i in C:
    ANS+=i*C[i]
    
for qq in range(Q):
    B,X=map(int,input().split())
    ANS+=X*C[B]
    C[X]+=C[B]

    ANS-=B*C[B]
    del(C[B])
    print(ANS)

Solution (Python):
import sys
input = sys.stdin.readline

N=int(input())
A=list(map(int,input().split()))
from collections import Counter
C=Counter(A)
Q=int(input())

ANS=0
for i in C:
    ANS+=i*C[i]
    
for qq in range(Q):
    B,X=map(int,input().split())
    ANS+=X*C[B]
    C[X]+=C[B]

    ANS-=B*C[B]

    del(C[B])

    print(ANS)

Solution (Python):


import sys









from collections import defaultdict          





















def main():
    mod = 1000000007                
    inf = float('inf')              
    
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def isp():   return input().split()
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    
    
    n = ii()
    L = lmi()
    q = ii()
    command = [lmi() for _ in range(q)]

    d = defaultdict(int)
    for num in L:
        d[num] += 1
    S = sum(L)

    for b, c in command:
        b_num = d[b]
        S += (c - b) * b_num
        d[b] = 0
        d[c] += b_num
        print(S)



if __name__ == "__main__":
    main()

Solution (Python):
FAST_IO = 0
if FAST_IO:
    import io, sys, atexit
    rr = iter(sys.stdin.read().splitlines()).next
    sys.stdout = _OUTPUT_BUFFER = io.BytesIO()
    @atexit.register
    def write():
        sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
else:
    rr = raw_input
rri = lambda: int(rr())
rrm = lambda: map(int, rr().split())
rrmm = lambda n: [rrm() for _ in xrange(n)]



from collections import defaultdict as ddic

def solve(N, A, Q, queries):
    count = ddic(int)
    for x in A:
        count[x] += 1
    S = sum(A)
    for b, c in queries:
        ct = count[b]
        count[b] = 0
        count[c] += ct
        S += (c-b) * ct
        print S
        

N = rri()
A=rrm()
Q= rri()
queries = [rrm() for _ in xrange(Q)]
solve(N, A, Q, queries)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
L = [0] * (100001)
s = sum(A)
for a in A:
    L[a] += 1
Q = int(input())
for _ in range(Q):
    B, C = map(int, input().split())
    L[C] += L[B]
    s += L[B] * (C-B)
    L[B] = 0
    print(s)