CodeEff/ECCO huggingface dataset:
# Original solution
import sys

def func1(lst, value):
    return [i for i, x in enumerate(lst) if x == value]

def main():
    input = sys.stdin.readline
    n = int(eval(input()))
    a = list(map(str, input().strip().split()))
    q = int(eval(input()))
    bc = [list(map(str, input().split())) for _ in range(q)]
    b, c = [list(i) for i in zip(*bc)]

    for i in range(q):
        aa = [n.replace(b[i],c[i]) for n in a]
        a = aa
        print((sum(list(map(int, aa)))))
        

if __name__ == "__main__":
    main()

# Optimized solution
import sys

def func1(lst, value):
    return [i for i, x in enumerate(lst) if x == value]

def main():
    input = sys.stdin.readline
    n = int(eval(input()))
    a = list(map(str, input().strip().split()))
    q = int(eval(input()))
    bc = [list(map(str, input().split())) for _ in range(q)]
    b, c = [list(i) for i in zip(*bc)]
    ii = 0

    for i in b:
        a = [n.replace(i,c[ii]) for n in a]
        print((sum(list(map(int, a)))))
        ii += 1
        

if __name__ == "__main__":
    main()
# Original solution
import sys
input = sys.stdin.readline

n = int(eval(input()))
a = list(map(str, input().strip().split()))
q = int(eval(input()))
bc = [list(map(str, input().split())) for _ in range(q)]
b, c = [list(i) for i in zip(*bc)]
i = 0

for _ in range(q):
    a = [n.replace(b[i],c[i]) for n in a]
    print((sum(list(map(int, a)))))
    i += 1

# Optimized solution
import sys
input = sys.stdin.readline
 
n = int(eval(input()))
a = list(map(int, input().strip().split()))
q = int(eval(input()))
bc = [list(map(int, input().split())) for _ in range(q)]
b, c = [list(i) for i in zip(*bc)]
i = 0
 
for _ in b:
    for aa in range(n):
        if(a[aa]==b[i]):
            a[aa]=c[i]
    i += 1
    print((sum(a)))
# Original solution
import collections
N = eval(input())
inp = list(map(int, input().split()))
dic = dict(collections.Counter(inp))
wa = 0
for i in range(len(dic)):
    wa += list(dic.keys())[i] * list(dic.values())[i]
se = set(dic.keys())
for i in range(int(eval(input()))):
    a, b = list(map(int, input().split()))
    if a in se:
        wa += dic[a] * (b-a)
        if b in se:
            pass
        else:
            dic[b] = 0
        se.add(b)
        dic[b] += dic[a]
        dic[a] = 0
    print(wa)

# Optimized solution
import collections
N = int(eval(input()))
inp = list(map(int,input().split()))
dic = dict(collections.Counter(inp))
wa = 0
for i, j in list(dic.items()):
	wa += i * j
se = set(dic.keys())
for i in range(int(eval(input()))):
	a, b = list(map(int,input().split()))
	if a in se:
		wa += dic[a] * (b-a)
		if b in se:
			pass
		else:
			dic[b] = 0
		se.add(b)
		dic[b] += dic[a]
		dic[a] = 0
	print(wa)
# Original solution
from collections import defaultdict
n = int(eval(input()))
a = list(map(str,input().split()))
q = int(eval(input()))
d = defaultdict(lambda: 0)
for nums in a:
    d[nums] += 1
for i in range (q):
    Sum = 0
    b,c = list(map(str,input().split()))
    d[c] += d[b]
    del d[b]
    for keys in d:
        cnt = d[keys]
        keys = int(keys)
        Sum += keys*cnt
    print(Sum)

# Optimized solution
from collections import defaultdict
n = int(eval(input()))
a = list(map(int,input().split()))
q = int(eval(input()))
d = defaultdict(lambda: 0)
Sum = 0
for nums in a:
    d[nums] += 1
for keys in d:
        cnt = d[keys]
        keys = int(keys)
        Sum += keys*cnt
