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())
l=list(map(int,input().split()))

d={}
sm=0
for i in range(n):
  sm+=l[i]
  if l[i] not in d:
    d[l[i]]=1
  else:
    d[l[i]]+=1

q=int(input())
for i in range(q):
  a,b=map(int,input().split())
  dif=b-a
  if a not in d:
    d[a]=0
  sm+=(dif*d[a])
  print(sm)
  if b not in d:
    d[b]=0
  d[b]+=d[a]
  d[a]=0

Solution (Python):
n=int(input())
A=list(map(int,input().split()))
q=int(input())
dic={i+1:0 for i in range(10**5)}
for a in A:
  dic[a]+=1

s=sum(A)

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

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

Solution (Python):
from collections import defaultdict

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

nums_dict = defaultdict(int)
sum_nums = sum(nums)

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

M = int(input())

for i in range(M):
    original, replaced = map(int, input().split())

    if original not in nums_dict:
        print(sum_nums)
        continue

    total_change = (replaced - original) * nums_dict[original]
    sum_nums += total_change

    print(sum_nums)

    nums_dict[replaced] += nums_dict[original]
    del nums_dict[original]

Solution (Python):
import sys
n = int(sys.stdin.readline().rstrip("\n"))
s = 0
a = [0]*(10**6)
for z in sys.stdin.readline().rstrip("\n").split():
    z = int(z)
    a[z] += 1
    s += z
q = int(sys.stdin.readline().rstrip("\n"))
for i in range(q):
    b,c = map(int, sys.stdin.readline().rstrip("\n").split())
    d = c-b
    s += d*a[b]
    a[c] += a[b]
    a[b] = 0
    print(s)

Solution (Python):
import sys 
from collections import Counter

N = int(sys.stdin.readline().strip())
A = list(map(int, sys.stdin.readline().strip().split()))
Q = int(sys.stdin.readline().strip())

ans = sum(A)
counter = Counter(A)

query = []
for _ in range(Q):
    b, c = map(int, sys.stdin.readline().strip().split())
    ans = ans - counter[b] * b + counter[b] * c
    print(ans)
    if c not in counter:
        counter[c] = 0
    counter[c] += counter[b]
    counter[b] = 0

Solution (Python):
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))

def do():
    n=INT()
    a=LIST()
    q=INT()
    temp=sum(a)
    cnt=[0]*(10**5+1)
    for i in a:
        cnt[i]+=1
    
    for i in range(q):
        
        b,c=INTM()
        temp=temp+(c-b)*cnt[b]
        cnt[c]+=cnt[b]
        cnt[b]=0
        print(temp)

                
if __name__ == '__main__':
    do()

Solution (Python):
from collections import Counter

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

counter = Counter(arr)
total = sum(arr)

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

Solution (Python):
from collections import Counter

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

dic = Counter(A)

Q = int(input())

for _ in range(Q):
    b, c = map(int, input().split())
    if b in dic.keys():
        sumA += ((c-b)*dic[b])

        
        if c in dic.keys():
            
            
            dic[c] += dic[b]
            del dic[b]
        else:
            dic[c] = dic[b]
            del dic[b]
        
        
        
        
        
    
    
    print(sumA)

Solution (Python):
from collections import Counter

N, *I = map(int, open(0).read().split())
A, Q, BC = I[:N], I[N], I[N + 1:]

C = Counter(A)
ans = sum(A)
for b, c in zip(*[iter(BC)] * 2):
    ans += (c - b) * C[b]
    C[c] += C[b]
    C[b] = 0
    print(ans)

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

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

Solution (Python):
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
counter = Counter(A)
total = 0
for k,v in counter.items():
  total += k*v
Q = int(input())

for _ in range(Q):
  b,c = map(int,input().split())
  if b in counter:
    total -= b*counter[b]
    item = counter[b]
    del counter[b]
    if c in counter:
      counter[c] += item
      total += c*item
    else:
      counter[c] = item
      total += c*counter[c]
  
  print(total)

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

Solution (Python):
import bisect

n=int(input())
a=[int(x) for x in input().rstrip().split()]
dp=[0 for i in range(10**5)]
q=int(input())

for i in a:
  dp[i-1]+=1

ans=sum(a)
for i in range(q):
  b,c=map(int,input().split())
  ans=ans-(dp[b-1]*b)+(c*dp[b-1])
  dp[c-1]+=dp[b-1]
  dp[b-1]=0
  print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
