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()))
q = int(input())

dict_a = {}
current_sum = 0
for i in range(n):
  dict_a[a[i]] = dict_a.get(a[i],0)+1
  current_sum += a[i]

for i in range(q):
  b,c = map(int,input().split())
  diff = c-b
  
  rep_num = dict_a.get(b,0)
  current_sum += diff*rep_num
  print(current_sum)
  
  dict_a[b] = 0
  dict_a[c] = dict_a.get(c,0) + rep_num

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
q = int(input())
sums = sum(a)
d = {}

for i in a:
  if i in d:
    d[i] += 1
  else:
    d[i] = 1

for i in range(q):
    b,c = map(int, input().split())
    if b in d:
        sums += d[b]*(c-b)
        if c in d:
          d[c] += d[b]
        else:
          d[c] = d[b]
        d[b] = 0
    print(sums)

Solution (Python):
n = int(input())
arr = list(map(int, input().split()))
s = sum(arr)
d = dict()
for i in arr:
	if (i not in d):
		d[i] = 1
	else:
		d[i] += 1
q = int(input())
for i in range(q):
	b, c = map(int, input().split())
	if (b in d):
		s -= b * d[b]
		s += c * d[b]
		if (c not in d):
			d[c] = d[b]
		else:
			d[c] += d[b]
		d.pop(b)
	print(s)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
bc=[list(map(int,input().split())) for i in range(q)]

asum=sum(a) 
d={}

for i in a:
  if i in d:
    d[i]+=1
  else:
    d[i]=1

for b,c in bc:
  if b in d:
    asum+=d[b]*(c-b)
    if c in d:
      d[c]+=d[b]
    else:
      d[c]=d[b]
    d[b]=0
  print(asum)

Solution (Python):
from collections import defaultdict
N = int(input())
li = [int(x) for x in input().split()]
Q = int(input())

d = defaultdict(int)

for i in li:
    d[i] += 1

ans = sum(li)
for i in range(Q):
    B, C = map(int, input().split())
    ans += (C - B) * d[B]
    d[C] += d[B]
    d[B] = 0
    print(ans)

Solution (Python):


from collections import Counter


def solve(N: int, A: "List[int]", Q: int, BC: "List[(int,int)]"):
    A = Counter(A)
    answers = []
    ans = ans = sum((i * n for i, n in A.items()))
    for b, c in BC:
        if b != c:
            ans += (c - b) * A[b]
            A[c] += A[b]
            del A[b]
        answers.append(ans)
    return answers


def main():
    N = int(input())  
    A = list(map(int, input().split()))
    Q = int(input())
    BC = [tuple(map(int, input().split())) for _ in range(Q)]
    answer = solve(N, A, Q, BC)
    for ans in answer:
        print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n = I()
    suuretu = LI()
    q = I()
    dic = collections.Counter(suuretu)
    nowsum = sum(suuretu)
    
    for _ in range(q):
        b,c = LI()
        cnt = dic[b]
        nowsum += (c-b)*cnt
        print(nowsum)
        dic[b] = 0
        dic[c] += cnt
main()

Solution (Python):
n=int(input())
arr=[int(i) for i in input().strip().split(" ")]
t=int(input())
dic={}
sum_array=0
for i in arr:
    dic[i]=dic.get(i,0)+1
    sum_array+=i
    

for _ in range(t):
    a,b=[int(i) for i in input().split(" ")]
    if a not in dic:
        print(sum_array)
        continue
    if b in dic:
        dic[b]+=dic[a]
    else:
        dic[b]=dic[a]
    
    sum_array+=dic[a]*(b-a)
    print(sum_array)
    dic[a]=0

Solution (Python):


import collections
N = int(input())
s = input().split()
b = [str(l) for l in s]
c = collections.Counter(b)

K = int(input())
t = [input().split() for i in range(K)]

d = int(0)
e = int(0)
ans = int(0)
ckey = []
cvalue = []
clen = int(len(c))

for i in c.keys():
    ckey.append(i)
for i in c.values():
    cvalue.append(i)
