CodeEff/ECCO huggingface dataset:
# Original solution
n = int(eval(input()))
A = list(map(int, input().split())) 
q = int(eval(input()))

arr = [0] * (10**5+1)
# arr = [i for i in range(1, 10**5+1)]

for i in A:
    arr[i] += i

for _ in range(q):
    b, c = list(map(int, input().split()))
    arr[c] += (arr[b]//b*c)
    arr[b] = 0

    print((sum(arr)))


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

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

n=int(eval(input()))
a=list(map(int,input().split()))
q=int(eval(input()))
b=[[int(i) for i in input().split()] for l in range(q)]

c=collections.Counter(a)

for x in b:
    c[x[0]],c[x[1]]=0,c[x[0]]+c[x[1]]
    print((sum([j*k for j,k in list(c.items())])))

# Optimized solution
import collections

n=int(eval(input()))
a=list(map(int,input().split()))
q=int(eval(input()))
b=[[int(i) for i in input().split()] for l in range(q)]

c=collections.Counter(a)
s=sum([j*k for j,k in list(c.items())])
for x in b:
    s=s-x[0]*c[x[0]]+x[1]*c[x[0]]
    c[x[0]],c[x[1]]=0,c[x[0]]+c[x[1]]
    print(s)
# Original solution
from collections import Counter
N=int(eval(input()))
A=list(map(int, input().split()))
q=int(eval(input()))
Q=[list(map(int, input().split())) for _ in range(q)]

L=[0]*(10**5+1)
B=dict(Counter(A))
for k in list(B.keys()):
    L[k]=B[k]
# print(L)

C=set(B)
sum = 0
for k in list(B.keys()):
    sum += k*B[k]
for i in range(q):
    x=Q[i][0]
    y=Q[i][1]
    if L[x]==0:
        print(sum)
        continue
    L[y]+=L[x]
    L[x]=0
    C.add(y)
    C.remove(x)
    sum=0
    for k in C:
        sum+=k*L[k]
    print(sum)

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

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

for i in range(q):
    S+=L[Q[i][0]]*(Q[i][1]-Q[i][0])
    L[Q[i][1]]+=L[Q[i][0]]
    L[Q[i][0]]=0
    # print(L)
    print(S)
# Original solution
N=int(eval(input()))
A=list(map(int,input().split()))
Q=int(eval(input()))
BC=[]
for i in range(Q):
    b,c=(list(map(int,input().split())))
    BC.append([b,c])

#最初に全探索で位置をインデックスに登録する
DP=[[] for _ in range(10**5+1)]
for i in range(len(A)):
    DP[A[i]].append(i)#
ans=sum(A)
for i in BC:
    target=i[0]
    nex=i[1]
    for j in DP[target]:
        A[j]=nex
        DP[nex].append(j)
        ans-=target
        ans+=nex
    print(ans)
    DP[target]=[]

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

#最初に全探索で位置をインデックスに登録する
DP=[0]*(10**5+1)
for i in A:
    DP[i]+=1#
ans=sum(A)
for i in BC:
    target=i[0]
    nex=i[1]
    num=DP[target]
    DP[target]=0
    DP[nex]=DP[nex]+num
    ans=ans+num*nex-num*target
    print(ans)
# Original solution
n=int(eval(input()))

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

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

for i in range(n):
  z[a[i]]+=1

q=int(eval(input()))

y=[]


for i in range(q):
  b,c=list(map(int,input().split()))
  z[c]+=z[b]
  z[b]=0
  #print(z)
  sam=0
  for j in range(len(z)):
    sam+=z[j]*j
  y.append(sam)

for i in range(len(y)):
  print((y[i]))

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

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

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

for i in range(n):
  z[a[i]]+=1

q=int(eval(input()))

y=[]

sam=sum(a)

for i in range(q):
  b,c=list(map(int,input().split()))
  z[c]+=z[b]
  sam-=z[b]*b
  sam+=z[b]*c
  z[b]=0
  #print(z)
  
  y.append(sam)

for i in range(len(y)):
  print((y[i]))
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))
s = sum(a)
q = int(eval(input()))
b = []
c = []
# diff = []
for i in range(q):
    _ = list(map(int, input().split()))
    c.append(_[1])
    b.append(_[0])