for i in range (q):
    b,c = list(map(int,input().split()))
    d[c] += d[b]
    Sum += (c-b)*d[b]
    del d[b]
    print(Sum)
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))

m = {}
for i in a:
    if i in list(m.keys()):
        m[i] = m[i] + 1
    else:
        m[i] = 1

for i in range(q):
    b, c = list(map(int,input().split()))
    if b in list(m.keys()):
        if c in list(m.keys()):
            m[c] = m[c] + m[b]
        else:
            m[c] = m[b]
        m.pop(b)
    ans = 0
    for k in m:
        ans += m[k] * k
    print(ans)


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

m = {}
for i in a:
    if i in list(m.keys()):
        m[i] = m[i] + 1
    else:
        m[i] = 1

ans = 0
for k in m:
    ans += m[k] * k

for i in range(q):
    b, c = list(map(int,input().split()))
    if b in list(m.keys()):
        t = 0
        if c in list(m.keys()):
            t = m[c] * c
            m[c] = m[c] + m[b]
        else:
            m[c] = m[b]
        ans += m[c] * c - m[b] * b - t
        m.pop(b)
    print(ans)
# Original solution
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)]
goukeiti=sum(A)
atai=[0]*(10**5+1)
for i in A:
    atai[i-1]+=1
for i in BC:
    if atai[i[0]-1]!=0:
        kotae=atai[i[0]-1]*i[1]-atai[i[0]-1]*(i[0])
        atai[i[1]-1]+=atai[i[0]-1]
        atai[i[0]-1]=0
        goukeiti+=kotae
        print(goukeiti)
    else:
        print(goukeiti)

# Optimized solution
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)]
goukeiti=sum(A)
atai=[0]*(10**5+1)
for i in A:
    atai[i-1]+=1
for i in BC:
    kotae=atai[i[0]-1]*i[1]-atai[i[0]-1]*(i[0])
    atai[i[1]-1]+=atai[i[0]-1]
    atai[i[0]-1]=0
    goukeiti+=kotae
    print(goukeiti)
# Original solution
N = int(input())
A = list(map(int, input().split()))
total = sum(A)
from collections import defaultdict
d = defaultdict(lambda: 0)
for a in A:
    d[a] += 1
Q = int(input())
ans = []
for i in range(Q):
    x,y = map(int, input().split())
    total += y*d[x]-x*d[x]
    d[y] += d[x]
    d[x] = 0
    ans.append(total)
print(*ans, sep='\n')



# Optimized solution
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')
# Original solution
n = int(eval(input()))
A = list(map(int,input().split()))
q = int(eval(input()))
total = sum(A)

l = [[i, 0] for i in range(10**5+1)]
for _ in range(n):
    l[A[_]][1] += 1

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

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

l = [0 for i in range(10**5+1)]
for _ in range(n):
    l[A[_]] += 1

for i in range(q):
    b, c = list(map(int,input().split()))
    total += (c * l[b])
    total -= (b * l[b])
    print(total)
    l[c] += l[b]
    l[b] = 0
# Original solution
n = int(eval(input()))
A = list(map(int,input().split()))
q = int(eval(input()))

total = sum(A) # これを基に各queryに対応(加算、減算)する

l = [0 for i in range(10**5+1)] # 配列のインデックスを各valueとみなす
for _ in A: # 各インデックスに各keyを加算
    l[_] += 1

for i in range(q):
    b, c = list(map(int,input().split()))
    total += c * l[b] # totalの調整のみ
    total -= b * l[b] # 同上
    print(total)
    l[c] += l[b] # ここでリストの更新
    l[b] = 0 # 同上


# Optimized solution
def Replacing():
    import sys
    input = sys.stdin.readline

    n = int(eval(input()))
    A = list(map(int,input().split()))
    q = int(eval(input()))
    
    total = sum(A) # これを基に各queryに対応(加算、減算)する
    
    l = [0 for i in range(10**5+1)] # 配列のインデックスを各valueとみなす
    for _ in A: # 各インデックスに各keyを加算
        l[_] += 1
    
    for i in range(q):
        b, c = list(map(int,input().split()))
        total += c * l[b] # totalの調整のみ
        total -= b * l[b] # 同上
        print(total)
        l[c] += l[b] # ここでリストの更新
        l[b] = 0 # 同上