for i in range(clen):
    ans = ans + int(ckey[i]) * int(cvalue[i])
    
for i in range(K):
    if c.get(t[i][0]) != None:
        d = c.get(t[i][0])
        if c.get(t[i][1]) != None:
            e = c.get(t[i][1])
            ans = ans - int(t[i][0]) * int(d) + int(t[i][1]) * int(d)
            print(ans)
            c[t[i][1]] = int(d) + int(e)
            c[t[i][0]] = 0
        else:
            ans = ans - int(t[i][0]) * int(d) + int(t[i][1]) * int(d)
            print(ans)
            c[t[i][1]] = int(d)
            c[t[i][0]] = 0
    else:
        print(ans)

Solution (Python):
from collections import Counter

n = int(input())
a = list(map(int,input().split()))
q = int(input())

C = Counter(a)
s = sum(a)

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

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
ans = sum(A)

X = [0]*(10**5+1)

for x in A:
    X[x] += 1

Q = int(input())

for _ in range(Q):
    B,C = map(int,input().split())
    ans += (C-B)*X[B]
    X[C] += X[B]
    X[B] = 0
    print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
BCs = [list(map(int,input().split())) for _ in range(Q)]
max_A = max(A)




A_num = [0 for i in range(10 ** 5 + 1)]
for a in A:
    A_num[a] += 1

Si = sum(A)

for BC in BCs:
    
    
    
    if A_num[BC[0]] > 0: 
        Si += A_num[BC[0]] * (BC[1] -BC[0])
        A_num[BC[1]] += A_num[BC[0]]
        A_num[BC[0]] = 0

    print(Si)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
total = sum(A)
d = {}
for a in A:
    if a in d:
        d[a] += 1
    else:
        d[a] = 1
Q = int(input())
BC = [list(map(int, input().split())) for _ in range(Q)]
for b, c in BC:
    if b in d:
        total += d[b] * (c-b)
        if c in d:
            d[c] += d[b]
        else:
            d[c] = d[b]
        d[b] = 0
    print(total)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

Q = int(input())

counter = [0 for _ in range(10**5+1)]
ans = 0

for i in range(N):
    counter[A[i]] += 1
    ans += A[i]

B = []
C = []
for i in range(Q):
    Bi, Ci = list(map(int, input().split()))
    B.append(Bi)
    C.append(Ci)

for i in range(Q):
    Bi = B[i]
    Ci = C[i]
    ans += Ci*counter[Bi] - Bi*counter[Bi]
    print(ans)
    counter[Ci] += counter[Bi]
    counter[Bi] = 0

Solution (Python):
from collections import defaultdict

n = int(input())
a = list(map(int, input().split()))
q = int(input())

sm = 0
cnt = defaultdict(int)
for i in a:
    sm += i
    cnt[i] += 1

for _ in range(q):
    b, c = list(map(int, input().split()))
    if b in cnt:
        sm -= cnt[b] * b
        sm += cnt[b] * c

        cnt[c] += cnt[b]
        del cnt[b]
    print(sm)

Solution (Python):
def abc171d_replacing():
    n = int(input())
    a = list(map(int, input().split()))
    a_dict = {}
    for v in a:
        if a_dict.get(v) is None:
            a_dict[v] = 1
        else:
            a_dict[v] += 1
    q = int(input())
    s = []
    total = 0
    for k, v in a_dict.items():
        total += k * v
    for _ in range(q):
        b, c = map(int, input().split())
        if b in a_dict.keys():
            total += (c * a_dict[b] - b * a_dict[b])
            if c in a_dict.keys():
                a_dict[c] += a_dict[b]
            else:
                a_dict[c] = a_dict[b]
            del a_dict[b]
        s.append(total)

    for v in s:
        print(v)


abc171d_replacing()

Solution (Python):






N = int(input())



A = list(map(int, input().split()))























Q = int(input())
B_C = [list(map(int,input().split())) for _ in range(Q)]

tot = sum(A)
cnt = [0] * 100001
for i in A:
    cnt[i] += 1