for i in range(q):
    for j in range(n):
        if b[i] == a[j]:
            a[j] = c[i]
    print((sum(a)))


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

s = sum(a)

# 辞書型で度数を持つ
hist = {}
for i in range(n):
    if a[i] in hist:
        hist[a[i]] += 1
    else:
        hist[a[i]] = 1
# print(hist)

for i in range(q):
    diff = c[i] - b[i]
    if b[i] in hist:
        s += hist[b[i]]*diff
        print(s)
        if c[i] in hist:
            hist[c[i]] += hist[b[i]]
            del hist[b[i]]
        else:
            hist[c[i]] = hist[b[i]]
            del hist[b[i]]
    else:
        print(s)
    # print(hist)
# Original solution
import collections
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
B = []
C = []
for i in range(Q):
    a, b = list(map(int, input().split()))
    B.append(a)
    C.append(b)
d = collections.Counter(A)
ans = 0
c = list(d.keys())
cs = list(d.values())
for i in range(Q):
    l=[j for j,x in enumerate(c) if x == B[i]]

    for k in l:
        c[k]=C[i]
    for m in range(len(c)):
        ans += c[m]*cs[m]
    print(ans)
    ans = 0


# Optimized solution
n = int(eval(input()))
a = [int(i) for i in input().split()]
q = int(eval(input()))
num = [0] * 100010
b = [0] * q
c = [0] * q
for i in range(q):
    b[i], c[i] = list(map(int, input().split()))

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

s = sum(a)

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

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

s = sum(a)

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


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


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

for i in A:
    LIST[i]+=1

s = sum(A)

for i in range(Q):
    s += LIST[b[i]]*(c[i]-b[i])
    LIST[c[i]]+=LIST[b[i]]
    LIST[b[i]] = 0
    print(s)
# Original solution
N = int(eval(input()))
a=list(map(int, input().split()))
Q = int(eval(input()))
num_a =[0]*100001
num_b = []
sum_a = sum(a)
b=[]
c=[]
for i in range(Q):
  b1,c1=list(map(int, input().split()))
  b.append(b1)
  c.append(c1)
  if b[i] not in a:
    continue
  elif num_a[b[i]]>0:
    continue
  else:
    num_a[b[i]] = a.count(b[i])
for i in range(Q):
  if num_a[b[i]]==0:
    num_b.append(0)
    sum_a = sum_a - b[i]*num_b[i] +c[i]*num_b[i]
  else:
    num_b.append(num_a[b[i]])
    num_a[c[i]] += num_a[b[i]]
    num_a[b[i]] = 0
    sum_a = sum_a - b[i]*num_b[i] +c[i]*num_b[i]
  print(sum_a)


# Optimized solution
N = int(eval(input()))
a=list(map(int, input().split()))
Q = int(eval(input()))
num_a =[0]*100001
num_b = []
sum_a = sum(a)
b=[]
c=[]
for i in a:
  num_a[i]+= 1
for i in range(Q):
  b1,c1=list(map(int, input().split()))
  b.append(b1)
  c.append(c1)
for i in range(Q):
  if num_a[b[i]]==0:
    num_b.append(0)
    sum_a = sum_a - b[i]*num_b[i] +c[i]*num_b[i]
  else:
    num_b.append(num_a[b[i]])
    num_a[c[i]] += num_a[b[i]]
    num_a[b[i]] = 0
    sum_a = sum_a - b[i]*num_b[i] +c[i]*num_b[i]
  print(sum_a)
# Original solution
n=int(eval(input()))
a=list(map(int,input().split()))
q=int(eval(input()))
ans=[0]*(10**5+1)
for i in a:
    ans[i]+=1

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

# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
L = [0] * (100001)
s = sum(A)
for a in A:
    L[a] += 1
Q = int(eval(input()))
for _ in range(Q):
    B, C = list(map(int, input().split()))
    L[C] += L[B]
    s += L[B] * (C-B)
    L[B] = 0
    print(s)
