CodeEff/ECCO huggingface dataset:
# Original solution
#!/usr/bin/env python3
import sys
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 B in d:
        val = d[B]
        if C in d:
            d[C] = d[C] + d[B]
        else:
            d[C] = d[B]
        d.pop(B)

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

# Optimized solution
#!/usr/bin/env python3
import sys
import collections

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

d = collections.Counter(A)
ans = 0
for k, v in list(d.items()):
    ans += k * v

for i in range(Q):
    B,C = list(map(int, input().split()))
    if B in d:
        val = d[B]
        ans -= B * d[B]
        ans += C * d[B]
        if C in d:
            d[C] = d[C] + d[B]
        else:
            d[C] = d[B]
        d.pop(B)

    print(ans)
# Original solution
import sys
input = sys.stdin.readline

n = int(eval(input()))
A = list(map(int,input().split()))
q = int(eval(input()))
M=[list(map(int,input().split())) for _ in range(q)]
Sum = sum(A)
d={}
for i in range(n):
    if A[i] not in d:
        d[A[i]]=1
    else:
        d[A[i]]+=1
for i in M:
    a,b=i[0],i[1]
    if a not in d:
        L = list(d.items())
        print((sum([x*y for x,y in L])))
        continue
    elif b in d:
        d[b]+=d[a]
    else:
        d[b]=d[a]
    d.pop(a)
    L = list(d.items())
    print((sum([x*y for x,y in L])))


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

n = int(eval(input()))
A = list(map(int,input().split()))
q = int(eval(input()))
M=[list(map(int,input().split())) for _ in range(q)]
Sum = sum(A)
d={}

for i in range(n):
    if A[i] not in d:
        d[A[i]]=1
    else:
        d[A[i]]+=1

for i in M:
    prev = 0
    a,b=i[0],i[1]
    if a not in d:
        print(Sum)
        continue
    
    elif b in d:
        prev = d[b]*b
        d[b]+=d[a]
    else:
        d[b]=d[a]
    
    Sum += d[b]*b-d[a]*a-prev

    d.pop(a)
    print(Sum)
# Original solution
N=int(eval(input()))
A=list(map(int,input().split()))
TMP=sum(A)
Q=int(eval(input()))
import collections
List=collections.Counter(A)
for i in range(Q):
  S,T=list(map(int,input().split()))  
  TMP=TMP+(T-S)*List[S]
  print(TMP)  
  List[T]+=List[S]
  List[S]=0

# Optimized solution
def II(): return int(eval(input()))
def MI(): return list(map(int, input().split()))
def LI(): return list(map(int, input().split()))
N=II()
A=LI()
Sum=sum(A)
Q=II()
cnt={}
queries=[]
for i in range(Q):
  b,c=MI()
  queries.append((b,c))
for a in A:
  if a not in cnt:
    cnt[a]=0
  cnt[a]+=1
#print(cnt)   
for b,c in queries:
  if b in cnt:
    Sum-=cnt[b]*b
    Sum+=cnt[b]*c
    if c not in cnt:
      cnt[c]=0
    cnt[c]+=cnt[b]
    cnt[b]=0
  print(Sum)
# Original solution
def II(): return int(eval(input()))
def MI(): return list(map(int, input().split()))
def LI(): return list(map(int, input().split()))
N=II()
A=LI()
Sum=sum(A)
Q=II()
cnt={}
queries=[]
for i in range(Q):
  b,c=MI()
  queries.append((b,c))
for a in A:
  if a not in cnt:
    cnt[a]=0
  cnt[a]+=1
#print(cnt)   
for b,c in queries:
  if b in cnt:
    Sum-=cnt[b]*b
    Sum+=cnt[b]*c
    if c not in cnt:
      cnt[c]=0
    cnt[c]+=cnt[b]
    cnt[b]=0
  print(Sum) 
  

# Optimized solution
def II(): return int(eval(input()))
def MI(): return list(map(int, input().split()))
def LI(): return list(map(int, input().split()))
N=II()
A=LI()
SA=sum(A)
Q=II()
queries=[]
for i in range(Q):
  b,c=MI()
  queries.append((b,c))