for l in B_C:
    cnt[l[1]] += cnt[l[0]]
    tot += cnt[l[0]] * (l[1] - l[0])
    cnt[l[0]] = 0
    print(tot)

Solution (Python):
from collections import Counter


N = int(input())
A = list(map(int, input().split( )))
Q = int(input())
Sum = sum(A)

coun = Counter(A)


for _ in range(Q):
  b, c = map(int, input().split( ))
  Sum += (c-b)*coun[b]
  coun[c] += coun[b]
  coun[b] = 0
  print(Sum)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))

cnt=[0]*(10**5+1)

for i in a:
  cnt[i]+=1

xxx=sum(a)

q=int(input())

for i in range(q):
  l,r=map(int,input().split())
  pin=cnt[l]
  cnt[r]+=pin
  cnt[l]=0

  xxx+=(r-l)*pin

  print(xxx)

Solution (Python):

def main():
    import sys

    input = sys.stdin.readline

    _ = int(input())
    lst = [0 for _ in [0] * (10 ** 5 + 1)]
    sum_lst = 0
    for a in [int(x) for x in input().split()]:
        sum_lst += a
        lst[a] += 1
    Q = int(input())

    for _ in range(Q):
        B, C = map(int, input().split())

        sum_lst += (C - B) * lst[B]
        print(sum_lst)
        lst[C] += lst[B]
        lst[B] = 0


if __name__ == '__main__':
    main()

Solution (Python):
def solve():
    N = int(input())
    A = [int(i) for i in input().split()]
    counter = {}
    total = 0
    for num in A:
        total += num
        counter[num] = counter.get(num, 0) + 1

    Q = int(input())
    for i in range(Q):
        B,C = [int(i) for i in input().split()]
        if B in counter:
            B_cnt = counter[B]
            counter[C] = counter.get(C, 0) + B_cnt
            total += (C-B) * B_cnt
            counter[B] = 0
        print(total)

if __name__ == "__main__":
    solve()

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

def main():
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    cnt = Counter(A)


    ans = sum(A)
    for i in range(Q):
        B, C = map(int, input().split())
        ans += cnt[B] * (C - B)
        cnt[C] += cnt[B]
        cnt[B] = 0
        print(ans)

main()

Solution (Python):
sum = 0
num = [0] * (10 ** 5 + 1)
N = int(input())
A = [int(a) for a in input().split()]
for i in range(0, N):
    sum += A[i]
    num[A[i]] += 1

Q = int(input())
for i in range(0, Q):
    B, C = (int(x) for x in input().split())
    sum += (C - B) * num[B]
    num[C] += num[B]
    num[B] = 0
    print(sum)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
q = int(input())
sm = sum(a)
hm = [0] * (10 ** 5 + 1)
for i in a:
    hm[i] += 1
for _ in range(q):
    b, c = map(int, input().split())
    sm += (c - b) * hm[b]
    hm[c] += hm[b]
    hm[b] = 0
    print(sm)

Solution (Python):



def next_line():
    return input()


def next_int():
    return int(input())


def next_int_array_one_line():
    return list(map(int, input().split()))


def next_int_array_multi_lines(size):
    return [int(input()) for _ in range(size)]


def next_str_array(size):
    return [input() for _ in range(size)]


def main():
    n = next_int()
    ar = next_int_array_one_line()
    num = [0] * 100001
    for a in ar:
        num[a] += 1
    q = next_int()
    res = 0
    for i in range(len(num)):
        res += num[i] * i

    for i in range(q):
        b, c = map(int, input().split())
        res += (c-b) * num[b]
        num[c] += num[b]
        num[b] = 0
        print(res)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
B = [0]*Q
C = [0]*Q
for i in range(Q):
    B[i],C[i] = map(int,input().split())
    
import collections
c = collections.Counter(A)
startsum = sum(A)
for i in range(Q):
    Bcount = c[B[i]]
    c[B[i]] = 0
    Ccount = c[C[i]]
    c[C[i]] += Bcount
    startsum += (C[i]-B[i])*Bcount
    print(startsum)