# Original solution
N = int(eval(input()))
A = sorted(map(int, input().split()))
ans = sum(A)
nums = [A.count(num) for num in range(10 ** 5 + 1)]

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

# Optimized solution
import collections

N = int(eval(input()))
A = sorted(map(int, input().split()))

cnt = collections.defaultdict(int)
for a in A:
    cnt[a] += 1
ans = sum(A)

Q = int(eval(input()))
for _ in range(Q):
    B, C = list(map(int, input().split()))
    ans += (C - B) * cnt[B]
    cnt[C] += cnt[B]
    cnt[B] = 0
    print(ans)
# Original solution
n=int(eval(input()))
a=[int(i) for i in input().split()]
q=int(eval(input()))
bc=[]
for i in range(q):
    bc.append([int(i) for i in input().split()])

import collections
collect = collections.Counter(a)

for i in bc:
    if i[1] in collect:
        collect[i[1]] += collect.pop(i[0],0)
    else:
        if i[0] in collect:
            collect[i[1]] = collect.pop(i[0],0)
    ans=0
    for k,v in list(collect.items()):
        ans += (k * v)
    print(ans)


# Optimized solution
#sum を計算しておく
n=int(eval(input()))
a=[int(i) for i in input().split()]
a.sort()
q=int(eval(input()))
bc=[]
for i in range(q):
    bc.append([int(i) for i in input().split()])

import collections
collect = collections.Counter(a)

ans=0
for k,v in list(collect.items()):
    ans += (k * v)
for i in bc:
    if i[0] in collect:
        if i[1] in collect:
            ans += ((i[1]-i[0]) * collect[i[0]])
            collect[i[1]] += collect.pop(i[0],0)
        else:
            ans += ((i[1]-i[0]) * collect[i[0]])
            collect[i[1]] = collect.pop(i[0],0)
    print(ans)
# Original solution
from collections import Counter
N = int(eval(input()))
A = list(map(int,input().split()))
Q = int(eval(input()))
num = Counter(A)
for i in range(Q):
    b,c = list(map(int,input().split()))
    num[c] += num[b]
    num[b] = 0
    sums = 0
    for j in num:
        sums += num[j]*j
    print(sums)

# Optimized solution
from collections import Counter
N = int(eval(input()))
A = list(map(int,input().split()))
Q = int(eval(input()))
ans = sum(A)
num = Counter(A)
for i in range(Q):
    b,c = list(map(int,input().split()))

    ans += (c-b)*num[b]
    num[c] += num[b] 
    num[b] = 0
    
    print(ans)
# Original solution
import collections

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)

# print(N,A,Q,B,C)


a = []

a = dict(collections.Counter(A))

# print(a)


# d1 = {'k1': 1, 'k2': 2}
# d2 = {'k1': 100, 'k3': 3, 'k4': 4}

# d1.update(d2)
# print(d1)
# # {'k1': 100, 'k2': 2, 'k3': 3, 'k4': 4}

# これを用いれば良い。

for i in range(Q):
    # 変更後
    after = {B[i]: 0, C[i]: a.setdefault(B[i], 0) + a.setdefault(C[i], 0)}
    # print(a, after)
    a.update(after)

    s = 0
    for k in a:
        s += k * a[k]
    print(s)

# print(a)



# Optimized solution
import collections

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)

# print(N,A,Q,B,C)


a = []

a = dict(collections.Counter(A))

s = 0
for k in a:
    s += k * a[k]
# print(s)

# sに対する増減だけ管理する

for i in range(Q):
    # 変更後
    after = {B[i]: 0, C[i]: a.setdefault(B[i], 0) + a.setdefault(C[i], 0)}
    # print(a, after)

    s = s + (C[i] - B[i] ) * a[B[i]]

    print(s)

    a.update(after)
# Original solution
from collections import Counter
n = int(eval(input()))
cnt = Counter(list(map(int, input().split())))
q = int(eval(input()))
Asum = sum(k*v for k, v in list(cnt.items()))
for _ in range(q):
    b, c = list(map(int, input().split()))
    cnt[c] += cnt[b]
    Asum += c*cnt[b]-b*cnt[b]
    cnt[b] = 0
    print(Asum)