cnt={}
for a in A:
  if a not in cnt:
    cnt[a]=0
  cnt[a]+=1
for b,c in queries:
  if b in cnt:
    SA-=cnt[b]*b
    SA+=cnt[b]*c
    if c not in cnt:
      cnt[c]=0
    cnt[c]+=cnt[b]
    cnt[b]=0
  print(SA)
# 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)]

for i in bc:
    str_a = " ".join(map(str, a))
    str_a = str_a.replace(str(i[0]), str(i[1]))
    a = list(map(int, str_a.split()))
    print((sum(a)))

# 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)]
dic = {}
suma = sum(a)

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

for j in bc:
    dif = j[1] - j[0]
    if j[0] in dic:
        suma += dif * dic[j[0]]
    else:
        print(suma)
        continue
    if j[1] in dic:
        dic[j[1]] += dic.pop(j[0])
    else:
        dic[j[1]] = dic.pop(j[0])
    print(suma)
# Original solution
from collections import defaultdict


def main():
    number = int(eval(input()))
    numcount = defaultdict(int)
    for x in input().split():
        numcount[int(x)] += 1
    queries = int(eval(input()))
    for _ in range(queries):
        before, after = [int(x) for x in input().split()]
        tmp = numcount[before]
        numcount[before] -= tmp
        numcount[after] += tmp

        print((sum(num * count for num, count in list(numcount.items()))))


if __name__ == '__main__':
    main()


# Optimized solution
from collections import defaultdict


def main():
    number = int(eval(input()))
    numcount = defaultdict(int)
    for x in input().split():
        numcount[int(x)] += 1
    queries = int(eval(input()))
    ans = sum(num * count for num, count in list(numcount.items()))
    for _ in range(queries):
        before, after = [int(x) for x in input().split()]
        tmp = numcount[before]
        numcount[before] -= tmp
        numcount[after] += tmp
        ans += (after - before) * tmp
        print(ans)


if __name__ == '__main__':
    main()
# Original solution
n = int(eval(input()))
a = list(map(int,input().split()))
q = int(eval(input()))
dic = {}
s = sum(a)
for i in range(1,10**5+1):
    if i in a:
        dic[i] = a.count(i)
    else:
        dic[i] = 0
lis=[]
for j in range(q):
    b,c = list(map(int,input().split()))
    lis.append([b,c])

for p in lis:
    s += (p[1]-p[0])*dic[p[0]]
    dic[p[1]] += dic[p[0]]
    dic[p[0]] = 0
    print(s)

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

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

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

max_i = max(max(A), max(C))

A_N = [A.count(i) * i for i in range(max_i + 1)]

sum_i = sum(A_N)

for i in range(Q):
    diff_replace = int(A_N[B[i]] / B[i]) * C[i]
    sum_i += diff_replace - A_N[B[i]]
    
    A_N[C[i]] += diff_replace
    A_N[B[i]] = 0
    
    print(sum_i)


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

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

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

max_i = max(max(A), max(C))

A_N = [0 for _ in range(max_i + 1)]
sum_i = 0

for a in A:
    A_N[a] += a
    sum_i += a

for i in range(Q):
    diff_replace = int(A_N[B[i]] / B[i]) * C[i]
    sum_i += diff_replace - A_N[B[i]]
    
    A_N[C[i]] += diff_replace
    A_N[B[i]] = 0
    
    print(sum_i)
# Original solution
#create date: 2020-06-29 09:42

import sys
stdin = sys.stdin

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n = ni()
    a = na()
    alist = [0] * (10**5 + 1)
    for ai in a:
        alist[ai] += 1
    q = ni()
    for i in range(q):
        b, c = na()
        alist[c] += alist[b]
        alist[b] = 0
        ans = 0
        
        for i, num in enumerate(alist):
            ans += i*num
        print(ans)

if __name__ == "__main__":
    main()

# Optimized solution
#create date: 2020-06-29 09:42

