CodeEff/ECCO huggingface dataset:
# Original solution
import collections
n = int(eval(input()))
li = list(map(int,input().split()))
q = int(eval(input()))
num_li=[list(map(int,input().split())) for i in range(q)]


c = collections.Counter(li)

for i,j in num_li:
    s = 0
    if i in c:
        cnt = c.pop(i)
        c[j] += cnt
    for k, v in list(c.items()):
        s += k * v
    print(s)

# Optimized solution
import collections
n = int(eval(input()))
li = list(map(int,input().split()))
q = int(eval(input()))
num_li=[list(map(int,input().split())) for i in range(q)]

c = collections.Counter(li)

li_sum = sum(li)

for i,j in num_li:
    s = 0
    if i in c:
        cnt = c.pop(i)
        c[j] += cnt
        li_sum = li_sum - (i*cnt) + (j*cnt)
    print(li_sum)
# Original solution
n = int(eval(input()))
a = list(map(int,input().split()));
q = int(eval(input()))
aa = 0;
for i in range(n):
    aa += a[i];

b = [input().split() for l in range(q)]

bu = [0]*(100000);

for i in range(100000):
    bu[i] = a.count(i+1);
    
for x in range(q):
    c = int(b[x][0])
    d = int(b[x][1])
    aa += (d-c)*(bu[c-1]);
    bu[d-1] += bu[c-1];
    bu[c-1] = 0;
    print(aa);

# Optimized solution

n = int(eval(input()))
a = list(map(int,input().split()));
q = int(eval(input()))
aa = 0;
for i in range(n):
    aa += a[i];

b = [input().split() for l in range(q)]

bu = [0]*(100000);

for i in range(n):
    bu[a[i]-1] += 1;
    
for x in range(q):
    c = int(b[x][0])
    d = int(b[x][1])
    aa += (d-c)*(bu[c-1]);
    bu[d-1] += bu[c-1];
    bu[c-1] = 0;
    print(aa);
# Original solution
import collections

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

c = collections.Counter(a)

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

    if(C not in c):
        c[C] = 0

    c[C] += c[B]
    del c[B]

    sum = 0
    for key, cnt in list(c.items()):
        sum += key*cnt

    print(sum)

# Optimized solution
import collections

N = int(eval(input()))
a = list(map(int,input().split()))
su = sum(a)

Q = int(eval(input()))

c = collections.Counter(a)

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

    if(C not in c):
        c[C] = 0

    num_B = c[B] # 配列に含まれるBの個数
    c[B] = 0
    c[C] += num_B
    # c[C] += c[B]
    # del c[B]

    # for key, cnt in c.items():
    #     sum -= items():
    #     sum += key*cnt

    su -= num_B * B # bのやつを減らす
    su += num_B * C # cのやつを増やすitems():

    print(su)
# Original solution
import collections
n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
data = collections.Counter(a)
count, key = list(data.values()), list(data.keys())

for i in range(q):
    b, c = list(map(int, input().split()))
    if b in key and c in key:
        count[key.index(c)] += count[key.index(b)]
        count[key.index(b)] = 0
    elif b in key:
        key.append(c)
        count.append(count[key.index(b)])
        count[key.index(b)] = 0
    res = 0
    for j in range(len(key)):
        res += count[j] * key[j]
    print(res)




# Optimized solution
n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
data = [0] * (10**5 + 1)

for i in a:
    data[i] += 1
res = sum(a)

for j in range(q):
    b, c = list(map(int, input().split()))
    res += (c - b) * data[b]
    data[c] += data[b]
    data[b] = 0
    print(res)
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
bc = [list(map(int,input().split())) for _ in range(q)]

# sum_first  = sum(a)

for i in bc:
    a = [i[1] if j==i[0] else j for j in a]
    # print(a)
    print((sum(a)))

# Optimized solution
n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
query = [list(map(int,input().split())) for _ in range(q)]

all_sum  = sum(a)

cnt = {}
for i in a:
    if i not in cnt:
        cnt[i] = 0
    cnt[i] += 1

for (b, c) in query:
    if b in cnt:
        all_sum -= b * cnt[b]
        all_sum += c * cnt[b]
        if c not in cnt:
            cnt[c]=0 
        cnt[c] += cnt[b]
        cnt[b] = 0
    print(all_sum)
# Original solution
n = int(eval(input()))
lst = [int(i) for i in input().split()]
dic = {}
for i in range(n):
  key = lst[i]
  if key not in dic:
    dic[key] = 0
  dic[key] += 1