dict={}
for k in a:
    if k in dict:
        dict[k]+=1
    else:
        dict[k]=1

ans=0
for k,v in dict.items():
    ans+=k*v

for _ in range(q):
    b,c=map(int,input().split())
    if b not in dict:
        print(ans)
        continue
    
    ans=ans+(c-b)*dict[b]
    if c in dict:
        dict[c]+=dict[b]
    else:
        dict[c]=dict[b]
    dict[b]=0
    print(ans)

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

A_sum = sum(A)
A_dict = {}

for a in A:
    if a not in A_dict:
        A_dict[a] = 0
    A_dict[a] += 1

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

for i in range(Q):
    b = B[i]
    c = C[i]
    if b not in A_dict:
        num = 0
    else:
        num = A_dict[b]
        A_dict[b] = 0
    print(A_sum - num*b + num*c)
    A_sum = A_sum - num*b + num*c
    if c not in A_dict:
        A_dict[c] = 0
    A_dict[c] += num

Solution (Python):
import collections


def resolve():
    n = int(input())
    targets = list(map(int, input().split()))
    q = int(input())
    ans = sum(targets)

    targets = collections.Counter(targets)

    for i in range(q):

        b, c = map(int, input().split())
        num_of_b = targets[b]
        targets.update({c: num_of_b})
        del targets[b]
        ans += (c-b)*num_of_b
        print(ans)

resolve()

Solution (Python):
from sys import stdin
from sys import setrecursionlimit
from collections import Counter
setrecursionlimit(10 ** 7)

n = int(input())
a = list(map(int,stdin.readline().rstrip().split()))
su = sum(a)

q = int(input())
cc = Counter(a)

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

Solution (Python):

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

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

Solution (Python):
from collections import Counter
n = int(input())
l = list(map(int,input().split()))

ans = sum(l)

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

Solution (Python):
import sys

readline = sys.stdin.readline
N = int(readline())
A = list(map(int, readline().split()))

x = [0] * (10**5+5)
for a in A:
    x[a] += 1
s = sum(A)
Q = int(readline())
for _ in range(Q):
    b, c = map(int, readline().split())
    s += (c - b) * x[b]
    print(s)
    x[c] += x[b]
    x[b] = 0

Solution (Python):
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
counter = Counter(A)
total = 0
for k,v in counter.items():
  total += k*v
Q = int(input())

for _ in range(Q):
  b,c = map(int,input().split())
  if b in counter:
    total -= b*counter[b]
    item = counter[b]
    del counter[b]
    if c in counter:
      counter[c] += item
      total += c*item
    else:
      counter[c] = item
      total += c*counter[c]
  
  print(total)

Solution (Python):
from collections import Counter

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

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

Solution (Python):
import sys
sys.setrecursionlimit(10000000)
import math
import bisect
from collections import deque
def input():
    return sys.stdin.readline()[:-1]
n = int(input())
a = list(map(int,input().split()))
q = int(input())

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

bc = []
for i in range(q):
    b,c = map(int,input().split())
    bc.append([b,c])

for ii in bc:
    [b,c] = ii
    if alis[b]!=0:
        ans+=(c-b)*alis[b]
        alis[b],alis[c] = 0,alis[b]+alis[c]
    
    print(ans)

Solution (Python):
from collections import Counter

N = int(input())
As = list(map(lambda x: int(x), input().split(" ")))
c = Counter(As)

sum_a = sum(As)

Q = int(input())
for _ in range(Q):
    B, C = map(lambda x: int(x), input().split(" "))
    change_count = c[B]
    sum_a -= change_count * B
    c[B] = 0
    sum_a += change_count * C

    if C in c:
        c[C] += change_count
    else:
        c[C] = change_count

    print(sum_a)

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

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

Solution (Python):
from collections import Counter

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

cnt=Counter(A)
total=sum(A)
for _ in range(q):
    b, c = map(int, input().strip().split())
    if b in cnt:
        total-=b*cnt[b]
        total+=c*cnt[b]
        cnt[c]+=cnt[b]
        del cnt[b]
    print(total)

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

Solution (Python):
import math



