CodeEff/ECCO huggingface dataset:
# Original solution
n=int(eval(input()))
l=list(map(int,input().split()))
q=int(eval(input()))
dic=dict()
list_sum=sum(l)
for i in range(len(l)):
    if(l[i] not in list(dic.keys())):
        dic[l[i]]=1
    else:
        dic[l[i]]+=1
# print(dic)

for i in range(q):
    s=0
    b,c=list(map(int,input().split()))
    if(b in dic and dic[b]!=0):
        # s=(c-b)*dic[b]+list_sum
        if(c in dic):
            dic[c]+=dic[b]
            dic[b]=0
        else:
            dic[c]=dic[b]
            dic[b]=0
    for x in dic:
        if(dic[x]!=0):
            s+=x*dic[x]
    print(s)





# Optimized solution
n=int(eval(input()))
l=list(map(int,input().split()))
q=int(eval(input()))
dic=dict()
list_sum=sum(l)
for i in range(len(l)):
    if(l[i] not in list(dic.keys())):
        dic[l[i]]=1
    else:
        dic[l[i]]+=1
# print(dic)
s=list_sum
for i in range(q):
    b,c=list(map(int,input().split()))
    # print(dic)
    if(b in dic and dic[b]!=0):
        s+=(c-b)*dic[b]
        if(c in dic):
            dic[c]+=dic[b]
            dic[b]=0
        else:
            dic[c]=dic[b]
            dic[b]=0
    
    print(s)
    # print(dic)
# Original solution
n = int(eval(input()))
a = list(map(int,input().split()))
q = int(eval(input()))

import collections
a = collections.Counter(a)
for i in range(q):
    b,c = list(map(int,input().split()))
    d = a[b]
    a[b] = 0
    a[c] += d
    print((sum([key*value for key,value in list(a.items())])))

# Optimized solution
n = int(eval(input()))
a = list(map(int,input().split()))
q = int(eval(input()))

import collections
ans = sum(a)
a = collections.Counter(a)

for i in range(q):
    b,c = list(map(int,input().split()))
    d = a[b]
    ans -= d*b
    a[b] = 0
    ans += d*c
    a[c] += d
    print(ans)
# Original solution
import collections
N=int(eval(input()))
A=list(map(int,input().split()))
Q=int(eval(input()))
d=collections.Counter(A)
for i in range(Q):
  B,C=list(map(int,input().split()))
  if d[C] in d:
    d[C]+=d[B]
  else:
    d[C]=d[B]
  d[B]=0
  ans=0
  for j in d:
    ans += j*d[j]
  print(ans)

# Optimized solution
import collections
N=int(eval(input()))
A=list(map(int,input().split()))
s=sum(A)
Q=int(eval(input()))
d=collections.Counter(A)
for i in range(Q):
  B,C=list(map(int,input().split()))
  s += d[B]*C - d[B]*B
  print(s)
  if C in d:
    d[C]+=d[B]
  else:
    d[C]=d[B]
  d[B]=0
# Original solution
import collections
N=int(eval(input()))
A=list(map(int,input().split()))
s=sum(A)
Q=int(eval(input()))
d=collections.Counter(A)
for i in range(Q):
  B,C=list(map(int,input().split()))
  s += d[B]*C - d[B]*B
  print(s)
  if C in d:
    d[C]+=d[B]
  else:
    d[C]=d[B]
  d[B]=0

# Optimized solution
N=int(eval(input()))
A=list(map(int,input().split()))
s=sum(A)
Q=int(eval(input()))
d=[0]*(10**5 +1)#各数字の個数
for i in A:
  d[i]+=1

for i in range(Q):
  B,C=list(map(int,input().split()))
  s += (C-B)*d[B]
  print(s)
  d[C]+=d[B]
  d[B]=0
# Original solution
N=int(eval(input()))
A=list(map(int,input().split()))
s=sum(A)
Q=int(eval(input()))
d=[0]*(10**5 +1)#各数字の個数
for i in A:
  d[i]+=1

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

# Optimized solution
def main():
  N=int(eval(input()))
  A=list(map(int,input().split()))
  s=sum(A)
  Q=int(eval(input()))
  d=[0]*(10**5 +1)#各数字の個数
  for i in A:
    d[i]+=1

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

if __name__ == "__main__":
    main()