# Optimized solution
from collections import Counter
n = int(eval(input()))
cnt = Counter(list(map(int, input().split())))
Asum = sum(k*v for k, v in list(cnt.items()))
q = int(eval(input()))
for _ in range(q):
    b, c = list(map(int, input().split()))
    Asum += (c-b)*cnt[b]
    print(Asum)
    cnt[b], cnt[c] = 0, cnt[b]+cnt[c]
# Original solution
n = int(eval(input()))
a = list(map(int,input().split()))
q = int(eval(input()))
ans = sum(a)
many =[0]*(10**5)
bc = [0]*q
for i in range(10**5) :
    many[i] = a.count(i+1)

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

for i in range(q) : 
    ans += (bc[i][1] - bc[i][0]) * many[bc[i][0] - 1]
    print(ans)
    many[bc[i][1]-1] += many[bc[i][0]-1]
    many[bc[i][0]-1] =0



# Optimized solution
n = int(eval(input()))
A = list(map(int,input().split()))
q = int(eval(input()))
ans = sum(A)
many =[0]*(10**5 + 1)
for a in A :
    many[a] += 1

for i in range(q) : 
    B,C = (int(a) for a in input().split())
    many[C] += many[B]
    ans += many[B] * (C-B)
    many[B] = 0
    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)]

anum=[]
asum=sum(a) #当初の数列Aの要素の和

for i in a:
  acnt=a.count(i)
  anum.append([i,acnt])

d=dict(anum) #当初の数列Aの各整数の数のdictionary

for i in range(q):
  if bc[i][0] not in list(d.keys()):
    print(asum)
  if bc[i][0] in list(d.keys()):
    x=(bc[i][1]-bc[i][0])*d[bc[i][0]]
    print((asum+x))
    asum+=x
    if bc[i][1] in list(d.keys()):
      d[bc[i][1]]=d[bc[i][1]]+d[bc[i][0]]
      d[bc[i][0]]=0
    else:
      d[bc[i][1]]=d[bc[i][0]]
      d[bc[i][0]]=0

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

asum=sum(a) #当初の数列Aの要素の和
d={}

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

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

from collections import Counter

def resolve():
  N = int(eval(input()))
  countA = Counter([int(x) for x in input().split(" ")])
  Q = int(eval(input()))

  sum_A = 0
  for a in list(countA.keys()):
    sum_A += a*countA[a]

  for _ in range(Q):
    B, C = list(map(int, input().split(" ")))
    if B in list(countA.keys()):
      countA[C] += countA.pop(B)
      sum_A = 0
      for a in list(countA.keys()):
        sum_A += a*countA[a]
      print(sum_A)
    else:
      print(sum_A)


if __name__ == "__main__":
  resolve()

# Optimized solution
from collections import Counter

def resolve():
  N = int(eval(input()))
  countA = Counter([int(x) for x in input().split(" ")])
  Q = int(eval(input()))

  sum_A = 0
  for a in list(countA.keys()):
    sum_A += a*countA[a]

  for _ in range(Q):
    B, C = list(map(int, input().split(" ")))
    if B in list(countA.keys()):
      target_value = countA.pop(B)
      sum_A += (C - B) * target_value
      countA[C] += target_value
      print(sum_A)
    else:
      print(sum_A)


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

s = {}

for v in a:
  if v in s:
    s[v] += v
  else:
    s[v] = v

for t in r:
  if t[0] in s:
    n = s[t[0]] // t[0] * t[1]
    if t[1] in s:
      s[t[1]] += n
    else:
      s[t[1]] = n
    del s[t[0]]
  print((sum(s.values())))


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

s = {}

for v in a:
  if v in s:
    s[v] += v
  else:
    s[v] = v

p = sum(s.values())

for t in r:
  if t[0] in s:
    n = s[t[0]] // t[0] * t[1]
    p = p - s[t[0]] + n
    if t[1] in s:
      s[t[1]] += n
    else:
      s[t[1]] = n
    del s[t[0]]
  print(p)