Replacing()
# Original solution
def Replacing():

    n = int(eval(input()))
    A = list(map(int,input().split()))
    q = int(eval(input()))
    
    total = sum(A) # これを基に各queryに対応(加算、減算)する
    
    l = [0 for i in range(10**5+1)] # 配列のインデックスを各valueとみなす
    for _ in A: # 各インデックスに各keyを加算
        l[_] += 1
    
    for i in range(q):
        b, c = list(map(int,input().split()))
        total += c * l[b] # totalの調整のみ
        total -= b * l[b] # 同上
        print(total)
        l[c] += l[b] # ここでリストの更新
        l[b] = 0 # 同上

Replacing()

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

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

total = sum(A) # これを基に各queryに対応(加算、減算)する

l = [0 for i in range(10**5+1)] # 配列のインデックスを各valueとみなす
for _ in A: # 各インデックスに各keyを加算
    l[_] += 1

for i in range(q):
    b, c = list(map(int,input().split()))
    total += c * l[b] # totalの調整のみ
    total -= b * l[b] # 同上
    print(total)
    l[c] += l[b] # ここでリストの更新
    l[b] = 0 # 同上
# Original solution
import sys
input = sys.stdin.readline

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

total = sum(A) # これを基に各queryに対応(加算、減算)する

l = [0 for i in range(10**5+1)] # 配列のインデックスを各valueとみなす
for _ in A: # 各インデックスに各keyを加算
    l[_] += 1

for i in range(q):
    b, c = list(map(int,input().split()))
    total += c * l[b] # totalの調整のみ
    total -= b * l[b] # 同上
    print(total)
    l[c] += l[b] # ここでリストの更新
    l[b] = 0 # 同上



# Optimized solution
def Replacing():
    import sys
    input = sys.stdin.readline

    n = int(eval(input()))
    A = list(map(int,input().split()))
    q = int(eval(input()))
    
    total = sum(A) # これを基に各queryに対応(加算、減算)する
    
    l = [0 for i in range(10**5+1)] # 配列のインデックスを各valueとみなす
    for _ in A: # 各インデックスに各keyを加算
        l[_] += 1
    
    for i in range(q):
        b, c = list(map(int,input().split()))
        total += c * l[b] # totalの調整のみ
        total -= b * l[b] # 同上
        print(total)
        l[c] += l[b] # ここでリストの更新
        l[b] = 0 # 同上

Replacing()
# Original solution
from collections import Counter

n = int(eval(input()))
a = Counter(list(map(int, input().split())))
q = int(eval(input()))
for i in range(q):
  b, c = list(map(int, input().split()))
  a[c] += a[b]
  del a[b]
  s = sum(x * y for x, y in list(a.items()))
  print(s)


# Optimized solution
from collections import Counter

n = int(eval(input()))
a = Counter(list(map(int, input().split())))
q = int(eval(input()))
s = sum(x * y for x, y in list(a.items()))
for _ in range(q):
  b, c = list(map(int, input().split()))
  s = s + (c - b) * a[b]
  a[c] += a[b]
  a[b] = 0
  print(s)
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))

q = int(eval(input()))

from collections import Counter

ca = Counter(a)

def sum_dict(dic):
    return sum([num*cnt for num,cnt in list(dic.items())])

for _ in range(q):
    b,c = list(map(int, input().split()))
    
    if b in ca:
        cnt = ca.pop(b)
        ca[c] += cnt
    
    print((sum_dict(ca)))

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

q = int(eval(input()))

from collections import Counter

ca = Counter(a)

ans=sum(a)
for _ in range(q):
    b,c = list(map(int, input().split()))
    
    if b in ca:
        cnt = ca.pop(b)
        ca[c] += cnt

        ans -= b * cnt
        ans += c * cnt
    
    print(ans)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))

dic = {}
for num in A:
    if num in dic:
        dic[num] += 1
    else:
        dic[num] = 1