#print(dic)
m = int(eval(input()))
for _ in range(m):
  sum = 0
  lst_s = list(map(int, input().split()))
  number = dic.get(lst_s[0])
  if number is not None:
    if lst_s[1] in dic:
      dic[lst_s[1]] += number
    else:
      dic[lst_s[1]] = number
    dic.pop(lst_s[0])
  for key, value in list(dic.items()):
    sum += key * value
  print(sum)

# Optimized solution
n = int(eval(input()))
lst = [int(i) for i in input().split()]
dic = {}
for i in range(n):
  key = lst[i]
  if key not in dic:
    dic[key] = 0
  dic[key] += 1
#print(dic)
sum = 0
for key, value in list(dic.items()):
  sum += key * value

m = int(eval(input()))
for _ in range(m):
  lst_s = list(map(int, input().split()))
  number = dic.get(lst_s[0])
  if number is not None:
    if lst_s[1] in dic:
      dic[lst_s[1]] += number
    else:
      dic[lst_s[1]] = number
    sum += lst_s[1] * number
    sum -= lst_s[0] * number
    dic.pop(lst_s[0])
  print(sum)
# Original solution
import collections
eval(input())
math_dic = collections.Counter((list(map(int,input().split()))))
num = int(eval(input()))
for i in range(num):
    a,b = list(map(int,input().split()))
    if a not in math_dic:
        print((sum([index*value for index,value in list(math_dic.items())])))
        continue
    if b not in math_dic:
        math_dic[b] = 0
    math_dic[b] += math_dic.pop(a)
    print((sum([index*value for index,value in list(math_dic.items())])))

# Optimized solution
import collections
eval(input())
math_dic = collections.Counter((list(map(int,input().split()))))
num = int(eval(input()))
total_num = sum([index*value for index,value in list(math_dic.items())])
for i in range(num):
    a,b = list(map(int,input().split()))
    if a not in math_dic:
        print(total_num)
        continue
    if b not in math_dic:
        math_dic[b] = 0
    total_num -= a*math_dic[a]
    total_num += b*math_dic[a]
    math_dic[b] += math_dic.pop(a)
    print(total_num)
# Original solution
import sys
input = sys.stdin.buffer.readline

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

from collections import Counter
C = Counter(A)
C = dict(C)
S = sum(A)
for i in range(q):
    b, c = list(map(int, input().split()))
    if b in C:
        vb = C[b]
        C[b] = 0
        S -= b*vb
        S += c*vb
        print(S)
        if c in C:
            C[c] += vb
        else:
            C[c] = vb
    else:
        print(S)


# Optimized solution
import sys
input = sys.stdin.buffer.readline

n = int(eval(input()))
A = list(map(int, input().split()))
S = sum(A)

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

q = int(eval(input()))
for i in range(q):
    b, c = list(map(int, input().split()))
    S -= D[b]*b
    S += D[b]*c
    D[c] += D[b]
    D[b] = 0
    print(S)
# Original solution
from collections import Counter
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
A = Counter(A)
bc = [tuple(map(int, input().split())) for i in range(Q)]

ans = 0
for b, c in bc:
    v = A.pop(b, None)
    if v:
        A[c] += v
    ans = 0
    ans = sum(i*j for i, j in list(A.items()))
    print(ans)

# Optimized solution
from collections import Counter
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
ans = sum(A)
A = Counter(A)
bc = [tuple(map(int, input().split())) for i in range(Q)]
for b, c in bc:
    v = A.pop(b, None)
    if v:
        A[c] += v
        ans = ans+((c-b)*v)
    print(ans)
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))

m = 10**5+1
nums = [0]*m
for i in a: nums[i] += 1

for _ in range(q):
  b,c = list(map(int, input().split()))
  nums[c] += nums[b]
  nums[b] = 0
  ans = 0
  for i in range(1,m): ans += i*nums[i]
  print(ans)

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

m = 10**5+1
nums = [0]*m
ans = 0
for i in a:
  nums[i] += 1
  ans += i
  
for _ in range(q):
  b,c = list(map(int, input().split()))
  ans -= b*nums[b]
  ans += c*nums[b]
  nums[c] += nums[b]
  nums[b] = 0
  print(ans)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))

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

A = list(set(A))
for i in range(Q):
  B, C = list(map(int, input().split()))
  count[C] += count[B]
  count[B] = 0
  ans = 0
  for i in range(10**5+1):
    ans += count[i] * i
  print(ans)
  

# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
S = sum(A)
count = [0]*(10**5+1)
for i in range(len(A)):
  count[A[i]] += 1

A = list(set(A))
for i in range(Q):
  B, C = list(map(int, input().split()))
  S = S - B*count[B] + C*count[B]
  count[C] += count[B]
  count[B] = 0
  print(S)
# Original 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)
for i in range(n):
    x=a[i]
    num[x]+=1


for k in range (q):
    b=bc[k][0]
    c=bc[k][1]
    num[c]+=num[b]
    num[b]=0
    kk=0
    for l in range(10**5+1):
        kk+=l*num[l]
    print(kk)

# 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)
for i in range(n):
    x=a[i]
    num[x]+=1

kk=0
for l in range(10**5+1):
    kk+=l*num[l]

for k in range (q):
    b=bc[k][0]
    c=bc[k][1]
    kk+=c*num[b]-b*num[b]
    num[c]+=num[b]
    num[b]=0
    print(kk)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
A_cnt = [0] * 100001
A_sum = 0

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

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

    for i, x in enumerate(A_cnt):
        if A_cnt[i] > 0:
            A_sum = A_sum + (i * A_cnt[i])
    print(A_sum)
    A_sum = 0

# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
A_cnt = [0] * 100001
A_sum = 0

# 各数値の数を数えておく
for a in A:
    A_cnt[a]+=1
    # ついでに現時点の総和も出しておく
    A_sum+=a

for _ in range(Q):
    B, C = list(map(int, input().split()))
    A_cnt[C] = A_cnt[C] + A_cnt[B]
    A_sum = A_sum + ((C-B) * A_cnt[B])
    A_cnt[B] = 0
    print(A_sum)
# Original solution
n, *bc = list(map(int, open(0).read().split()))
a = [bc.pop(0) for _ in range(n)]
q = bc.pop(0)

d = {}
for x in a:
    if x in d:
        d[x] += 1
    else:
        d[x] = 1
ret = sum(a)
for i in range(0, min(q*2, len(bc)), 2):
    b = bc[i]
    c = bc[i+1]
    if b in d:
        v = d.pop(b)
        if c in d:
            d[c] += v
        else:
            d[c] = v
        ret += c * v - b * v
    #ret = sum([k * v for k, v in d.items()])
    print(ret)

# Optimized solution
n, r = open(0).read().split(maxsplit=1)
n = int(n)
*a, r = r.split(maxsplit=n)
a = list(map(int, a))
q, *bc = list(map(int, r.split()))

d = {}
for x in a:
    if x in d:
        d[x] += 1
    else:
        d[x] = 1
ret = sum(a)
for i in range(0, min(q*2, len(bc)), 2):
    b = bc[i]
    c = bc[i+1]
    if b in d:
        v = d.pop(b)
        if c in d:
            d[c] += v
        else:
            d[c] = v
        ret += c * v - b * v
    #ret = sum([k * v for k, v in d.items()])
    print(ret)
# Original solution
n, r = open(0).read().split(maxsplit=1)
n = int(n)
*a, r = r.split(maxsplit=n)
a = list(map(int, a))
q, *bc = list(map(int, r.split()))

d = {}
for x in a:
    if x in d:
        d[x] += 1
    else:
        d[x] = 1
ret = sum(a)
for i in range(0, min(q*2, len(bc)), 2):
    b = bc[i]
    c = bc[i+1]
    if b in d:
        v = d.pop(b)
        if c in d:
            d[c] += v
        else:
            d[c] = v
        ret += c * v - b * v
    #ret = sum([k * v for k, v in d.items()])
    print(ret)


# Optimized solution
n, *r = list(map(int, open(0).read().split()))
a = r[:n]
q = r[n]
bc = r[n+1:]

d = {}
for x in a:
    if x in d:
        d[x] += 1
    else:
        d[x] = 1
ret = sum(a)
for i in range(0, min(q*2, len(bc)), 2):
    b = bc[i]
    c = bc[i+1]
    if b in d:
        v = d.pop(b)
        if c in d:
            d[c] += v
        else:
            d[c] = v
        ret += c * v - b * v
    #ret = sum([k * v for k, v in d.items()])
    print(ret)
# Original solution
from collections import deque
from bisect import bisect_left
n=int(eval(input()))
a=sorted(list(map(int,input().split())))

sus=deque([sum(a)])
q=int(eval(input()))
for i in range(q):
  count=0
  b,c=list(map(int,input().split()))
  idx=bisect_left(a,b)
  while True:
    if idx>=n:break
    if a[idx]==b:
      a[idx]=c
      count+=1
      idx+=1
    else:
      break
  a=sorted(a)
  sus.append(sus[-1]+(c-b)*count)
  print((sus[-1]))