import sys
stdin = sys.stdin

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n = ni()
    a = na()
    alist = [0] * (10**5 + 1)
    for ai in a:
        alist[ai] += 1
    asum = 0
    for i, num in enumerate(alist):
            asum += i*num

    q = ni()
    for i in range(q):
        b, c = na()
        asum += alist[b] * (c-b)
        alist[c] += alist[b]
        alist[b] = 0
        print(asum)

if __name__ == "__main__":
    main()
# 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)]

# print(n,q)
# print(A)
# print(BC)
C = {}
for a in A:
    if a in C:
        C[a]+=1
    else:
        C[a]=1

s = sum(A)
# print(a)
for b,c in BC:
    # print(b,c)
    d = c-b
    if not b in C:
        C[b] = 0
    s += d*C[b]
    if not c in C:
        C[c] = 0
    C[c] += C[b]
    C[b] = 0
    print(s)


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

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

s = sum(A)
for b, c in BC:
    if not b in C: C[b] = 0
    if not c in C: C[c] = 0
    s += (c - b) * C[b]
    C[c] += C[b]
    C[b] = 0
    print(s)
# Original solution
#n, m, q = map(int, input().split())
#List = list(map(int, input().split()))
#req = [list(map(int, input().split())) for _ in range(q)]
#t = t[:-1]
#print(ans[j], end = "") 改行無しで出力
#[0]*n
n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
req = [list(map(int, input().split())) for _ in range(q)]
count = [0]*100000
sum = 0
ans = [0]*q

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

for j in a:
    sum += j

for k in range(q):
    diff = req[k][1] - req[k][0]
    diff = diff * count[req[k][0] - 1]
    count[req[k][1] - 1] += count[req[k][0] - 1]
    count[req[k][0] - 1] = 0
    sum += diff
    ans[k] = sum

for l in range(q):
    print((ans[l]))


# Optimized solution
#n, m, q = map(int, input().split())
#List = list(map(int, input().split()))
#req = [list(map(int, input().split())) for _ in range(q)]
#t = t[:-1]
#print(ans[j], end = "") 改行無しで出力
#[0]*n
n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
req = [list(map(int, input().split())) for _ in range(q)]
count = [0]*100001
sum = sum(a)

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

for k in range(q):
    sum += (req[k][1] - req[k][0]) * count[req[k][0]]
    count[req[k][1]] += count[req[k][0]]
    count[req[k][0]] = 0
    print(sum)
# Original solution
import collections
N = int(eval(input()))
A = [int(x) for x in input().split()]
a = collections.Counter(A)
Q = int(eval(input()))
for i in range(Q):
  B, C = list(map(int, input().split()))
  if B in a:
    a[C] += a[B]
    a[B] = 0
  tmp = 0
  for i in a:
    tmp += a[i] * i
  print(tmp)

# Optimized solution
N = int(eval(input()))
A = [int(x) for x in 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
#!/usr/bin/env python3
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()))
    A = [int(str(a).replace(b, c)) for a in A]
    print((sum(A)))

# Optimized solution
#!/usr/bin/env python3
n = int(eval(input())) 
A = list(map(int, input().split()))
q = int(eval(input())) 
for i in range(q):
    b, c = list(map(str, input().split()))
    if int(b) in A:
        A = [int(str(a).replace(b, c)) for a in A]
    print((sum(A)))
# Original solution
from collections import defaultdict
import sys

input = sys.stdin.readline

def main():
    N = int(eval(input()))
    raw_number = list(map(int, input().split()))
    numbers = defaultdict(int)

    for num in raw_number:
        numbers[num] += 1

    Q = int(eval(input()))

    for _ in range(Q):
        a, b = list(map(int, input().split()))
        numbers[b] += numbers[a]
        numbers.pop(a)
        print((sum([v*k for (k, v) in list(numbers.items())])))
    
main()

# Optimized solution
class Counter(dict):
    def __missing__(self, i):
        return 0

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

total = sum(a)

bucket = Counter()

for item in a:
    bucket[item] += 1

for i in range(q):
    b, c = list(map(int, input().split()))
    taken = bucket[b]
    total -= bucket[b]*b
    bucket[c] += bucket[b]
    bucket[b] = 0
    total += taken*c
    print(total)