n=int(input())
l=[int(x) for x in input().split()]
q=int(input())
d={}
for i in range(len(l)):
	if l[i] in d:
		d[l[i]]+=1
	else:
		d[l[i]]=1
s=sum(l)
for i in range(q):
	a,b=map(int,input().split())
	if a==b:
		print(s)
		continue
	if a not in d:
		d[a]=0
	s=(s-d[a]*a+b*d[a])
	print(s)
	if b in d:
		d[b]+=d[a]
	else:
		d[b]=d[a]
	d[a]=0

Solution (Python):
n = int(input())
arr = [int(x) for x in input().split()]
m = {}
total = 0
for i, x in enumerate(arr):
    if m.get(x):
        m[x] += 1
    else:
        m[x] = 1
    total += x
for _ in range(int(input())):
    b, c = [int(x) for x in input().split()]
    if m.get(c):
        q = 1
    else:
        m[c] = 0
    if m.get(b):
        q = 1
    else:
        m[b] = 0
    total = total - (b*m[b]) + (c*m[b])
    m[c] += m[b]
    m[b] = 0
    print(total)

Solution (Python):
import sys
input = sys.stdin.readline
from collections import Counter
def main():
    N = int(input())
    A = list(map(int, input().split()))
    D = Counter(A)
    Q = int(input())
    T = sum(A)
    for i in range(Q):
        b, c = map(int, input().split())
        if b in D.keys():
            T += (c-b) * D[b]
            D[c] += D[b]
            D[b] = 0
        print(T)

if __name__ == '__main__':
    main()

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

Solution (Python):
from collections import defaultdict

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

nums_dict = defaultdict(int)
sum_nums = sum(nums)

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

M = int(input())

for i in range(M):
    original, replaced = map(int, input().split())

    if original not in nums_dict:
        print(sum_nums)
        continue

    total_change = (replaced - original) * nums_dict[original]
    sum_nums += total_change

    print(sum_nums)

    nums_dict[replaced] += nums_dict[original]
    del nums_dict[original]

Solution (Python):


import sys
import math
import itertools
import collections

def solve(N:int,A:list,Q:int,B:list,C:list) -> None:
    total = sum(A)
    A = [str(a) for a in A]
    counter = collections.Counter(A)
    for b,c in zip(B,C):
        number = counter[str(b)]
        total = total - (number * b) + (number * c)
        print(total)
        counter[str(b)] = 0
        counter[str(c)] += number

def main():
    N = int(input())
    A = list(map(int,input().split()))
    Q = int(input())
    B = []
    C = []
    for _ in range(Q):
        b,c = map(int,input().split())
        B.append(b)
        C.append(c)
    
    solve(N,A,Q,B,C)

if __name__ == "__main__":
    main()

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

Solution (Python):
def main():
	import sys
	input=sys.stdin.readline
	n=int(input())
	num=[0]*(10**5+2)
	a=list(map(int,input().split()))
	for x in a:
		num[x-1]+=1
	q=int(input())
	ans=0
	for i in range(len(num)):
		ans+=(i+1)*num[i]
	for i in range(q):
		b,c=map(int,input().split())
		ans+=num[b-1]*(c-b)
		num[c-1]+=num[b-1]
		num[b-1]=0
		print(ans)
if __name__ == '__main__':
	main()

Solution (Python):
N = input().strip() ;

arr = list ( map ( int , input().strip().split(" ") ) ) ; 

dic = {} ;
for v in arr :
	d = dic[v] if v in dic else 0;
	dic[v] = d + 1; 

Q = int ( input().strip() ) ;
s0 = sum ( arr ) ;
for zz in range( Q  ) :
	b , c = map ( int , input().strip().split(" ") ) ;
	

	if not b in dic :
		pass ;
	else :
		s0 -= b * dic[b] ;
		s0 += c * dic[b] ;

		d = dic[c] if c in dic else 0 ;
		dic[c] = dic[b] + d ;
		del dic[b] ;
	print (  s0 ) ;

Solution (Python):
from collections import Counter

n = int(input())
a = map(int, input().split())
d = Counter(a)

s = sum([k*v for k, v in d.items()])

q = int(input())
for _ in range(q):
    b, c = map(int, input().split())
    if b not in d.keys():
        print(s)
        continue
    cc = 0
    s = s + (c-b) * d[b]
    print(s)
    if c in d.keys():
        cc = d[c]
    d[c] = cc + d[b]
    del d[b]