# Optimized solution
n=int(eval(input()))
a=[0]*(10**5)
A=list(map(int,input().split()))
su=0
for i in range(n):
  su+=A[i]
  a[A[i]-1]+=1
q=int(eval(input()))
sus=[su]
for i in range(q):
  
  b,c=[int(x)-1 for x in input().split()]
  sus.append(sus[-1]+(c-b)*a[b])
  a[c]+=a[b]
  a[b]-=a[b]
  print((sus[-1]))
# Original solution
from collections import defaultdict

n = int(eval(input()))

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

dic = defaultdict(int)
for num in nums:
    dic[num] += 1 


answers = []
q = int(eval(input()))
for _ in range(q):
    b, c = list(map(int, input().split()))
    dic[c] += dic[b]
    dic[b] = 0

    answer = 0
    for i, v in list(dic.items()):
        answer += i * v

    answers.append(answer)

for answer in answers:
    print(answer)

# Optimized solution
from collections import Counter

n = int(eval(input()))

nums = tuple(map(int, input().split()))

count = Counter(nums)
s = sum(nums)

answers = []
q = int(eval(input()))
for _ in range(q):
    b, c = list(map(int, input().split()))
    s = s + (c - b) * count[b]
    count[c] += count[b]
    count[b] = 0
    answers.append(s)
    
for answer in answers:
    print(answer)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
BCs = [list(map(int,input().split())) for _ in range(Q)]
max_A = max(A)

# Aの値について数列を作る
A_num = [A.count(i) for i in range(10 ** 5 + 1)]
Si = sum(A)

for BC in BCs:
    # BC[0] ...　置換前の値
    # BC[1] ...　置換後の値
    
    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)

# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
BCs = [list(map(int,input().split())) for _ in range(Q)]
max_A = max(A)

# Aの値について数列を作る
# A_num = [A.count(i) for i in range(10 ** 5 + 1)]

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:
    # BC[0] ...　置換前の値
    # BC[1] ...　置換後の値
    
    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)
# Original solution
#!/usr/bin/env python3
def main():
    import sys

    input = sys.stdin.readline

    _ = int(eval(input()))
    A = [int(x) for x in input().split()]
    Q = int(eval(input()))

    lst = [0 for _ in [0] * (10 ** 5 + 1)]
    sum_lst = 0
    for n in range(10 ** 5 + 1):
        lst[n] = A.count(n)
        sum_lst += n * lst[n]

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

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


if __name__ == '__main__':
    main()


# Optimized solution
#!/usr/bin/env python3
def main():
    import sys

    input = sys.stdin.readline

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

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

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


if __name__ == '__main__':
    main()
# Original solution
# -*- coding: utf-8 -*-
import collections

N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
xy = [list(map(int, input().split())) for _ in range(Q)]
x, y = [list(i) for i in zip(*xy)]


def replace_list(num_list):
    num_list = [y[index] if j==i else j for j in num_list]
    return num_list




for index, i in enumerate(x):
    #first_sum = sum(A)
    #c = collections.Counter(A)
    #first_sum = c[i]*(y[index]-i)+first_sum
    while i in A :
        A.remove(i)  #全てのiを削除します。
        A.append(y[index])
    first_sum = sum(A)
    print(first_sum)
    


# Optimized solution
# -*- coding: utf-8 -*-
import collections

N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
xy = [list(map(int, input().split())) for _ in range(Q)]
b, c = [list(i) for i in zip(*xy)]

sum_num = sum(A)
counter = collections.Counter(A)

for i in range(Q):
    sum_num -= counter[b[i]]*b[i] #counter[x]でxの個数をカウント
    sum_num += counter[b[i]]*c[i] #cで上書きされたので、c*counter[b]になる
    counter[c[i]] += counter[b[i]]
    counter[b[i]] = 0
    print(sum_num)
# Original solution
from collections import Counter
n=int(eval(input()))
a = Counter(list(map(int,input().split())))
q=int(eval(input()))

last = sum([v*a[v] for v in a])
for _ in range(q):
  b,c = list(map(int,input().split()))
  last = last - b*a.get(b,0) + c*a.get(b,0)
  a[b],a[c] = 0, a.get(c,0)+a.get(b,0)
  print(last)
  
  
  