# Original solution
from collections import defaultdict
 
n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
 
total = sum(a)
 
numbers = defaultdict(int)
 
for item in a:
    numbers[item] += 1
    
for i in range(q):
    b, c = list(map(int, input().split()))
    taken = numbers[b]
    total -= numbers[b]*b
    numbers[c] += numbers[b]
    numbers[b] = 0
    total += taken*c
    print(total)

# Optimized solution
from collections import defaultdict
 
n = int(eval(input()))
raw_numbers = list(map(int, input().split()))
q = int(eval(input()))
 
total = sum(raw_numbers)
 
numbers = defaultdict(int)
 
for item in raw_numbers:
    numbers[item] += 1

for i in range(q):
    b, c = list(map(int, input().split()))
    taken = numbers[b]
    total -= numbers[b]*b
    numbers[c] += numbers[b]
    numbers[b] = 0
    total += taken*c
    print(total)
# Original solution
from collections import defaultdict
 
n = int(eval(input()))
raw_numbers = list(map(int, input().split()))
q = int(eval(input()))
 
total = sum(raw_numbers)
 
numbers = defaultdict(int)
 
for item in raw_numbers:
    numbers[item] += 1

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

# Optimized solution
from collections import defaultdict
 
n = int(eval(input()))
raw_numbers = list(map(int, input().split()))
q = int(eval(input()))
 
total = sum(raw_numbers)
 
numbers = defaultdict(int)
 
for item in raw_numbers:
    numbers[item] += 1

for i in range(q):
    a, b = list(map(int, input().split()))
    taken = numbers[a]
    total -= numbers[a]*a
    numbers[b] += numbers[a]
    numbers[a] = 0
    total += taken*b
    print(total)
# Original solution
from collections import defaultdict
 
N = int(eval(input()))
raw_numbers = list(map(int, input().split()))
 
total = sum(raw_numbers)
 
numbers = defaultdict(int)
 
for num in raw_numbers:
    numbers[num] += 1

Q = int(eval(input()))

for i in range(Q):
    a, b = list(map(int, input().split()))
    taken = numbers[a]
    total -= numbers[a]*a
    numbers[b] += numbers[a]
    numbers[a] = 0
    total += taken*b
    print(total)

# Optimized solution
from collections import defaultdict
 
N = int(eval(input()))
raw_numbers = list(map(int, input().split()))
Q = int(eval(input()))
 
total = sum(raw_numbers)
 
numbers = defaultdict(int)
 
for num in raw_numbers:
    numbers[num] += 1

for _ in range(Q):
    a, b = list(map(int, input().split()))
    taken = numbers[a]
    total -= numbers[a]*a
    numbers[b] += numbers[a]
    numbers[a] = 0
    total += taken*b
    print(total)
# Original solution
from collections import defaultdict
 
N = int(eval(input()))
raw_numbers = list(map(int, input().split()))
Q = int(eval(input()))
 
total = sum(raw_numbers)
 
numbers = defaultdict(int)
 
for num in raw_numbers:
    numbers[num] += 1

for _ in range(Q):
    a, b = list(map(int, input().split()))
    taken = numbers[a]
    total -= numbers[a]*a
    numbers[b] += numbers[a]
    numbers[a] = 0
    total += taken*b
    print(total)

# Optimized solution
from collections import defaultdict
 
N = int(eval(input()))
raw_numbers = list(map(int, input().split()))
Q = int(eval(input()))
 
total = sum(raw_numbers)
 
numbers = defaultdict(int)
 
for num in raw_numbers:
    numbers[num] += 1

for _ in range(Q):
    a, b = list(map(int, input().split()))
    taken = numbers[a]
    total -= numbers[a]*a
    numbers[b] += numbers[a]
    total += taken*b
    numbers[a] = 0
    print(total)
# Original solution
N=int(eval(input()))
A=[str(i) for i in input().split()]
Q=int(eval(input()))