Solution (Python):
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
q = int(input())
num = Counter(a)
s = sum(a)
for _ in range(q):
    b,c = map(int,input().split())
    s += (c-b)*num[b]
    num[c] += num[b]
    num[b] = 0
    print(s)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
num=[0]*100005
ans=sum(a)

for _ in a:
  num[_-1]+=1

q=int(input())

for i in range(q):
  b,c=map(int,input().split())
  
  if b<=c:
    ans+=(abs(c-b))*num[b-1]
    
  else:
    ans-=(abs(c-b))*num[b-1]
    
  print(ans)
  
  num[c-1]+=num[b-1]
  num[b-1]=0

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
q=int(input())

summ=sum(a)
num=[0 for i in range(10**5+1)]
for i in range(n):
    num[a[i]] += 1
for i in range(q):
    b,c=map(int,input().split())
    b_num=num[b]
    num[c] += b_num
    num[b]=0
    summ +=(c-b)*b_num
    print(summ)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
B = [0]*Q
C = [0]*Q
for i in range(Q):
    B[i], C[i] = map(int,input().split())
    
num = [0]*100001
for i in A:
    num[i] += 1

S = [0]*(Q+1)
S[0] = sum(A)
change = 0
for i in range(Q):
    change = num[B[i]]
    S[i+1] = S[i] + change*(C[i]-B[i])
    num[B[i]] -= change
    num[C[i]] += change
    
for i in S[1:]:
    print(i)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
arr=[0]*(10**5+1)
for i in a:
    arr[i]+=1


s=sum(a)

for i in range(int(input())):
    b,c=map(int,input().split())
    arr[c]+=arr[b]
    s=s+arr[b]*c-arr[b]*b
    arr[b]=0
    print(s)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
co=[0]*(10**5)
s=sum(a)
for i in a:
  co[i-1]+=1
for i in range(int(input())):
  b,c=map(int,input().split())
  s=s+(c-b)*co[b-1]
  print(s)
  co[c-1]+=co[b-1]
  co[b-1]=0

Solution (Python):
from collections import defaultdict

N=int(input())
count=defaultdict(int)
*A,=map(int,input().split())
for i in range(N):
  count[A[i]] += 1
  
ans = 0
for k in count.keys():
  ans += count[k]*k

Q=int(input())
for q in range(Q):
  b,c = map(int,input().split())
  ans += c*count[b] - b*count[b]
  count[c] += count[b]
  count[b] = 0
  print(ans)

Solution (Python):
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
sum_res = sum(A)
counter = Counter(A)

for _ in range(Q):
    B, C = map(int, input().split())
    sum_res += (C - B) * counter[B]
    counter[C] += counter[B]
    counter[B] = 0
    print(sum_res)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
sum_res = sum(A)
counter = [0 for _ in range(10 ** 5 + 1)]

for a in A:
    counter[a] += 1

for _ in range(Q):
    B, C = map(int, input().split())
    sum_res += (C - B) * counter[B]
    counter[C] += counter[B]
    counter[B] = 0
    print(sum_res)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
sum_res = sum(A)
counter = [0 for _ in range(10 ** 5 + 1)]

for a in A:
    counter[a] += 1

for _ in range(Q):
    B, C = map(int, input().split())
    sum_res = sum_res - (counter[B] * B)
    sum_res = sum_res + (counter[B] * C)
    counter[C] += counter[B]
    counter[B] = 0
    print(sum_res)

Solution (Python):
from collections import defaultdict

N = int(input())
A = list(map(int, input().split()))
Q = int(input())

table = defaultdict(int)
ans = 0
for a in A:
    table[a] += 1
    ans += a

for _ in range(Q):
    B, C = map(int, input().split())
    diff = table[B] * (C - B)
    table[C] += table[B]
    table[B] = 0
    ans += diff
    print(ans)