# Original solution
def main():
  N=int(eval(input()))
  A=list(map(int,input().split()))
  s=sum(A)
  Q=int(eval(input()))
  d=[0]*(10**5 +1)#各数字の個数
  ans=[]
  for i in A:
    d[i]+=1

  for i in range(Q):
    B,C=list(map(int,input().split()))
    s += (C-B)*d[B]
    ans.append(s)
    d[C]+=d[B]
    d[B]=0
    
  print(("\n".join(map(str, ans))))

if __name__ == "__main__":
    main()

# Optimized solution
def main():
  N=int(eval(input()))
  A=list(map(int,input().split()))
  s=sum(A)
  Q=int(eval(input()))
  d=[0]*(10**5 +1)#各数字の個数
  ans=[]
  for i in A:
    d[i]+=1

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

  print(("\n".join(map(str, ans))))
  #BC入力ごとに出力するよりまとめて出力した方が倍くらい早い

if __name__ == "__main__":
    main()
# Original solution
from collections import Counter
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
B = [list(map(int, input().split())) for _ in range(Q)]

A_counter = Counter(A)
for b in B:
    cnt = A_counter[b[0]]
    A_counter[b[1]] += cnt
    del A_counter[b[0]]
    print((sum([num * cnt for num, cnt in list(A_counter.items())])))


# Optimized solution
from collections import Counter
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
B = [list(map(int, input().split())) for _ in range(Q)]

res = sum(A)
A_counter = Counter(A)
for i, j in B:
    if i not in A_counter:
        A_counter[i] = 0
    if j not in A_counter:
        A_counter[j] = 0

    res += (j - i) * A_counter[i]
    print(res)
    
    A_counter[j] += A_counter[i]
    A_counter[i] = 0
# Original solution
from collections import Counter
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
B = [list(map(int, input().split())) for _ in range(Q)]

res = sum(A)
A_counter = Counter(A)
for i, j in B:
    if i not in A_counter:
        A_counter[i] = 0
    if j not in A_counter:
        A_counter[j] = 0

    res += (j - i) * A_counter[i]
    print(res)
    
    A_counter[j] += A_counter[i]
    A_counter[i] = 0


# Optimized solution
from collections import Counter
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
B = [list(map(int, input().split())) for _ in range(Q)]

res = sum(A)
A_counter = Counter(A)
for i, j in B:
    # if i not in A_counter:
    #     A_counter[i] = 0
    # if j not in A_counter:
    #     A_counter[j] = 0

    res += (j - i) * A_counter[i]
    print(res)

    A_counter[j] += A_counter[i]
    A_counter[i] = 0
# Original solution
from collections import Counter
n = int(eval(input()))
a = [int(i) for i in input().split()]
cnt_a = Counter(a)
q = int(eval(input()))
s = [sum(a)] + [0] * q
for i in range(q):
    b, c = list(map(int, input().split()))
    bn = cnt_a[b]
    s[i + 1] = s[i] + (c - b)*bn
    cnt_a[b] = 0
    cnt_a[c] += bn
    print((s[i + 1]))

# Optimized solution
from collections import Counter
n = int(input())
a = [int(i) for i in input().split()]
cnt_a = Counter(a)
q = int(input())
s = [sum(a)] + [0] * q
for i in range(q):
    b, c = map(int, input().split())
    bn = cnt_a[b]
    s[i + 1] = s[i] + (c - b)*bn
    cnt_a[b] = 0
    cnt_a[c] += bn
print(*s[1:], sep='\n')
# Original solution
from collections import Counter
n = int(eval(input()))
a = [int(i) for i in input().split()]
cnt_a = Counter(a)
q = int(eval(input()))
s = sum(a)
for i in range(q):
    b, c = list(map(int, input().split()))
    s += (c - b)*cnt_a[b]
    print(s)
    cnt_a[b], cnt_a[c] = 0, cnt_a[c] + cnt_a[b]

# Optimized solution
from collections import Counter
n = int(eval(input()))
a = [int(i) for i in input().split()]
cnt = Counter(a)
q = int(eval(input()))
s = sum(a)
for i in range(q):
    b, c = list(map(int, input().split()))
    s += (c - b)*cnt[b]
    print(s)
    cnt[b], cnt[c] = 0, cnt[c] + cnt[b]
# Original solution
from collections import Counter
n = int(eval(input()))
a = [int(i) for i in input().split()]
cnt = Counter(a)
q = int(eval(input()))
s = sum(a)
for i in range(q):
    b, c = list(map(int, input().split()))
    s += (c - b)*cnt[b]
    print(s)
    cnt[b], cnt[c] = 0, cnt[c] + cnt[b]