BC= [list(map(str, input().split())) for i in range(Q)]
#
# print(BC)
for i in range(Q):
    A = [s.replace(BC[i][0],BC[i][1]) for s in A]

    print((sum(int(j) for j in A)))


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


N=int(eval(input()))
A=[str(i) for i in input().split()]
Q=int(eval(input()))

BC= [list(map(str, input().split())) for i in range(Q)]
#
# print(BC)
for i in range(Q):
    A = [s.replace(BC[i][0],BC[i][1]) for s in A]

    print((sum(int(j) for j in A)))
# 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())))
A_sum=sum(A)
A_count=[]
for i in range(100001):
    A_count.append(A.count(i))
for i in range(Q):
    A_sum+=(BC[i][1]-BC[i][0])*A_count[BC[i][0]]
    A_count[BC[i][1]]+=A_count[BC[i][0]]
    print(A_sum)

# 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())))
A_sum=sum(A)
A_count=[]
for i in range(100001):
    A_count.append(A.count(i))
for i in range(Q):
    A_sum+=(BC[i][1]-BC[i][0])*A_count[BC[i][0]]
    A_count[BC[i][1]]+=A_count[BC[i][0]]
    A_count[BC[i][0]]=0
    print(A_sum)
# 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())))
A_sum=sum(A)
A_count=[]
for i in range(100001):
    A_count.append(A.count(i))
for i in range(Q):
    B,C=(list(map(int,input().split())))
    A_sum+=(C-B)*A_count[B]
    A_count[C]+=A_count[B]
    A_count[B]=0
    print(A_sum)

# Optimized solution
from collections import Counter

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())))
A_sum=sum(A)
A_count=Counter(A)

for i in range(Q):
    B,C=(list(map(int,input().split())))
    A_sum+=(C-B)*A_count[B]
    A_count[C]+=A_count[B]
    A_count[B]=0
    print(A_sum)
# Original solution
import math

n = int(eval(input()))
aL = list(map(int, input().split(" ")))
q = int(eval(input()))
bcL = [list(map(int, input().split(" "))) for _ in range(q)]

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

for a in aL:
    sums[a] += 1

for b, c in bcL:
    sums[c] += sums[b]
    sums[b] = 0
    _sum = 0
    for i, s in enumerate(sums):
        _sum += i * s
    print(_sum)

# Optimized solution
import math

n = int(eval(input()))
aL = list(map(int, input().split(" ")))
q = int(eval(input()))
bcL = [list(map(int, input().split(" "))) for _ in range(q)]

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

for a in aL:
    sums[a] += 1

_sum = 0
for i, a in enumerate(sums):
    _sum += i * a

for b, c in bcL:
    _sum += (c - b) * sums[b]

    sums[c] += sums[b]
    sums[b] = 0
    print(_sum)
# Original solution
N = int(input().strip())

A = {}
for a in [int(x) for x in input().strip().split(" ")]:
    A.setdefault(a, 0)
    A[a] += 1

Q = int(input().strip())

for q in range(Q):
    B, C = [int(x) for x in input().strip().split(" ")]
    if B in A:
        A.setdefault(C, 0)
        A[C] += A[B]
        del A[B]
    print((sum([k * v for k, v in list(A.items())])))


# Optimized solution
N = int(input().strip())

A = {}
for a in [int(x) for x in input().strip().split(" ")]:
    A.setdefault(a, 0)
    A[a] += 1
total = sum([k * v for k, v in list(A.items())]) 

Q = int(input().strip())

for q in range(Q):
    B, C = [int(x) for x in input().strip().split(" ")]
    if B in A:
        A.setdefault(C, 0)
        dif = (C - B) * A[B]
        A[C] += A[B]
        del A[B]
        total += dif
    print(total)
# Original solution
import sys
N = int(eval(input()))
A = list(map(int, input().split()))
total = sum(A)
num = [0] * (10 ** 5 + 1)
for a in A:
    num[a] += 1

Q = int(eval(input()))
for _ in range(Q):
    b, c = list(map(int, sys.stdin.readline().split()))
    total -= b * num[b]
    total += c * num[b]
    num[c] += num[b]
    num[b] = 0
    print(total)