Solution (Python):
def main():
    import sys
    from collections import Counter

    input = sys.stdin.readline
    input()
    a = tuple(map(int, input().split()))
    ans = sum(a)
    a = Counter(a)
    for _ in range(int(input())):
        b, c = map(int, input().split())
        if b in a:
            ans += (c - b) * a[b]
            a[c] = a[c] + a[b] if c in a else a[b]
            del a[b]
        print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    import sys
    from collections import Counter

    input = sys.stdin.readline
    input()
    a = list(map(int, input().split()))
    ans = sum(a)
    a = Counter(a)
    for _ in range(int(input())):
        b, c = map(int, input().split())
        if b in a:
            ans += (c - b) * a[b]
            print(ans)
            if c in a:
                a[c] += a[b]
            else:
                a[c] = a[b]
            del a[b]
        else:
            print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    import sys
    from collections import Counter

    input = sys.stdin.readline
    input()
    a = Counter(map(int, input().split()))
    ans = sum(k * v for k, v in a.items())
    for _ in range(int(input())):
        b, c = map(int, input().split())
        if b in a:
            ans += (c - b) * a[b]
            print(ans)
            a[c] = a[c] + a[b] if c in a else a[b]
            del a[b]
        else:
            print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    import sys
    from collections import Counter

    input = sys.stdin.readline
    input()
    a = list(map(int, input().split()))
    ans = sum(a)
    a = Counter(a)
    for _ in range(int(input())):
        b, c = map(int, input().split())
        if b in a:
            ans += (c - b) * a[b]
            print(ans)
            a[c] = a[c] + a[b] if c in a else a[b]
            del a[b]
        else:
            print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    import sys
    from collections import Counter

    input = sys.stdin.readline
    input()
    a = tuple(map(int, input().split()))
    ans = sum(a)
    a = Counter(a)
    for _ in range(int(input())):
        b, c = map(int, input().split())
        if b in a:
            ans += (c - b) * a[b]
            print(ans)
            a[c] = a[c] + a[b] if c in a else a[b]
            del a[b]
        else:
            print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys

def input():
  return sys.stdin.readline()

N=int(input())
A=list(map(int,input().split()))
B=[0]*(10**5)
for a in A:
  B[a-1]+=1
s=sum(A)
Q=int(input())
for i in range(Q):
  b,c=map(int,input().split())
  bb,bc=B[b-1],B[c-1]
  B[c-1]+=bb
  B[b-1]=0
  s+=bb*c-bb*b
  print(s)

Solution (Python):
import sys

def input():
  return sys.stdin.readline()

N=int(input())
A=list(map(int,input().split()))
B=[0]*(10**5)
for a in A:
  B[a-1]+=1
s=sum(A)
Q=int(input())
for i in range(Q):
  b,c=map(int,input().split())
  bb,bc=B[b-1],B[c-1]
  B[c-1]+=bb
  B[b-1]=0
  s+=bb*c-bb*b
  print(s)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
b=[]
c=[]
for i in range(q):
    tempb,tempc=map(int,input().split())
    b.append(tempb)
    c.append(tempc)
cnt=[-1]+[0]*10**5
for i in range(n):
    cnt[a[i]]+=1
ans=sum(a)
for i in range(q):
    ans+=(c[i]-b[i])*cnt[b[i]]
    cnt[c[i]]+=cnt[b[i]]
    cnt[b[i]]=0
    print(ans)

Solution (Python):
from collections import Counter
 
N = int(input())
A = list(map(int, input().split()))
count = Counter(A)
S = sum(A)
 
for _ in range(int(input())):
    b, c = map(int, input().split())
    k = count[b]
    count[b] = 0
    count[c] += k
    S += (c - b) * k
    print(S)

Solution (Python):
N = int(input())

A_li = list(map(int, input().split()))

Q = int(input())


A_li.sort()
res = sum(A_li)
main_li = {}

now = 0
for i in A_li:
    if i == now:
        main_li[i] += 1
    else:
        main_li[i] = 1
        now = i