# Optimized solution
n=int(eval(input()))
A = list(map(int,input().split()))
B = {}
s = 0
for a in A:
  B[a] = B.get(a,0)+1
  s += a
q=int(eval(input()))
for _ in range(q):
  b,c=list(map(int,input().split()))
  s = s - b*B.get(b,0) + c*B.get(b,0)
  B[c] = B.get(c,0)+B.get(b,0)
  B[b] = 0
  print(s)
# Original solution
from collections import Counter

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

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

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


# Optimized solution
from collections import Counter

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

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

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

num = []
for i in range(100001):
  num.append(a.count(i))

sum = sum(a)
for i in range(q):
  sum = sum + (c[i] - b[i]) * num[b[i]]
  print(sum)
  num[c[i]] += num[b[i]]
  num[b[i]] = 0

# Optimized solution
from collections import Counter

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

counter = Counter(a)
sum_res = sum(a)

for _ in range(q):
  b, c = list(map(int, input().split()))

  sum_res += (c - b) * counter[b]

  counter[c] += counter[b]
  counter[b] =0

  print(sum_res)
# Original solution
from collections import Counter 

n = int(eval(input()))
a = list(map(int, input().split()))
co = Counter(a)
q = int(eval(input()))
for _ in range(q):
    b, c = list(map(int, input().split()))
    if b in list(co.keys()):
        co[c] += co[b]
        co[b] = 0
    ans = 0
    for i in list(co.items()):
        ans += i[0] * i[1]
    print(ans)

# Optimized solution
from collections import Counter 

n = int(eval(input()))
a = list(map(int, input().split()))
co = Counter(a)
q = int(eval(input()))
ans = 0
for i in list(co.items()):
    ans += i[0] * i[1]
for _ in range(q):
    b, c = list(map(int, input().split()))
    if b in list(co.keys()):
        ans -= b * co[b]
        co[c] += co[b]
        ans += c * co[b]
        co[b] = 0
    print(ans)
# Original solution
from collections import Counter
def get_sum(count):
    return sum([key * count[key] for key in list(count.keys())])
n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
count = Counter(a)
for i in range(q):
    b, c = list(map(int, input().split()))
    count[c] += count[b]
    count[b] = 0
    print((get_sum(count)))

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

def main():
    n = int(eval(input()))
    a = [int(x) for x in input().split()]
    ans = sum(a)
    q = int(eval(input()))
    d = dict((x,a.count(x)) for x in set(a))
    # Counterを敢えて使わないでやってみる
    # d = Counter(a)
    for _ in range(q):
        b, c = list(map(int, input().split()))
        # cの値がない場合の対処
        d.setdefault(c, 0)
        # bの値がない場合の対処
        d[c] = d.get(c) + d.get(b, 0)
        # 差分はbの個数 * (c-b) 
        diff = d.get(b, 0) * (c-b)
        d[b] = 0
        ans += diff
        print(ans)

if __name__ == '__main__':
    main()

# Optimized solution
from collections import Counter

def main():
    n = int(eval(input()))
    a = [int(x) for x in input().split()]
    ans = sum(a)
    q = int(eval(input()))
    #d = dict((x,a.count(x)) for x in set(a))
    d = Counter(a)
    for _ in range(q):
        b, c = list(map(int, input().split()))
        # cの値がない場合の対処
        d.setdefault(c, 0)
        # bの値がない場合の対処
        d[c] = d.get(c) + d.get(b, 0)
        # 差分はbの個数 * (c-b) 
        diff = d.get(b, 0) * (c-b)
        d[b] = 0
        ans += diff
        print(ans)
        
if __name__ == '__main__':
    main()
# Original solution
from collections import*
_,A,_,*Q=[list(map(int,l.split()))for l in open(0)];A=[a for a in A];s=sum(A);C=Counter(A)
for b,c in Q:s+=(c-b)*C[b];C[c]+=C[b];C[b]=0;print(s)

# Optimized solution
from collections import*
_,A,_,*Q=[list(map(int,l.split()))for l in open(0)];s=sum(A:=[a for a in A]);C=Counter(A)
for b,c in Q:s+=(c-b)*C[b];C[c]+=C[b];C[b]=0;print(s)
# Original solution
n=int(eval(input()))
a=list(map(int,input().split()))
q=int(eval(input()))

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

# Optimized solution
n=int(eval(input()))
a=list(map(int,input().split()))
q=int(eval(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=list(map(int,input().split()))
    b_num=num[b]
    num[c] += b_num
    num[b]=0
    summ +=(c-b)*b_num
    print(summ)