# Optimized solution
import sys
N = int(eval(input()))
A = tuple(map(int, input().split()))
count = [0] * (10 ** 5 + 1)

for a in A:
    count[a] += 1
s = sum(A)

Q = int(eval(input()))
for _ in range(Q):
    b, c = list(map(int, sys.stdin.readline().split()))
    s -= count[b] * b
    s += count[b] * c
    count[c] += count[b]
    count[b] = 0
    print(s)
# Original solution
import math
# from collections import Counter

# N = int(input()) # 1行, 単数
# arg = list(map(int, input().split())) # 1行, 配列
# D = Counter(arg) # 出現数カウント

# print('Yes')
# print('No')
# print()
import collections
N = int(eval(input())) # 1行, 単数
A = list(map(int, input().split())) # 1行, 配列
Q = int(eval(input())) # 1行, 単数
counter = collections.Counter(A)

while True:
  try:
    # ans = 0
    B,C = list(map(int, input().split())) # 1行, 配列
    # counter[C] = counter[C] + counter[B]
    # del counter[B]
    # for i in counter.keys():
    #   ans = ans + i * counter[i]
    A = [C if i == B else i for i in A]
    
    print((sum(A)))
  except:
    break;

# Optimized solution
import math
# from collections import Counter

# N = int(input()) # 1行, 単数
# arg = list(map(int, input().split())) # 1行, 配列
# D = Counter(arg) # 出現数カウント

# print('Yes')
# print('No')
# print()
import collections
N = int(eval(input())) # 1行, 単数
A = list(map(int, input().split())) # 1行, 配列
Q = int(eval(input())) # 1行, 単数
counter = collections.Counter(A)
ans = 0
for i in list(counter.keys()):
  ans = ans + i * counter[i]

while True:
  try:

    B,C = list(map(int, input().split())) # 1行, 配列
    old = C * counter[C] + counter[B] * B
    counter[C] = counter[C] + counter[B]
    del counter[B]
    new = C * counter[C]
    ans = ans + (new - old)
    print(ans)
  except:
    break;
# Original solution
N = int(eval(input()))
A = list(map(str, input().split()))
Q = int(eval(input()))
for i in range(1,Q+1):
  x, y = list(map(str, input().split()))
  A = [s.replace(x, y) for s in A]
  A_int = list(map((lambda x: int(x)), A))
  print((sum(A_int)))

# Optimized solution
N = int(eval(input()))
A = list(map(str, input().split()))
Q = int(eval(input()))
for i in range(1,Q+1):
  x, y = list(map(str, input().split()))
  A = [s.replace(x, y) for s in A]
  A_int = list(map(int, A))
  print((sum(A_int)))
# 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)]
temp = [0] * (10**5 + 1)

for i in a:
    temp[i] += i

for i in range(q):
    b,c = bc[i][0],bc[i][1]
    temp[c] += (temp[b]//b) * c
    temp[b] = 0
    print((sum(temp)))


# 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 _ in range(q)]
    temp = [0] * (10**5 + 1)
    
    for i in a:
        temp[i] += 1
    ans = sum(a)
    for i in range(q):
        b,c = bc[i][0],bc[i][1]
        ans = ans - temp[b] * b + temp[b] * c 
        print(ans)
        temp[c] += temp[b]
        temp[b] = 0
if __name__ == "__main__":
    main()
# Original solution
from collections import defaultdict
n = int(eval(input()))
a = list(map(int, input().split()))
dic = defaultdict(int)
for i in range(n):
    dic[a[i]]+=1
Q= int(eval(input()))
for q in range(Q):
    b, c = list(map(int, input().split()))
    dic[c]+=dic[b]
    dic[b]=0
    ans = 0
    for j in dic:
        ans += dic[j]*j
    print(ans)

# Optimized solution
from collections import defaultdict
n = int(eval(input()))
a = list(map(int, input().split()))
dic = defaultdict(int)
for i in range(n):
    dic[a[i]]+=1
ans = 0
for j in dic:
    ans += dic[j]*j

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