Solution (Python):
n=int(input())
arr = list(map(int,input().split()))
som = sum(arr)
q=int(input())
freq = {}
for i in range(n):
    if arr[i] in freq:
        freq[arr[i]]+=1
    else:
        freq[arr[i]]=1
for _ in range(q):
    b,c=map(int,input().split())
    if b in freq:
        som = som + freq[b]*(c-b)
        if c in freq:
            freq[c] += freq[b]
        else:
            freq[c] = freq[b]
        freq[b]=0
        print(som)
    else:
        print(som)

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

ans = sum(A)
numbers_a = [0] * (10**5+10)

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

for i in range(Q):
    ans = ans - numbers_a[BC[i][0]]*BC[i][0] + numbers_a[BC[i][0]]*BC[i][1]
    print(ans)
    x = numbers_a[BC[i][0]]
    numbers_a[BC[i][1]] += x
    numbers_a[BC[i][0]] = 0

Solution (Python):
I=lambda:list(map(int,input().split()))
n=I()[0];arr=I();q=I()[0];
cnt={};
for i in arr:cnt[i]=0;
for i in arr:cnt[i]+=1;
s=sum(arr)
while(q):
  q-=1;b,c=I();
  if(b not in cnt or not cnt[b]):print(s);continue;
  s-=(cnt[b]*b);s+=(cnt[b]*c);
  if(c not in cnt):cnt[c]=0;
  cnt[c]+=cnt[b];cnt[b]=0;
  print(s)

Solution (Python):
n = int(input())
lisA = list(map(int,input().split()))
dic  = {}
for item in lisA:
    if item in dic:
        dic[item] += 1
    else:
        dic[item] = 1
q = int(input())
ans = sum(lisA)
for i in range(q):
    b,c = map(int,input().split())
    if b not in dic:
        print(ans)
    else:
        tmp = dic[b]
        if c in dic:
            dic[c] += dic[b]
        else:
            dic[c] = dic[b]
        dic[b] = 0
        if b > c:
            ans -= (b-c) * tmp
        else:
            ans += (c-b) * tmp
        print(ans)

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)]

CA = Counter(A)

s = sum(A)

for b, c in BC:
  heri = CA[b] * b
  hue = CA[b] * c
  s += (- heri + hue)
  CA[c] += CA[b]
  CA[b] = 0
  print(s)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")

n = int(input())
a = [int(x) for x in input().split()]
q = int(input())
from collections import Counter
C = Counter(a)
total = sum(a)
for _ in range(q):
    b, c = map(int, input().split())
    C[c] += C[b]
    total += c*C[b]
    total -= b*C[b]
    C[b] = 0
    print(total)

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7






N = getN()
A = getList()
Q = getN()
B = [getNM() for i in range(Q)]

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

ans = sum(A)
for i in range(Q):
    bef, aft = B[i]
    bef_time = num[bef]
    num[bef] = 0
    ans += bef_time * (aft - bef)
    num[aft] += bef_time
    print(ans)

Solution (Python):
import collections
def main():
    n = int(input())
    A=[int(i) for i in input().split()]
    dd = collections.defaultdict(int)
    k = sum(A)
    res = []
    q = int(input())
    for i in A:
        dd[i]+=1
    for i in range(q):
        b,c=map(int, input().split())
        k = k - (b*dd[b])
        k = k + ((c)*(dd[b]))
        dd[c]=dd[c]+dd[b]
        dd[b]=0

        res.append(k)
    for i in res:
        print(i)


if __name__ == '__main__':
    main()

Solution (Python):

import numpy as np
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect


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


def z():
    return int(input())


def S():
    return input()


def C(line):
    return [input() for _ in range(line)]


N = z()
A = zz()
Q = z()
B, C = [], []
for i in range(Q):
    b, c = zz()
    B.append(b)
    C.append(c)
num_lis = [0]*(10**5 + 1)

for a in A:
    num_lis[a] += 1
sum_A = sum(A)

for i in range(Q):
    b, c = B[i], C[i]

    sum_A = sum_A + (num_lis[b])*c - num_lis[b]*b
    
    
    
    num_lis[c] = num_lis[c] + num_lis[b]
    
    num_lis[b] = 0
    
    
    print(sum_A)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