for _ in range(Q):
    B, C = list(map(int, input().split()))
    if B not in main_li.keys():
        print(res)
        continue
    if C in main_li.keys():
        main_li[C] += main_li[B]
    else:
        main_li[C] = main_li[B]
    res += (C - B) * main_li[B]
    main_li.pop(B)
    print(res)

Solution (Python):
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
count = Counter(A)
S = sum(A)

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

Solution (Python):
N = int(input())
nums = list(map(int, input().split()))
Q = int(input())
operations = [list(map(int, input().split())) for _ in range(Q)]

counts = [ 0 for __ in range(10**5+1)]
answer = sum(nums)

for num in nums:
  counts[num] += 1

for b, c in operations:
  answer += counts[b]*(c-b)
  print(answer)
  counts[c] += counts[b]
  counts[b] = 0

Solution (Python):
from collections import defaultdict

n = int(input())
arr = list(input().split())
q = int(input())
num_counts = defaultdict(lambda: 0)
ans = 0

for i in arr:
  num_counts[i] += 1
  ans += int(i)

for _ in range(q):
  bef, af = list(input().split())
  apper_count = num_counts[bef]
  ans += (int(af) - int(bef)) * apper_count
  num_counts[bef] -= apper_count
  num_counts[af] += apper_count
  print(ans)

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

Solution (Python):
n=int(input())
A=list(map(int,input().split()))

asum = sum(A)
S={}
for i in range(len(A)):
    if A[i] in S:
        S[A[i]]+=1
    else:
        S[A[i]]=1

D=[]
q=int(input())
for i in range(q):
    diff=0
    b,c =map(int,input().split())
    if not c in S:
        S[c]=0
    if b in S:
        diff = (S[b]*c-S[b]*b)
        S[c]+=S[b]
        S[b]=0

    asum+=diff
    D.append(asum)

for d in D:
    print(d)

Solution (Python):
from collections import Counter

def main():
    n = int(input())
    a = list(map(int,input().split()))
    q = int(input())
    c = Counter(a)
    allsum = 0
    for k,v in c.items():
        allsum += k*v
    ans = []
    for i in range(q):
        bi,ci = list(map(int,input().split()))
        allsum += ci*c[bi] - bi*c[bi]
        c[ci] = c[ci] + c[bi]
        c[bi] = 0
        ans.append(allsum)
    for i in ans:
        print(i)    
main()

Solution (Python):
from collections import Counter

n = int(input())
a = list(map(int,input().split()))
q = int(input())

c = Counter(a)

allsum = 0
for k,v in c.items():
    allsum += k*v
    
ans = []

for i in range(q):
    bi,ci = list(map(int,input().split()))
    allsum += ci*c[bi] - bi*c[bi]
    c[ci] = c[ci] + c[bi]
    c[bi] = 0
    ans.append(allsum)
    
for i in ans:
    print(i)

Solution (Python):
from collections import Counter

n = int(input())
a = list(map(int,input().split()))
q = int(input())



c = Counter(a)

allsum = 0
for k,v in c.items():
    allsum += k*v
    
ans = []

for i in range(q):
    bi,ci = list(map(int,input().split()))
    allsum += ci*c[bi] - bi*c[bi]
    c[ci] = c[ci] + c[bi]
    c[bi] = 0
    ans.append(allsum)
    
for i in ans:
    print(i)

Solution (Python):
"""
import numpy as np
from collections import Counter

n = map(int,input().split())
a = list(map(int,input().split()))
Q = int(input())
a_count = dict(Counter(a))



def change_dict_key(d, old_key, new_key, default_value=None):
    d[new_key] = d.pop(old_key, default_value)








for q in range(Q):
    num = list(map(int,input().split()))
    
    if(num[0] not in a_count):
        print(np.dot( np.array(list(a_count.keys())) , np.array(list(a_count.values()) ) ) )
        continue

    if(num[1] not in a_count):
        change_dict_key(a_count,num[0],num[1])

    else :
        a_count[num[1]] += a_count[num[0]] 
        del a_count[num[0]] 
    
    

    print(np.dot( np.array(list(a_count.keys())) , np.array(list(a_count.values()) ) ) )
    
"""
N = int(input())
A = list(map(int, input().split()))
Q = int(input())

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