# Optimized solution
from collections import Counter
n = int(eval(input()))
a = list(map(int, input().split()))
s = sum(a)
cnt = Counter(a)
q = int(eval(input()))
for i in range(q):
    b, c = list(map(int, input().split()))
    cnt_b = cnt[b]
    cnt[b], cnt[c] = 0, cnt[c] + cnt_b
    s += (c - b)*cnt_b
    print(s)
# Original solution
s=set()
l = [0] * ( 10 ** 5 + 1 )
N = int(eval(input()))
A = list(map(int, input().split()))
for i in range(len(A)):
    l[A[i]] += 1
    s.add(A[i])
Q = int(eval(input()))
for i in range(Q):
    B, C = list(map(int, input().split()))
    if l[B] > 0:
        s.add(C)
        l[C] += l[B]
        l[B] = 0
        s.discard(B)
    sum=0
    for i in s:
        sum += i * l[i]
    print(sum)


# Optimized solution
s=set()
l = [0] * ( 10 ** 5 + 1 )
sum=0
N = int(eval(input()))
A = list(map(int, input().split()))
for i in range(len(A)):
    l[A[i]] += 1
    s.add(A[i])
    sum += A[i]
Q = int(eval(input()))
for i in range(Q):
    B, C = list(map(int, input().split()))
    if l[B] > 0:
        s.add(C)
        sum = sum + l[B] * ( C - B )
        l[C] += l[B]
        l[B] = 0
        s.discard(B)
    print(sum)
# Original solution
N=int(eval(input()))
A=list(map(int,input().split()))
Q=int(eval(input()))

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

S=0
#まずはじめの合計求める

num=[0]
#これに数の出現回数をプロットする
for i in range(1,10**5+1):
    cnt=A.count(i)
    num.append(cnt)
    if cnt!=0:
        S+=cnt*i
        

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

# Optimized solution
N = int(eval(input()))
A = list(map(int,input().split()))
Q = int(eval(input()))
BC = []
for i in range(Q):
    BC.append(list(map(int,input().split())))

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

S = []
 
#1 iterlation
S.append(sum(A))
for i in range(N):
    num[A[i]]+=1

    
for j in range(Q):
    if num[BC[j][0]] != 0:
        #Bに当たる交換するものがある時
        B = BC[j][0]
        C = BC[j][1]
        S.append(S[-1] + (C - B)*num[B] )
        #S[-1]で合計の一番最新のもの
        dammy = num[B]
        num[B] = 0
        num[C] += dammy
 
    else:
        B = BC[j][0]
        C = BC[j][1]
        S.append(S[-1])

        
for i in range(1,Q+1):
    print((S[i]))
# Original solution
n= int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
b = [list(map(int,input().split())) for i in range(q)]
num = [0]*(10**5+1)
for i in a:
  num[i] = a.count(i)
sum_a = sum(a)

for i in range(q):
  dif = b[i][1]-b[i][0]
  #print(dif)
  sum_a += dif*num[b[i][0]]
  num[b[i][1]] += num[b[i][0]]
  num[b[i][0]] = 0
  print(sum_a)

# Optimized solution
n= int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
b = [list(map(int,input().split())) for i in range(q)]
num = [0]*(10**5+1)
for i in a:
  num[i] += 1
sum_a = sum(a)

for i in range(q):
  dif = b[i][1]-b[i][0]
  #print(dif)
  sum_a += dif*num[b[i][0]]
  num[b[i][1]] += num[b[i][0]]
  num[b[i][0]] = 0
  print(sum_a)
# Original solution
import collections