for _ in range(Q):
    total = 0
    B, C = list(map(int, input().split()))
    if B in dic and C in dic:
        dic[C] += dic[B]
        del dic[B]
    elif B in dic and C not in dic:
        dic[C] = dic[B]
        del dic[B]
    for mykey, myvalue in list(dic.items()):
        total += mykey * myvalue
    print(total)


# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))

dic = {}
for num in A:
    if num in dic:
        dic[num] += 1
    else:
        dic[num] = 1

prev_sum = 0

for key, value in list(dic.items()):
    prev_sum += key * value

for _ in range(Q):
    B, C = list(map(int, input().split()))
    if B in dic and C in dic:
        dic[C] += dic[B]
        dif = (C - B) * dic[B]
        prev_sum += dif
        del dic[B]
    elif B in dic and C not in dic:
        dic[C] = dic[B]
        dif = (C - B) * dic[B]
        prev_sum += dif
        del dic[B]
    print(prev_sum)
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))
mp = {}
for i in range(n):
    if(a[i] in list(mp.keys())):
        mp[a[i]] += 1
    else:
        mp[a[i]] = 1
q = int(eval(input()))
for i in range(q):
    b, c = list(map(int, input().split()))
    if(b not in list(mp.keys())):
        mp[b] = 0
    if(c in list(mp.keys())):
        mp[c] += mp[b]
    else:
        mp[c] = mp[b]
    mp[b] = ans = 0
    for k in list(mp.keys()):
        ans += k * mp[k]
    print(ans)

# Optimized solution
n = int(eval(input()))
a = list(map(int, input().split()))
mp, ans = {}, 0
for i in range(n):
    if(a[i] in list(mp.keys())):
        mp[a[i]] += 1
    else:
        mp[a[i]] = 1
for k in list(mp.keys()):
    ans += k * mp[k]
q = int(eval(input()))
for i in range(q):
    b, c = list(map(int, input().split()))
    if(b not in list(mp.keys())):
        mp[b] = 0
        print(ans)
        continue
    if(c in list(mp.keys())):
        mp[c] += mp[b]
    else:
        mp[c] = mp[b]
    ans += (c-b) * mp[b]
    mp[b] = 0
    print(ans)
# Original solution
N=int(eval(input()))
number=list(map(int,input().split()))
sum_number=sum(number)
Q=int(eval(input()))
number_count=[0]*100001
for i in range(100001):
    number_count[i]=number.count(i)

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


# Optimized solution
from collections import Counter
N=int(eval(input()))
number=list(map(int,input().split()))
sum_number=sum(number)
Q=int(eval(input()))
number_count=Counter(number)


for _ in range(Q):
    B,C=list(map(int,input().split()))
    sum_number += (C-B)*number_count[B]
    number_count[C] += number_count[B]
    number_count[B] = 0
    print(sum_number)
# Original solution
n = int(eval(input()))
l_a = list(map(int, input().split()))
many = []
for i in range(1, 100001) :
    many.append(l_a.count(i))
bef = sum(l_a)
q = int(eval(input()))
ans = []
for _ in range(q) :
    b, c = list(map(int, input().split()))
    bef += (c - b)*many[b - 1]
    ans.append(bef)
    many[c - 1] += many[b - 1]
    many[b - 1] = 0

for value in ans :
    print(value)


# Optimized solution
import collections

n = int(eval(input()))
l_a = list(map(int, input().split()))
col = collections.Counter(l_a)
bef = sum(l_a)
q = int(eval(input()))
ans = []
for _ in range(q) :
    b, c = list(map(int, input().split()))
    bef += (c - b)*col[b]
    ans.append(bef)
    col[c] += col[b]
    col[b] = 0

for value in ans :
    print(value)
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
b = []
c = []
for i in range(q):
    b_i, c_i = list(map(int, input().split()))
    b.append(b_i)
    c.append(c_i)

res = {}
for a_i in a:
    if not a_i in res:
        res[a_i] = 0
    res[a_i] += 1