bucket = [0]*100001
for i in A:
    bucket[i] += 1

sum = sum(A)

for i in range(Q):
    sum += (C[i] - B[i]) * bucket[B[i]]
    bucket[C[i]] += bucket[B[i]]
    bucket[B[i]] = 0
    print(sum)

Solution (Python):



N=int(input())
A=list(map(int,input().split()))

log=[0]*10**5

for a in A:
    log[a-1]+=1


wa=sum([(i+1)*log[i] for i in range(len(log))])

for _ in range(int(input())):
    B,C=map(int,input().split())
    wa+=(C-B)*log[B-1]
    log[B-1],log[C-1]=0,log[B-1]+log[C-1]
    print(wa)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
b=[0]*q
c=[0]*q
for i in range(q):
  b[i],c[i]=map(int,input().split())

s=sum(a)


"""
for i in range(q):
  while b[i] in a:
    s+=c[i]-b[i]
    a.remove(b[i])
    a.append(c[i])
  print(s)
"""

cnt_list=[0]*(10**5+1)
for i in range(n):
  cnt_list[a[i]]+=1

for i in range(q):
  s+=(c[i]-b[i])*cnt_list[b[i]]
  cnt_list[c[i]]+=cnt_list[b[i]]
  cnt_list[b[i]]=0
  print(s)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
BC = [list(map(int, input().split())) for _ in range(Q)]

num = [0] * 100001
for a in A:
    num[a] += 1
S = sum(A)
for i in range(Q):
    S += (BC[i][1] - BC[i][0]) * num[BC[i][0]]
    print(S)
    num[BC[i][1]] += num[BC[i][0]]
    num[BC[i][0]] = 0

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
BC = [list(map(int, input().split())) for _ in range(Q)]

S = 0
num = [0] * 100001
for a in A:
    S += a
    num[a] += 1
for i in range(Q):
    S += (BC[i][1] - BC[i][0]) * num[BC[i][0]]
    print(S)
    num[BC[i][1]] += num[BC[i][0]]
    num[BC[i][0]] = 0

Solution (Python):
N=int(input())
A=list(map(int, input().split()))
q=int(input())
Q=[list(map(int, input().split())) for _ in range(q)]

L=[0]*(10**5+1)
S=0
for i in range(N):
    L[A[i]]+=1
    S+=A[i]

for i in range(q):
    S+=L[Q[i][0]]*(Q[i][1]-Q[i][0])
    L[Q[i][1]]+=L[Q[i][0]]
    L[Q[i][0]]=0
    
    print(S)

Solution (Python):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from collections import Counter
def main():
    n = int(input())
    a = tuple(map(int, input().split()))
    suma = sum(a)
    aa = Counter(a)
    q = int(input())
    m = map(int, read().split())
    for b, c in zip(m, m):
        kosu = aa[b]
        sa = c - b
        suma += sa * kosu
        print(suma)
        aa[b] = 0
        aa[c] += kosu

if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
BC = [list(map(int, input().split())) for _ in range(Q)]

counter = Counter(A)
counter_sum = sum(k*v for k, v in counter.items())

for b, c in BC:
    counter[c] += counter[b]
    counter_sum -= b * counter[b]
    counter_sum += c * counter[b]
    counter[b] = 0
    print(counter_sum)

Solution (Python):
import collections
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
M=collections.Counter(A)
ans=0
for j in M.items():
  ans+=j[0]*j[1]
for i in range(Q):
  B,C=map(int,input().split())
  ans+=(C-B)*M[B]
  M[C]+=M[B]
  M[B]=0
  print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
import collections
l=collections.Counter(a)
q=int(input())
s=sum(a)
for i in range(q):
    b,c=map(int,input().split())
    s+=(c-b)*l[b]
    l[c]+=l[b]
    l[b]=0
    print(s)