def main():
    n = int(eval(input()))
    a = list(map(int, input().split()))
    q = int(eval(input()))

    a_dict = collections.Counter(a)
    a_dict = {key: key*value for key, value in list(a_dict.items())}
    for _ in range(q):
        b, c = list(map(int, input().split()))
        if b in a_dict:
            if c in a_dict:
                a_dict[c] += (a_dict.pop(b)//b) * c
            else:
                a_dict[c] = (a_dict.pop(b)//b) * c
        print((sum(a_dict.values())))


if __name__ == '__main__':
    main()


# Optimized solution
import collections

def main():
    n = int(eval(input()))
    a = list(map(int, input().split()))
    q = int(eval(input()))

    a_dict = collections.Counter(a)
    a_dict = {key: key*value for key, value in list(a_dict.items())}
    answer = sum(a_dict.values())
    for _ in range(q):
        b, c = list(map(int, input().split()))
        diff = 0
        if b in a_dict:
            b_sum = a_dict.pop(b)
            c_add = b_sum//b * c
            diff = c_add - b_sum
            if c in a_dict:
                a_dict[c] += c_add
            else:
                a_dict[c] = c_add
        answer += diff
        print(answer)


if __name__ == '__main__':
    main()
# Original solution
def resolve():
    n = int(eval(input()))
    a = list(map(int, input().split()))
    count = [0 for _ in range(10**5)]
    for i in range(n):
        count[a[i] - 1] += 1

    q = int(eval(input()))
    for _ in range(q):
        b, c = list(map(int, input().split()))
        count[c - 1] += count[b - 1]
        count[b - 1] = 0
        total = 0
        for i in range(10**5):
            if count[i] == 0:
                continue
            total += count[i] * (i + 1)
        print(total)


if 'unittest' not in globals():
    resolve()

# Optimized solution
def resolve():
    n = int(eval(input()))
    a = list(map(int, input().split()))
    total = 0
    count = [0 for _ in range(10**5)]
    for i in range(n):
        count[a[i] - 1] += 1
        total += a[i]

    q = int(eval(input()))
    for _ in range(q):
        b, c = list(map(int, input().split()))
        count_b = count[b - 1]
        count[b - 1] = 0
        count[c - 1] += count_b

        total -= count_b * (b - c)
        print(total)


if 'unittest' not in globals():
    resolve()
# Original solution
import collections
N=int(eval(input()))
A=list(map(int,input().split()))
Q=int(eval(input()))
M=collections.Counter(A)
for i in range(Q):
  B,C=list(map(int,input().split()))
  M[C]+=M[B]
  M[B]=0
  ans=0
  for j in list(M.items()):
    ans+=j[0]*j[1]
  print(ans)

# Optimized solution
import collections
N=int(eval(input()))
A=list(map(int,input().split()))
Q=int(eval(input()))
M=collections.Counter(A)
ans=0
for j in list(M.items()):
  ans+=j[0]*j[1]
for i in range(Q):
  B,C=list(map(int,input().split()))
  ans+=(C-B)*M[B]
  M[C]+=M[B]
  M[B]=0
  print(ans)
# Original solution
N = int(eval(input()))
A = list(map(int,input().split()))
Q = int(eval(input()))
B = []
C = []
for i in range(Q):
    b,c = list(map(int,input().split()))
    B.append(b)
    C.append(c)
S = sum(A)
NUM = [0]
for j in range(1,100005):
    num_j = A.count(j)
    NUM.append(num_j)
for k in range(Q):
    S += (C[k]-B[k]) * (NUM[B[k]])
    NUM[C[k]] += NUM[B[k]]
    NUM[B[k]] = 0
    print(S)

# Optimized solution
N = int(eval(input()))
A = list(map(int,input().split()))
Q = int(eval(input()))
B = []
C = []
for i in range(Q):
    b,c = list(map(int,input().split()))
    B.append(b)
    C.append(c)
S = sum(A)
NUM = [0] * 100005
for a in A:
    NUM[a] += 1
for k in range(Q):
    S += (C[k]-B[k]) * (NUM[B[k]])
    NUM[C[k]] += NUM[B[k]]
    NUM[B[k]] = 0
    print(S)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
bucket = [0]*100001

for i in range(100000):
    bucket[i] = A.count(i)

sum = sum(A)

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

# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(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)
# Original solution
n=int(eval(input()))
a=list(map(int,input().split()))
dic={}
for i in range(n):
  if a[i] in dic:
    dic[a[i]]+=1
  else:
    dic[a[i]]=1
q=int(eval(input()))
for i in range(q):
  b,c=list(map(int,input().split()))
  if b in dic:
    if c in dic:
      dic[c]+=dic[b]
      del dic[b]
    else:
      dic[c]=dic[b]
      del dic[b]
  ans=0
  for key,value in list(dic.items()):
    ans+=key*value
  print(ans)

# Optimized solution
n=int(eval(input()))
a=list(map(int,input().split()))
dic={}
for i in range(n):
  if a[i] in dic:
    dic[a[i]]+=1
  else:
    dic[a[i]]=1
q=int(eval(input()))
ans=0
for key,value in list(dic.items()):
  ans+=key*value
for i in range(q):
  b,c=list(map(int,input().split()))
  if b in dic:
    if c in dic:
      dic[c]+=dic[b]
      ans+=dic[b]*(c-b)
      del dic[b]
    else:
      dic[c]=dic[b]
      ans+=dic[b]*(c-b)
      del dic[b]
  print(ans)
# Original solution
n=int(eval(input()))
l=[0]*10**5
A=list(map(int,input().split()))

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

q=int(eval(input()))
for i in range(q):
    b,c=list(map(int,input().split()))
    tmp=l[b-1]
    l[b-1]=0
    l[c-1]+=tmp
    
    ans=0
    for j in range(10**5):
        ans+=l[j]*(j+1)
    print(ans)

# Optimized solution
n=int(eval(input()))
l=[0]*10**5
A=list(map(int,input().split()))

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

ans=sum(A)

q=int(eval(input()))
for i in range(q):
    b,c=list(map(int,input().split()))
    tmp=l[b-1]
    l[b-1]=0
    l[c-1]+=tmp
    
    ans+=tmp*(c-b)
    
    print(ans)
# Original solution
from collections import Counter

N = int(eval(input()))
A = [int(x) for x in input().split()]
d = Counter(A)
Q = int(eval(input()))

def sum_from_counter(d):
  return sum([k * d[k] for k in d])

for i in range(Q):
  B, C = [int(x) for x in input().split()]
  d[C] = d.get(C, 0) + d[B]
  del d[B]
  print((sum_from_counter(d)))

# Optimized solution
from collections import Counter

N = int(eval(input()))
A = [int(x) for x in input().split()]
d = Counter(A)
Q = int(eval(input()))

def sum_from_counter(d):
  return sum([k * d[k] for k in d])

s = sum([k * d[k] for k in d])
for i in range(Q):
  B, C = [int(x) for x in input().split()]
  d[C] = d.get(C, 0) + d[B]
  s += (C - B) * d[B]
  d[B] = 0
  print(s)
# Original solution
s = int(eval(input())) 

dict={}
num_list = list(map(int, input().split()))

length=len(num_list)
count=[0]*(s+1000)
for i in range(length):
    count[num_list[i]]+=1
    dict[num_list[i]]=count[num_list[i]]
    
#print(dict)


sum=0
count=0
n = int(eval(input())) 
for i in range(n):
    a,b = list(map(int, input().split()))  
    if dict.get(a)!=None:
        if dict.get(b)!=None:
            count=dict[b]
            dict[b]=dict[a]+dict[b]
        else:
            dict[b]=dict[a]
        dict[a]=0
    else:
        pass
    #print(dict)
    for k, v in list(dict.items()):
        sum+=k*v
    print(sum)
    sum=0

   


# Optimized solution
s = int(eval(input())) 

dict={}
num_list = list(map(int, input().split()))

length=len(num_list)
count=[0]*(s+1000)
for i in range(length):
    count[num_list[i]]+=1
    dict[num_list[i]]=count[num_list[i]]
    
sum=0
#print(dict)
for k, v in list(dict.items()):
    sum+=k*v
#print(dict)
#print(sum)

count=0
d=0
n = int(eval(input())) 
for i in range(n):
    a,b = list(map(int, input().split()))  
    if dict.get(a)!=None:
        d=(b-a)*dict[a]
        #print("d:{}".format(d))
        if dict.get(b)!=None:
            count=dict[b]
            dict[b]=dict[a]+dict[b]
        else:
            dict[b]=dict[a]
        dict[a]=0
        sum+=d
        
        #print(dict)
        
    else:
        pass
    d=0
    print(sum)
    #print()
# Original solution
from collections import defaultdict
N = int(eval(input()))
A = list(map(int, input().split()))
D = defaultdict(int)
for a in A:
  D[a] += 1
Q = int(eval(input()))
B = []
C = []
for _ in range(Q):
  l = list(map(int, input().split()))
  B.append(l[0])
  C.append(l[1])

for i in range(Q):
  k = D[B[i]]
  D.pop(B[i])
  D[C[i]] += k
  ans = 0
  for k, v in list(D.items()):
    ans += k * v
  print(ans)

# Optimized solution
from collections import defaultdict
N = int(eval(input()))
A = list(map(int, input().split()))
D = defaultdict(int)
for a in A:
  D[a] += 1
Q = int(eval(input()))
B = []
C = []
for _ in range(Q):
  l = list(map(int, input().split()))
  B.append(l[0])
  C.append(l[1])

ans = sum(A)
for i in range(Q):
  k = D[B[i]]
  D[B[i]] = 0
  D[C[i]] += k
  ans = ans - (B[i]*k) + (C[i]*k)
  print(ans)
# Original solution
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy

if __name__ == "__main__":
    n = int(eval(input()))
    a = list(map(int,input().split()))
    q = int(eval(input()))
    cnt = [0]*(10**5+7)
    for i in range(n):
        cnt[a[i]]+=1
    ruiseki = sum(a)
    for i in range(q):
        b,c = list(map(int,input().split()))
        d = cnt[b]
        e = d*(c-b)
        cnt[c] += d
        cnt[b] -= d
        ruiseki += e
        print(ruiseki)

# Optimized solution
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy

def main():
    n = int(eval(input()))
    a = list(map(int,input().split()))
    q = int(eval(input()))
    cnt = [0]*(10**5+7)
    for i in range(n):
        cnt[a[i]]+=1
    p = sum(a)
    for i in range(q):
        b,c = list(map(int,input().split()))
        d = cnt[b]
        e = cnt[c]
        diff = d*(c-b)
        p += diff
        cnt[c]+=d
        cnt[b]-=d
        print(p)
if __name__ == "__main__":
    main()
# Original solution
def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    Q = int(eval(input()))
    BC = [list(map(int, input().split())) for i in range(Q)]

    g = {}
    for a in A:
        if a not in g:
            g[a] = 1
        else:
            g[a] += 1

    for i in range(Q):
        B, C = tuple(BC[i])
        if B in g:
            t = g.pop(B)
            if C in g:
                g[C] += t
            else:
                g[C] = t
        ans = 0
        for k, v in list(g.items()):
            ans += k * v
        print(ans)


if __name__ == "__main__":
    main()


# Optimized solution
def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    Q = int(eval(input()))
    BC = [list(map(int, input().split())) for i in range(Q)]

    g = {}
    ans = 0
    for a in A:
        ans += a
        if a not in g:
            g[a] = 1
        else:
            g[a] += 1

    for i in range(Q):
        B, C = tuple(BC[i])
        if B in g:
            t = g.pop(B)
            ans += t * (C - B)
            if C in g:
                g[C] += t
            else:
                g[C] = t
        print(ans)


if __name__ == "__main__":
    main()
# Original solution
n=int(eval(input()))
a=list(map(int,input().split()))
q=int(eval(input()))
b=[]
c=[]
for _ in range(q):
    x,y=list(map(int,input().split()))
    b.append(x)
    c.append(y)

d=[0]*100001
for i in a:
    d[i]+=1

for m,p in zip(b,c):
    minus=d[m]
    d[m]=0
    d[p]+=minus
    ans=0
    for i in range(len(d)):
        ans+=i*d[i]
    print(ans)


# Optimized solution
n=int(eval(input()))
a=list(map(int,input().split()))
q=int(eval(input()))
b=[]
c=[]
for _ in range(q):
    x,y=list(map(int,input().split()))
    b.append(x)
    c.append(y)

d=[0]*100001
for i in a:
    d[i]+=1

ans=sum(a)
for m,p in zip(b,c):
    minus=d[m]
    d[m]=0
    d[p]+=minus
    ans+=(p-m)*minus
    print(ans)
# Original solution
from collections import Counter
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(100000)


def main():
    N = int(input().strip())
    A_list = [int(i) for i in input().strip().split()]
    Q = int(input().strip())
    _sum = sum(A_list)
    num_count = Counter(A_list)
    for _ in range(Q):
        B, C = [int(i) for i in input().strip().split()]
        if B in num_count:
            if C in num_count:
                num_count[C] = num_count[C] + num_count[B]
            else:
                num_count[C] = num_count[B]
            del num_count[B]
            _sum = 0
            for k, v in list(num_count.items()):
                _sum += k * v
            print(_sum)
        else:
            print(_sum)


if __name__ == "__main__":
    main()


# Optimized solution
from collections import Counter
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(100000)


def main():
    N = int(input().strip())
    A_list = [int(i) for i in input().strip().split()]
    Q = int(input().strip())
    _sum = sum(A_list)
    num_count = Counter(A_list)
    for _ in range(Q):
        B, C = [int(i) for i in input().strip().split()]
        if B in num_count:
            if C in num_count:
                num_count[C] = num_count[C] + num_count[B]
            else:
                num_count[C] = num_count[B]

            _sum = _sum - (num_count[B] * B) + num_count[B] * C
            del num_count[B]
            print(_sum)
        else:
            print(_sum)


if __name__ == "__main__":
    main()