for b_i, c_i in zip(b, c):
    num = res[b_i] if b_i in res else 0
    res[b_i] = 0
    if not c_i in res:
        res[c_i] = 0
    res[c_i] += num

    ans = 0
    for key, val in list(res.items()):
        ans += key * val
    print(ans)


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


res = {}
for a_i in a:
    if not a_i in res:
        res[a_i] = 0
    res[a_i] += 1

ans = 0
for key, val in list(res.items()):
    ans += key * val

for b_i, c_i in zip(b, c):
    num = res[b_i] if b_i in res else 0
    res[b_i] = 0
    if not c_i in res:
        res[c_i] = 0
    res[c_i] += num

    ans += (c_i - b_i) * num

    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):
    b1,c1=list(map(int, input().split()))
    B.append(b1)
    C.append(c1)

# print(A)
for i in range(q):
    A=[ C[i] if B[i]==a else a for a in A ]
    print((sum(A)))


# Optimized solution
n=int(eval(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(eval(input()))
for i in range(q):
    diff=0
    b,c =list(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)
# Original solution
N=int(eval(input()))
A=list(map(int,input().split()))
Q=int(eval(input()))
L=[0]*(10**5)
for i in A:L[i-1]+=i
for i in range(Q):
    B,C=list(map(int,input().split()))
    L[C-1]+=(L[B-1]//B)*C;L[B-1]=0
    print((sum(L)))

# Optimized solution
N=int(eval(input()))
A=list(map(int,input().split()))
Q=int(eval(input()))
L=[0]*(10**5)
S=sum(A)
for i in A:L[i-1]+=1
for i in range(Q):
    B,C=list(map(int,input().split()));B-=1;C-=1
    S+=((C+1)-(B+1))*L[B]
    L[C]+=L[B];L[B]=0
    print(S)
# Original solution
n = int(eval(input()))
a = list(map(str, input().split()))
q = int(eval(input()))

for i in range(q):
  b, c = list(map(str, input().split()))
  num = ",".join(a)
  num = num.replace(b, c)
  a = num.split(",")
  ans = list(map(int, a))
  print((sum(ans)))

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

def list1(n):
  if n == b:
    return c
  else:
    return n
  
for i in range(q):
  b, c = list(map(int, input().split()))
  a = list(map(list1, a))
  print((sum(a)))
# 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)]


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

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

for bc in BC:
    b, c = bc
    array[c] += array[b]
    array[b] = 0

    total = 0
    for i in range(10 ** 5 + 1):
        total += i * array[i]
    print(total)


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


array = [0]*(10**5+1)
total = sum(A)

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

for bc in BC:
    b, c = bc
    total += c*array[b]-b*array[b]
    array[c] += array[b]
    array[b] = 0
    print(total)
# Original solution
from collections import Counter
from sys import stdin
N = int(stdin.readline().rstrip())
A = Counter([int(_) for _ in stdin.readline().rstrip().split()])
Q = int(stdin.readline().rstrip())
BC = [list(map(int, stdin.readline().rstrip().split())) for _ in range(Q)]
ans = []
for bc in BC:
    if bc[1] not in A:
        A[bc[1]] = A[bc[0]]
    else:
        A[bc[1]] += A[bc[0]]
    A.pop(bc[0], None)
    ans.append(sum(k*v for k, v in list(A.items())))
for a in ans:
    print(a)

# Optimized solution
from collections import Counter
from sys import stdin
N = int(stdin.readline().rstrip())
A = [int(_) for _ in stdin.readline().rstrip().split()]
CA = Counter(A)
Q = int(stdin.readline().rstrip())
ans = sum(A)
for i in range(Q):
    b, c = [int(_) for _ in stdin.readline().rstrip().split()]
    ans += (c-b)*CA[b]
    CA[c] += CA[b]
    CA[b] = 0
    print(ans)
# Original solution
from collections import Counter
n = int(eval(input()))
A = list(map(int, input().split()))
q = int(eval(input()))
dict_A = Counter(A)
sum_list = sum(A)
for i in range(q):
    b, c = list(map(int, input().split()))
    if b not in dict_A:
        print(sum_list)
    else:
        dict_A[c] += dict_A[b]
        dict_A[b] = 0
        sum_list = 0
        for key, value in list(dict_A.items()):
            sum_list += key * value
        print(sum_list)

# Optimized solution
from collections import Counter
n = int(eval(input()))
A = list(map(int, input().split()))
q = int(eval(input()))
dict_A = Counter(A)
sum_list = sum(A)
for i in range(q):
    b, c = list(map(int, input().split()))
    if b not in dict_A:
        print(sum_list)
    else:
        b_num = dict_A[b]
        dict_A[c] += dict_A[b]
        dict_A[b] = 0
        sum_list = sum_list + (c - b) * b_num
        print(sum_list)
# Original solution
n = int(eval(input()))
a = sorted(list(map(int, input().split())))
q = int(eval(input()))
l1 = [0]*(10**5+10)
l2 = [0]*(10**5+10)
index = 0
l1[0] = a[0]
l2[0] = 1
for i in a[1:]:
    if i == l1[index]:
        l2[index] += 1
    else:
        index += 1
        l1[index] = i
        l2[index] = 1
s = sum(a)
for _ in range(q):
    b, c = list(map(int, input().split()))

    i = 0
    while i < index + 2:
        if (b == l1[i]):
            break
        i += 1
    if i > index:
        print(s)
        continue

    j = 0
    while j < index + 2:
        if(c == l1[j]):
            break
        j += 1
    if j > index:
        index = j
        l1[index] = c
    s += (c - b) * l2[i]
    l2[j] += l2[i]
    l2[i] = 0
    print(s)


# Optimized solution
n = int(eval(input()))
a = sorted(list(map(int, input().split())))
q = int(eval(input()))
l = [0]*(10**5+10)
for i in a:
    l[i] += 1
s = sum(a)
for _ in range(q):
    b, c = list(map(int, input().split()))
    s += (c - b) * l[b]
    l[c] += l[b]
    l[b] = 0
    print(s)
# Original solution
import sys
input = sys.stdin.readline

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

dict = {}
for i in range(1, 10**5+1):
    dict[i] = 0
for i in a:
    dict[i] += 1

for i in range(q):
    b, c = list(map(int, input().split()))
    dict[c] += dict[b]
    dict[b] = 0
    ans = 0
    for i, j in list(dict.items()):
        ans += i*j
    print(ans)

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

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

dict = {}

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

s = sum(a)

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

n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
bc = [list(map(int, input().split())) for _ in range(q)]
suma = sum(a)
counter = Counter(a)
for i in range(q):
    suma -= bc[i][0] * counter[bc[i][0]]
    suma += bc[i][1] * counter[bc[i][0]]
    counter[bc[i][1]] += counter[bc[i][0]]
    counter[bc[i][0]] = 0
    print(suma)


# Optimized solution
from collections import Counter

n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
bc = [list(map(int, input().split())) for _ in range(q)]
suma = sum(a)
counter = Counter(a)
for b, c in bc:
    suma -= b * counter[b]
    suma += c * counter[b]
    counter[c] += counter[b]
    counter[b] = 0
    print(suma)
# Original solution
def solve():
    N = int(eval(input()))
    s = 0
    n = [0 for _ in range(100000+1)]
    for a in map(int, input().split()):
        s += a
        n[a] += 1
    q = int(eval(input()))
    for _ in range(q):
        b, c = list(map(int, input().split()))
        s += (c - b) * n[b]
        print(s)
        n[c] += n[b]
        n[b] = 0


if __name__ == '__main__':
    solve()


# Optimized solution
def solve():
    N = int(eval(input()))
    s = 0
    n = [0] * (10**5+1)
    for a in map(int, input().split()):
        s += a
        n[a] += 1
    q = int(eval(input()))
    for _ in range(q):
        b, c = list(map(int, input().split()))
        s += (c - b) * n[b]
        print(s)
        n[c] += n[b]
        n[b] = 0


if __name__ == '__main__':
    solve()