Solution (Python):
N = int(input())
A=list(map(int,input().split()))
Q = int(input())
BC = [map(int, input().split()) for _ in range(Q)]
B, C = [list(i) for i in zip(*BC)]
sum=0
D=[0]*(10**5+1)
for i in range(N):
    sum+=A[i]
    D[A[i]]+=1
for i in range(Q):
    D[C[i]]+=D[B[i]]
    sum+=D[B[i]]*(C[i]-B[i])
    D[B[i]]=0
    print(sum)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
N = int(input())
A=list(map(int,input().split()))
Q = int(input())
BC = [map(int, input().split()) for _ in range(Q)]
B, C = [list(i) for i in zip(*BC)]
sum=0
D=[0]*(10**5+1)
for i in range(N):
    sum+=A[i]
    D[A[i]]+=1
for i in range(Q):
    D[C[i]]+=D[B[i]]
    sum+=D[B[i]]*(C[i]-B[i])
    D[B[i]]=0
    print(sum)

Solution (Python):
import numpy as np

from collections import defaultdict
N = int(input())
A = tuple(map(int, input().split()))
Q = int(input())
BC= [list(map(int,input().split())) for _ in range(Q)]

counter = defaultdict(int)
for x in A:
    counter[x] += 1

total = sum(A)
for b, c in BC:
    total += (c - b) * counter[b]
    counter[c] += counter[b]
    counter[b] = 0
    print(total)

Solution (Python):
import sys
N = int(input())
A = tuple(map(int, input().split()))
count = [0] * (10 ** 5 + 1)

for a in A:
    count[a] += 1
s = sum(A)

Q = int(input())
for _ in range(Q):
    b, c = map(int, sys.stdin.readline().split())
    s -= count[b] * b
    s += count[b] * c
    count[c] += count[b]
    count[b] = 0
    print(s)

Solution (Python):
import collections

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
counter = collections.Counter(A)
total = 0
for k, v in counter.items():
    total += k * v
for i in range(Q):
    B, C = map(int, input().split())
    if counter.get(B) != None:
        count = counter.pop(B)
        counter[C] = counter.get(C, 0) + count
        total -= count * B
        total += count * C
    print(total)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))

q = int(input())

m = {}

for i in range(1000000):
  m[i] = 0

for e in a:
  m[e] += 1
  
ans = sum(a)

for i in range(q):
  b,q = map(int,input().split())
  diff = (q-b) * m[b]
  
  m[q] += m[b]
  m[b] = 0
  
  ans += diff
  print(ans)

Solution (Python):


def solve():
    """しゃくとり法で解く"""
    N = int(input())
    As = list(map(int, input().split()))

    
    dic = {}
    for a in As:
        if not a in dic:
            dic[a] = 0
        dic[a] += 1

    S = sum(As)  

    
    Q = int(input())
    for q in range(Q):
        b, c = list(map(int, input().split()))
        if b in dic:
            if not c in dic:
                dic[c] = 0
            S -= b*dic[b]
            S += c*dic[b]
            dic[c] += dic[b]
            dic[b] = 0
        print(S)


if __name__ == "__main__":
    solve()

Solution (Python):
def main():
    n = int(input())
    a = list(map(int,input().split()))
    d= {}
    for i in range(n):
        if d.get(a[i])==None:
            d[a[i]] = 1
        else:
            d[a[i]] += 1
    s = sum(a)
    q = int(input())
    for i in range(q):
        b,c = map(int,input().split())
        if d.get(b)==None:
            print(s)
        else:
            s += (c-b)*d[b]
            if d.get(c)==None:
                d[c] = d[b]
            else:
                d[c] += d[b]
            d[b] = 0
            print(s)

if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter
def solve():
  N = int(input())
  A = list(map(int, input().split()))
  c = Counter(A)
  Q = int(input())
  ans = [0]*Q
  total = sum(A)
  for i in range(Q):
    x,y = map(int, input().split())
    total += c[x]*(y-x)
    c[y] += c[x]
    c[x] = 0
    ans[i] = total
  return ans
print(*solve(),sep='\n')