bcs = []
for i in range(Q):
  j,k = map(int,input().split())
  bcs.append([j,k])
s = sum(A)
cnts = [0]*100000
for a in A:
  cnts[a-1]+=1
for b,c in bcs:
  t = cnts[b-1]
  s -= t * b
  cnts[b-1]=0
  cnts[c-1]+=t
  s += t*c 
  print(s)

Solution (Python):


import sys
iim = lambda: map(int, sys.stdin.readline().rstrip().split())
from collections import defaultdict

def resolve():
    it = map(int, sys.stdin.read().split())
    N = next(it)

    d1 = defaultdict(int)
    sum1 = 0
    for i in range(N):
        v = next(it)
        sum1 += v
        d1[v] += 1

    Q = next(it)
    ans = []
    for bi, ci in zip(it, it):
        cnt = d1[bi]
        sum1 += (ci-bi)*cnt
        d1[bi] = 0
        d1[ci] += cnt
        ans.append(sum1)

    print(*ans, sep="\n")

if __name__ == "__main__":
    resolve()

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)
import math
from math import gcd
def lcm(a, b): return a * b // gcd(a, b)
from itertools import count, permutations, chain
from functools import lru_cache
from collections import deque, defaultdict
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok

def get_inv(n, modp):
    return pow(n, modp-2, modp)

def factorials_list(n, modp):    
    fs = [1]
    for i in range(1, n+1):
        fs.append(fs[-1] * i % modp)
    return fs

def invs_list(n, fs, modp):     
    invs = [get_inv(fs[-1], modp)]
    for i in range(n, 1-1, -1):
        invs.append(invs[-1] * i % modp)
    invs.reverse()
    return invs

def comb(n, k, modp):
    num = 1
    for i in range(n, n-k, -1):
        num = num * i % modp
    den = 1
    for i in range(2, k+1):
        den = den * i % modp
    return num * get_inv(den, modp) % modp

def comb_from_list(n, k, modp, fs, invs):   
    return fs[n] * invs[n-k] * invs[k] % modp



class UnionFindEx:
    def __init__(self, size):
        
        self.roots = [-1] * size
    def getRootID(self, i):
        r = self.roots[i]
        if r < 0:   
            return i
        else:
            r = self.getRootID(r)
            self.roots[i] = r
            return r
    def getGroupSize(self, i):
        return -self.roots[self.getRootID(i)]
    def connect(self, i, j):
        r1, r2 = self.getRootID(i), self.getRootID(j)
        if r1 == r2:
            return False
        if self.getGroupSize(r1) < self.getGroupSize(r2):
            r1, r2 = r2, r1
        self.roots[r1] += self.roots[r2]    
        self.roots[r2] = r1
        return True

Yes = 'Yes'
No = 'No'


def main():
    N=ii()
    d = [0] * 10**5 * 2
    ans = 0
    for a in mis():
        d[a] += 1
        ans += a
    Q=ii()
    for _ in range(Q):
        b, c = mis()
        d[c] += d[b]
        ans -= d[b]*b
        ans += d[b]*c
        d[b] = 0
        print(ans)


main()

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

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

for i,j in cnt.items():
    lst[i] = j
ans = sum(A)
for i in range(Q):
    b, c = map(int, input().split())
    ans += lst[b]*(c-b)
    lst[c] += lst[b]
    lst[b] = 0
    print(ans)

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

dic = {}

ans = sum(a)
for i in range(n):
  dic.setdefault(a[i], 0)
  dic[a[i]] += 1

q = int(input())

for i in range(q):
  b,c = map(int, input().split())
  
  dic.setdefault(c,0)
  dic.setdefault(b,0)
  
  tmp = dic.pop(b)
  dic[c] += tmp
  
  ans += (c-b) * tmp
  print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
import collections
c = collections.Counter(a)

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

suma=sum(a)

for [bk,nx] in query:
    
    
    suma=suma-bk*c[bk]+nx*c[bk]
    print(suma)
    c[nx]+=c[bk]
    c[bk]=0

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
a = list(map(int,readline().split()))
q = int(readline())
bc = list(map(int,read().split()))

from collections import defaultdict
d = defaultdict(int)

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

ans = []
si = sum(a)
it = iter(bc)
for b,c in zip(it,it):
    d[c] += d[b]
    si += (c*d[b]) - b*d[b]
    d[b] = 0
    ans.append(si)