# Original solution
n=int(eval(input()))
a=list(map(int,input().split()))
q=int(eval(input()))
num=[[a.count(i+1)] for i in range(10**5)]
ans=sum(a)
for i in range(q):
    b,c=list(map(int,input().split()))
    ans=ans+(c-b)*num[b-1][0]
    num[c-1][0]+=num[b-1][0]
    num[b-1][0]=0
    print(ans)
    

# Optimized solution
n=int(eval(input()))
a=list(map(int,input().split()))
q=int(eval(input()))
num=[0]*(10**5+1)
ans=sum(a)
for i in a:
    num[i]+=1
for i in range(q):
    b,c=list(map(int,input().split()))
    ans=ans+(c-b)*num[b]
    num[c]+=num[b]
    num[b]=0
    print(ans)
# Original solution
import collections
N = int(eval(input()))
A = list(map(int, input().split()))
indices = collections.defaultdict(list)
for i, a in enumerate(A):
  indices[a].append(i)
Q = int(eval(input()))
for _ in range(Q):
  B, C = list(map(int, input().split()))
  for i in indices[B]:
    indices[C].append(i)
    A[i] = C
  print((sum(A)))

# Optimized solution
import collections
N = int(eval(input()))
A = list(map(int, input().split()))
count = collections.defaultdict(int)
currSum = 0
for a in A:
  count[a] += 1
  currSum += a
Q = int(eval(input()))
for _ in range(Q):
  B, C = list(map(int, input().split()))
  currSum += (count[B] * (C - B))
  count[C] += count[B]
  count[B] = 0
  print(currSum)
# Original solution
def main():
    N = int(eval(input()))
    A = list(map(int,input().split()))
    Q = int(eval(input()))
    B = [list(map(int,input().split())) for _ in range(Q)]
    dict = {}

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

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

    for i in range(Q):
        dict[B[i][1]] += dict[B[i][0]]
        dict[B[i][0]] = 0
        #print(dict)
        sum = 0
        for i in range(1,10**5+1):
        #for i in range(1,6):
            if dict[i] != 0:
                sum += dict[i]*i
        print(sum)

main()


# Optimized solution
def main():
    N = int(eval(input()))
    A = list(map(int,input().split()))
    Q = int(eval(input()))
    B = [list(map(int,input().split())) for _ in range(Q)]
    dict = {}

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

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

    sum = 0
    for i in range(1,10**5+1):
        #for i in range(1,6):
        if dict[i] != 0:
            sum += dict[i]*i

    for i in range(Q):
        sum = sum +(dict[B[i][0]]*B[i][1] - dict[B[i][0]]*B[i][0])

        dict[B[i][1]] += dict[B[i][0]]
        dict[B[i][0]] = 0
        #print(dict)
        print(sum)

main()
# 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)]

x = Counter(a)
for b, c in bc:
    x[c] += x[b]
    x[b] = 0

    ans = 0
    for key, value in x.most_common():
        ans += key * value
    print(ans)


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

x = Counter(a)
ans = sum(a)
for b, c in bc:
    ans += (c - b) * x[b]
    print(ans)
    x[c] += x[b]
    x[b] = 0
# Original solution
n = int(eval(input()))
a = [int(x) for x in input().split()]
q = int(eval(input()))
bc=[]
for i in range(q):
    bc.append([int(x) for x in input().split()])

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

for i in range(max(a)+1):
	a_cnt[i]=a.count(i)

a_goukei=sum(a)

for gyou in range(q):
	a_goukei+=(bc[gyou][1]-bc[gyou][0])*a_cnt[bc[gyou][0]]
	print(a_goukei)
	a_cnt[bc[gyou][1]]+=a_cnt[bc[gyou][0]]
	a_cnt[bc[gyou][0]]=0

# Optimized solution
n = int(eval(input()))
a = [int(x) for x in input().split()]
q = int(eval(input()))
bc=[]
for i in range(q):
    bc.append([int(x) for x in input().split()])

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

for i in range(len(a)):
	a_cnt[a[i]]+=1

a_goukei=sum(a)

for gyou in range(q):
	a_goukei+=(bc[gyou][1]-bc[gyou][0])*a_cnt[bc[gyou][0]]
	print(a_goukei)
	a_cnt[bc[gyou][1]]+=a_cnt[bc[gyou][0]]
	a_cnt[bc[gyou][0]]=0
# Original solution
import sys
readline = sys.stdin.buffer.readline
read = sys.stdin.read
sys.setrecursionlimit(10 ** 8)

 
def main():
    N = int(readline())
    A = [0] *(10**5+1)
    total = 0
    for a in map(int, readline().split()):
        total += a
        A[a] += 1
 
 
    Q = int(readline())
    for _ in range(Q):
        b, c = list(map(int, readline().split()))
        total += A[b]*(c-b)
        print(total)
        A[c] += A[b]
        A[b] = 0
 
 
if __name__ == '__main__':
    main()

# Optimized solution
import sys
readline = sys.stdin.buffer.readline
read = sys.stdin.read
 
def main():
    N = int(readline())
    A = [0] *(10**5+1)
    total = 0
    for a in map(int, readline().split()):
        total += a
        A[a] += 1
 
 
    Q = int(readline())
    for _ in range(Q):
        b, c = list(map(int, readline().split()))
        total += A[b]*(c-b)
        print(total)
        A[c] += A[b]
        A[b] = 0
 
 
if __name__ == '__main__':
    main()
# Original solution
# collect input N integers
# replace every element whose value is Bi with Ci
# get input x and y and compute them to display output 
from collections import Counter
n=eval(input())
a=list(map(int,input().split()))
b=Counter(a)
n=int(eval(input()))
ans=sum(a)
for i in range(n):
    x,y=list(map(int,input().split()))
    ans+=b[x]*(y-x)
    print(ans)
    b[y]+=b[x]
    b[x]=0

# Optimized solution
n = int(input())
a = [int(x) for x in input().split()]
q = int(input())
op = []
for _ in range(q):
    v = input().split()
    op.append([int(x) for x in v])

d = {}
total = 0
for v in a:
    d[v] = d.get(v, 0) + 1
    total += v

for src, dest in op:
    c = d.pop(src, 0)
    d[dest] = d.get(dest, 0) + c
    total += c * (dest - src)
    print(total)
# 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)

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

for i in range(Q):
    b = B[i]
    c = C[i]
    if b in A_new:
        n = A_new[b]
        A_new[b] = 0
        if c in A_new:
            A_new[c] += n
        else:
            A_new[c] = n
    key_list = list(A_new.keys())
    res = 0
    for k in key_list:
        res += A_new[k] * k
    print(res)


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

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

sum = 0
for i in range(0, 1):
    b = B[i]
    c = C[i]
    if b in A_new:
        n = A_new[b]
        A_new[b] = 0
        if c in A_new:
            A_new[c] += n
        else:
            A_new[c] = n
    key_list = list(A_new.keys())
    res = 0
    for k in key_list:
        res += A_new[k] * k
    print(res)
    sum = res


for i in range(1, Q):
    b = B[i]
    c = C[i]
    dif = 0
    if b in A_new:
        n = A_new[b]
        A_new[b] = 0
        if c in A_new:
            A_new[c] += n
        else:
            A_new[c] = n
        dif = c * n - b * n
    sum += dif
    print(sum)
# Original solution
from collections import defaultdict

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

dct = defaultdict(int)
for _ in range(N):
  dct[nums.pop()] += 1

for _ in range(Q):
  B, C = list(map(int,input().split()))
  if B in dct:
    if C in dct:
      dct[C] += dct.pop(B)
    else:
      dct[C] = dct.pop(B)

  sum = 0
  for k, v in list(dct.items()):
    sum += k * v
  
  print(sum)


# Optimized solution
from collections import defaultdict

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

res = 0

dct = defaultdict(int)
for _ in range(N):
  n = nums.pop()
  dct[n] += 1
  res += n

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