print('\n'.join(map(str,ans)))

Solution (Python):
A=int(input())
l=list(map(int,input().split()))
list_change=[0]*100001
for i in l:
   list_change[i]+=1
N=int(input())
total=sum(l)
for i in range(N):
   A,B=list(map(int,input().split()))
   X=list_change[A]
   list_change[A]=0
   list_change[B]+=X
   total+=(B-A)*X
   print(total)

Solution (Python):
def resolve():
    import sys
    sys.setrecursionlimit(10 ** 6)  
    to_index = lambda x: int(x) - 1  
    print_list_in_2D = lambda x: print(*x, sep="\n")  

    
    def input_int():
        return int(input())

    def map_int_input():
        return map(int, input())

    MII = map_int_input

    def MII_split():
        return map(int, input().split())

    def MII_to_index():
        return map(to_index, input())

    def MII_split_to_index():
        return map(to_index, input().split())

    
    def list_int_inputs():
        return list(map(int, input()))

    LII = list_int_inputs

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

    
    def LII_2D(rows_number):
        return [LII() for _ in range(rows_number)]

    def LII_split_2D(rows_number):
        return [LII_split() for _ in range(rows_number)]

    N = input_int()
    As = LII_split()
    Q = input_int()

    d = dict()
    for i in range(int(1e5)):
        d[i + 1] = 0
    for A in As:
        d[A] += 1

    ans = sum(As)
    for _ in range(Q):
        B, C = MII_split()
        d[C] += d[B]
        ans += (C - B) * d[B]
        d[B] = 0
        print(ans)
        
resolve()

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

A = dict()
for i in range(n):
    a = aa[i]
    if a not in A.keys():
        A[a] = 1
    else:
        A[a] += 1

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

ans = sum(aa)
for b, c in bc:
    if b in A.keys() and c in A.keys():
        A[c] += A[b]
        ans = ans - b * A[b] + c * A[b]
        A[b] = 0
    elif b in A.keys() and c not in A.keys():
        A[c] = A[b]
        ans = ans - b * A[b] + c * A[b]
        A[b] = 0
    print(ans)

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



from collections import defaultdict
dic = defaultdict(int)

N = int(readline())
A = list(map(int,readline().split()))
ans = 0
for i in range(len(A)):
  dic[A[i]] += 1
  ans += A[i]

Q = int(readline())

for i in range(Q):
  b,c = map(int,readline().split())
  
  ans += c * dic[b] - b * dic[b]
  print(ans)
  dic[c] += dic[b]
  dic[b] = 0

Solution (Python):
import sys
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
Co = Counter(A)
Q = int(input())
BC = [list(map(int, sys.stdin.readline().split())) for i in range(Q)]
ans = sum(A)
for i in range(Q):
    B, C = BC[i]
    ans += ((C - B) * Co[B])
    print(ans)
    Co[C] += Co[B]
    Co[B] = 0

Solution (Python):
import numpy as np

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



ansArray = np.zeros(10**5 + 1, dtype = int)
indexArray = np.array(range(10**5+1))

for A in As:
    ansArray[A] += 1



ans = np.dot(ansArray, indexArray)

for BC in BCs:

    ans = ans + (BC[1] - BC[0]) * ansArray[BC[0]]
    print(ans)
    ansArray[BC[1]] += ansArray[BC[0]]
    ansArray[BC[0]] = 0

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

li2 = [0for i in range(10 ** 5 + 1)]

for i in range(len(A)):
    li2[A[i]] += 1

sum1 = sum(A)

for i in range(len(li)):
    sum1 += li2[li[i][0]] * (li[i][1] - li[i][0])
    li2[li[i][1]] += li2[li[i][0]]
    li2[li[i][0]] = 0
    print(sum1)

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

Solution (Python):
def d_replacing():
    from collections import Counter
    N = int(input())
    A = [int(i) for i in input().split()]
    Q = int(input())
    Queries = [[int(i) for i in input().split()] for j in range(Q)]

    total = sum(A)
    sequence = Counter(A)

    ans = []
    for b, c in Queries:
        sub = b * sequence[b]
        add = c * sequence[b]
        total += add - sub
        ans.append(total)
        sequence[c] += sequence[b]
        sequence[b] = 0
    return '\n'.join(map(str, ans))

print(d_replacing())