CodeEff/ECCO huggingface dataset:
# Original solution
import collections

def A_sum(A):
  s = 0
  for i in list(A.items()):
    s+=i[0]*i[1]
  print(s)

def A_rep(A,f,t):
  f_num = A.get(f)
  if f_num != None:
      A[t]=A.get(t,0) + f_num
      A.pop(f)

N = int(eval(input()))
A = collections.Counter(list(map(int,input().split())))
Q = int(eval(input()))
for i in range(Q):
  b,c = list(map(int,input().split()))
  A_rep(A,b,c)
  A_sum(A)


# Optimized solution
import collections

def A_sum(A):
  s = 0
  for i in list(A.items()):
    s+=i[0]*i[1]
  return s

def A_rep(A,f,t):
  f_num = A.get(f)
  if f_num != None:
      A[t]=A.get(t,0) + f_num
      diff = f_num*(t-f)
      A.pop(f)
      return diff
  return 0

N = int(eval(input()))
A = collections.Counter(list(map(int,input().split())))
Q = int(eval(input()))
ans = A_sum(A)
for i in range(Q):
  b,c = list(map(int,input().split()))
  diff = A_rep(A,b,c)
  if diff !=0:
    ans+=diff
  print(ans)
# Original solution
import sys
import collections

ri = lambda: int(sys.stdin.readline())
rl = lambda: list(map(int, sys.stdin.readline().split()))

n = ri()
a = rl()
il = list(set(a))
li = collections.Counter(a)
q = ri()
res = []
bc = [rl() for _ in range(q)]
for vvv in bc:
    b, c = vvv
    if b in il:
        if c in il:
            li[c] += li[b]
        else:
            li[c] = li[b]
            il.append(c)
        li[b] = 0
        il.remove(b)
    ans = 0
    for k, v in list(li.items()):
        ans += k*v
    res.append(ans)
for aaa in res:
    print(aaa)



# Optimized solution
import sys
ri = lambda: int(sys.stdin.readline())
rm = lambda: list(map(int, sys.stdin.readline().split()))
rl = lambda: list(map(int, sys.stdin.readline().split()))

n = ri()
a = rl()
t = sum(a)
cnt_li = [0]*10**5
for i in a:
    cnt_li[i-1] += 1

for _ in range(ri()):
    b, c = rm()
    t += (c-b)*cnt_li[b-1]
    print(t)
    cnt_li[c-1] += cnt_li[b-1]
    cnt_li[b-1] = 0
# Original solution
n = int(eval(input()))
A = tuple(map(int, input().split()))
Q = int(eval(input()))
Bs = [tuple(tuple(map(int, input().split()))) for i in range(Q)]
X = [0]
for i in range(1, 100001):
  X.append(A.count(i))
S = sum(A)
for B in Bs:
  S += X[B[0]]*(B[1]-B[0])
  X[B[1]] += X[B[0]]
  X[B[0]] = 0
  print(S)

# Optimized solution
n = int(eval(input()))
A = list(map(int, input().split()))
S = sum(A)
D = {c:0 for c in range(1, 10**5+1)}
for a in A:
  D[a] += 1
for _ in range(int(eval(input()))):
  B, C = list(map(int, input().split()))
  S += (C-B)*D[B]
  D[C] += D[B]
  D[B] = 0
  print(S)
# Original solution
import bisect

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

Q = int(eval(input()))

A.sort()
B = list(set(A))
X = [[0 for j in range(2)] for i in range(len(B))]
for i in range(len(B)):
  b=bisect.bisect_left(A,B[i])
  c=bisect.bisect_right(A,B[i])
  num = c - b #B[i]の要素数
  X[i][0] = B[i]
  X[i][1] = num

M = [0 for j in range(Q)]
N = [0 for j in range(Q)]
for i in range(Q):
  M[i],N[i] = list(map(int,input().split()))

ans = int()
for i in range(Q):
  x = M[i]
  y = N[i]
  ans = 0
  for j in range(len(B)):
    if X[j][0] == x:
      X[j][0] = y
    ans += X[j][0] * X[j][1]
  print(ans)
  #print(X)
  



# 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)]
B, C = [list(i) for i in zip(*BC)]
#print(B,C,BC)

dic = {}
for a in A:
  dic[a] = dic.get(a,0) + 1
#print(dic)

ans = sum(A)
for q in range(Q):
  ans = ans + (C[q]-B[q]) * dic.get(B[q],0)
  dic[C[q]] = dic.get(B[q],0) + dic.get(C[q],0)
  dic[B[q]] = 0
  print(ans)
# Original solution
n = int(eval(input()))
a = list(map(str, input().split()))
q = int(eval(input()))
b = [0] * q
c = [0] * q
for i in range(q):
    b[i], c[i] = list(map(str, input().split()))

tmp = ','.join(a)

for i in range(q):
    tmp = tmp.replace(b[i], c[i])
    ans = list(tmp.split(','))
    ans2 = 0
    for j in ans:
        ans2 += int(j)
    print(ans2)

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

tmp = ','.join(a)

for i in range(q):
    tmp = tmp.replace(b[i], c[i])
    ans = list(map(int, tmp.split(',')))
    print((sum(ans)))
# Original solution
import sys

def L(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI():return list(map(int,sys.stdin.readline().rstrip().split()))
def LS():return list(sys.stdin.readline().rstrip().split())

import re
def solver(N,A,Q,BC):

    for b,c in BC:
        #print(A,':',b,'->',c)
        bef = '[\s]*'+str(b)
        aft = ' '+str(c)
        #print("bef,aft=",bef,aft)
        A = re.sub(bef, aft, A)
        #A = A.replace(' '+str(b), ' '+str(c))
        #print(A)
        print((sum(map(int,A.split()))))


def main():
    N = I()
    #A = LI()
    A = L()
    Q = I()
    BC = []*Q
    BC = [LI() for _ in range(Q)]

    #print("N,Q=",N,Q)
    #print("A=",A)
    #print("BC=",BC)

    solver(N,A,Q,BC)

if __name__ == '__main__':
    main()


# Optimized solution
import sys

def L(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI():return list(map(int,sys.stdin.readline().rstrip().split()))
def LS():return list(sys.stdin.readline().rstrip().split())

import collections

def solver(N,A,Q,BC):
    T = sum(map(int,A))
    #print(T)

    Nums = collections.Counter(A)
    #print(Nums)
    for b,c in BC:
        diff = c - b

        T += Nums[b] * diff
        print(T)

        Nums[c] += Nums[b]
        Nums[b] = 0


def main():
    N = I()
    A = LI()
    Q = I()
    BC = []*Q
    BC = [LI() for _ in range(Q)]

    solver(N,A,Q,BC)

if __name__ == '__main__':
    main()
# Original solution
n = int(eval(input()))+1
a = list(map(int, input().split()))
cnt = []

sum = 0
for i in range(100005):
    cnt.append(a.count(i))
    sum += i * cnt[i]

q = int(eval(input()))

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


# Optimized solution
n = int(eval(input()))+1
a = list(map(int, input().split()))
cnt = []
for i in range(100005):
    cnt.append(0)
sum = 0

for i in a:
    cnt[i] += 1
for i in range(100005):
    sum += i * cnt[i]

q = int(eval(input()))

for i in range(q):
    b, c = list(map(int, input().split()))
    sum += (c-b)*cnt[b]
    cnt[c] += cnt[b]
    cnt[b] = 0
    print(sum)
# Original solution
n = int(eval(input()))+1
a = list(map(int, input().split()))
cnt = []
for i in range(100005):
    cnt.append(0)
sum = 0

for i in a:
    cnt[i] += 1
for i in range(100005):
    sum += i * cnt[i]

q = int(eval(input()))

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






# Optimized solution
n = int(eval(input()))+1
a = list(map(int, input().split()))
cnt = []
for i in range(100005):
    cnt.append(0)
sum = 0

# a.count(i)を使うとTLE
# count()がO(N)のためfor内で使用するとO(N2)になる？
for i in a:
    cnt[i] += 1
for i in range(100005):
    sum += i * cnt[i]

q = int(eval(input()))
ans = []
for i in range(q):
    b, c = list(map(int, input().split()))
    sum += (c-b)*cnt[b]
    cnt[c] += cnt[b]
    cnt[b] = 0
    ans.append(sum)

for i in ans:
    print(i)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))

data = [0] * 100001

def my_sum(d):
    retval = 0

    for i in range(len(d)):
        if d[i] == 0:
            continue
        retval += d[i] * i
    return retval

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

    if data[B] != 0:
        data[C] += data[B]
        data[B] = 0
 
    print((my_sum(data)))

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

data = [0] * 100001

def my_sum(d):
    retval = 0

    for i in range(len(d)):
        if d[i] == 0:
            continue
        retval += d[i] * i
    return retval

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

sum_val = my_sum(data)

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

    sum_val = sum_val - (data[B] * B) + (data[B] * C)

    data[C] += data[B]
    data[B] = 0

    print(sum_val)
# Original solution
from collections import Counter
n = int(eval(input()))
a = list(map(int,input().split()))
x = 0
count = Counter(a)
q = int(eval(input()))
b,c = [],[]
for i in range(q):
    bi,ci = list(map(int,input().split()))
    b.append(bi),c.append(ci)
for i in range(q):
    bi,ci = b[i],c[i]
    count[ci]+=count[bi]
    count[bi] = 0
    x = 0
    for k,v in list(count.items()):
        x+=k*v
    print(x)


# Optimized solution
from collections import Counter
n = int(eval(input()))
a = list(map(int,input().split()))
x = sum(a)
count = Counter(a)

q = int(eval(input()))
b,c = [],[]

for i in range(q):
    bi,ci = list(map(int,input().split()))
    b.append(bi),c.append(ci)
for i in range(q):
    bi,ci = b[i],c[i]
    x -= bi*count[bi]
    x += ci*count[bi]
    count[ci]+=count[bi]
    count[bi] = 0
    print(x)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
D = {}
for i in A:
  if i in D:
    D[i] += 1
  else:
    D[i] = 1
for i in range(Q):
  s = 0
  bi, ci = list(map(int, input().split()))
  if bi in D:
    if ci in D:
      D[ci] += D[bi]
    else:
      D[ci] = D[bi]
    del D[bi]
  for j in D:
    s += j * D[j]
  print(s)

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

for i in A:
  if i in D:
    D[i] += 1
  else:
    D[i] = 1
s = 0
for j in D:
  s += j * D[j]
for i in range(Q):
  bi, ci = list(map(int, input().split()))
  if bi in D:
    if ci in D:
      D[ci] += D[bi]
    else:
      D[ci] = D[bi]
    s += (ci - bi) * D[bi]
    del D[bi]
  print(s)
# Original solution
n=int(eval(input()))
member = [0]*(10**5+1)

li = list(map(int,input().split()))
for i in range(n):
  member[li[i]] +=1

q = int(eval(input()))
for j in range(q):
  x,y = input().split()
  x=int(x)
  y=int(y)
  member[y]+=member[x]
  member[x]=0
  total = 0
  for k in range(10**5+1):
    total += k*member[k]
  print(total)

# Optimized solution
n=int(eval(input()))
member = [0]*(10**5+1)
 
li = list(map(int,input().split()))
total = sum(li)
for i in range(n):
  member[li[i]] +=1
 
q = int(eval(input()))
for j in range(q):
  x,y = input().split()
  x=int(x)
  y=int(y)
  total += (y-x)*member[x]
  print(total)
  member[y]+=member[x]
  member[x]=0
# Original solution
from collections import Counter
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))

counter = Counter(A)
for i in range(Q):
  b, c = list(map(int, input().split()))
  count_b = counter[b]
  counter[b]-= count_b
  counter[c] += count_b
  print((sum([k*v for k, v in list(counter.items())])))

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

counter = Counter(A)
ans = sum(A)
for i in range(Q):
  b, c = list(map(int, input().split()))
  count_b = counter[b]
  counter[b] -= count_b
  counter[c] += count_b
  ans = ans - b*count_b + c*count_b
  print(ans)
# Original solution
def solve():
    n=int(eval(input()))
    r=range
    a=list(map(int, input().split()))
    q=int(eval(input()))
    
    for _ in r(q):
        bi, ci = list(map(int, input().split()))
        c=0
        for i in r(n):
            if a[i] == bi:
                a[i]=ci
            c+=a[i]
        print(c)


if __name__ == '__main__':
    t=1
    for _ in range(t):
        solve()


# Optimized solution
def solve():
    n=int(eval(input()))
    r=range
    a=list(map(int, input().split()))
    q=int(eval(input()))

    d={}
    s=0
    for i in r(n):
        cu=a[i]

        if d.get(cu, None):
            d[cu]+=1
        else:
            d[cu]=1
        
        s+=cu
    
    for _ in r(q):
        bi, ci = list(map(int, input().split()))
        diff=ci-bi

        if d.get(ci, None):
            d[ci]+=d.get(bi, 0)
        else:
            d[ci]=d.get(bi, 0)

        s+=diff * d.get(bi, 0)
        print(s)

        if d.get(bi, None):
            d[bi]=0


if __name__ == '__main__':
    t=1
    for _ in range(t):
        solve()
# Original solution
N = int(eval(input()))
lsn = list(map(int,input().split()))
Q = int(eval(input()))
ls2 = []
for i in range(Q):
    Bi,Ci = list(map(int,input().split()))
    ls2.append([Bi,Ci])
index = [0]*(10**5+1)
for i in range(10**5+1):
    index[i] = lsn.count(i)
sum1 = sum(lsn)
for l in ls2:
    sum1 = sum1 +(l[1]-l[0])*index[l[0]]
    index[l[1]] += index[l[0]]
    index[l[0]] = 0
    print(sum1)

# Optimized solution
import collections
N = int(eval(input()))
lsn = list(map(int,input().split()))
Q = int(eval(input()))
ls2 = []
for i in range(Q):
    Bi,Ci = list(map(int,input().split()))
    ls2.append([Bi,Ci])
index = collections.Counter(lsn)
sum1 = sum(lsn)
for l in ls2:
    sum1 = sum1 +(l[1]-l[0])*index[l[0]]
    index[l[1]] += index[l[0]]
    index[l[0]] = 0
    print(sum1)
# Original solution
import sys
from collections import Counter

input = sys.stdin.readline


def S():
    return input().rstrip()


def I():
    return int(eval(input()))


def MI():
    return list(map(int, input().split()))


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


n = I()
al = list(MI())
q = I()

a = Counter(al)

for i in range(q):
    b, c = MI()
    if b in list(a.keys()):
        v = a.pop(b)
        if c in list(a.keys()):
            a[c] += v
        else:
            a[c] = v

    print_sum(a)


# Optimized solution
import sys
from collections import Counter

input = sys.stdin.readline


def S():
    return input().rstrip()


def I():
    return int(eval(input()))


def MI():
    return list(map(int, input().split()))


n = I()
al = list(MI())
q = I()

a = Counter(al)

s = sum(al)
for _ in range(q):
    b, c = MI()
    if b in list(a.keys()):
        v = a.pop(b)
        if c in list(a.keys()):
            a[c] += v
        else:
            a[c] = v
        s += (c - b) * v
    print(s)
# Original solution
import collections

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

ans = sum(A)

for i in range(Q):
    A = [BC[i][1] if x==BC[i][0] else x for x in A]
    print((sum(A)))

# Optimized solution
import collections

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

ans = sum(A)
d = collections.defaultdict(int)

for key in A:
    d[key] += 1

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

dic = {a+1:0 for a in range(10**5)}
for a in A:
  dic[a] += 1

for i in range(Q):
  B, C = list(map(int, input().split()))
  dic[C] += dic[B]
  dic[B] = 0
  
  ans = 0
  for k, v in list(dic.items()):
    ans += k*v
  
  print(ans)


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

ans = sum(A)
dic = {a+1:0 for a in range(10**5)}
for a in A:
  dic[a] += 1

for i in range(Q):
  B, C = list(map(int, input().split()))
  ans += dic[B]*(C-B)
  dic[C] += dic[B]
  dic[B] = 0
    
  print(ans)
# Original solution
from collections import Counter
import copy

N = int(eval(input()))

target_list = list(map(int, input().split()))
check = [Counter(target_list)]
result_list = [sum(target_list)]


for index in range(int(eval(input()))):
    B, C = list(map(int, input().split()))
    if B in check[index]:
        result = copy.deepcopy(check[index])
        change = result.pop(B)
        diff = change * (C - B)
        if C in result:
            result[C] = result[C] + change
        else:
            result[C] = change

    else:
        result = copy.deepcopy(check[index])
        diff = 0
    check.append(result)
    print((result_list[index] + diff))
    result_list.append(result_list[index] + diff)


# Optimized solution
from collections import Counter

N = int(eval(input()))

target_list = list(map(int, input().split()))
check = Counter(target_list)
result = sum(target_list)


for index in range(int(eval(input()))):
    B, C = list(map(int, input().split()))
    if B in check:
        result = result + (check[B]) * (C - B)
        if C in check:
            check[C] = check[C] + check[B]
        else:
            check[C] = check[B]
        check[B] = 0
    print(result)
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
d = dict()

for i in range(n):
    if a[i] not in list(d.keys()):
        d[a[i]] = 1
    else:
        d[a[i]] += 1

for i in range(q):
    b, c = list(map(int, input().split()))
    if b in list(d.keys()):
        if c in list(d.keys()):
            d[c] += d[b]
            del d[b]
        else:
            d[c] = d[b]
            del d[b]
    print((sum([k * v for k, v in list(d.items())])))

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

for i in range(n):
    if a[i] not in list(d.keys()):
        d[a[i]] = 1
    else:
        d[a[i]] += 1

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

for i in range(q):
    b, c = list(map(int, input().split()))
    if b in list(d.keys()):
        s += (c - b) * d[b]
        if c in list(d.keys()):
            d[c] += d[b]
            d[b] = 0
        else:
            d[c] = d[b]
            d[b] = 0
    print(s)
# Original solution
# -*- coding: utf-8 -*-
n=int(eval(input()))
numlista=list(map(int,input().split()))
#aの合計を求める(suma)
suma=0
for i in range(n):
  suma+=numlista[i]
#1~10000がそれぞれいくつあるのか調べる(numkosuu)
numkosuu=[]
for i in range(100000):
  numkosuu.append(numlista.count(i+1))
q=int(eval(input()))
for i in range(q):
  b,c=list(map(int,input().split()))
#変化したところのみ計算する
  suma+=(c-b)*numkosuu[b-1]
#個数を更新する
  numkosuu[c-1]+=numkosuu[b-1]
  numkosuu[b-1]=0
  print(suma)

# Optimized solution
# -*- coding: utf-8 -*-
n=int(eval(input()))
numlista=list(map(int,input().split()))
import collections
numkosuu=collections.Counter(numlista)
suma=0
for i in numlista:
  suma+=i
q=int(eval(input()))
for i in range(q):
  b,c=list(map(int,input().split()))
#変化したところのみ計算する
  suma+=(c-b)*numkosuu[b]
#個数を更新する
  numkosuu[c]+=numkosuu[b]
  numkosuu[b]=0
  print(suma)
# Original solution
n=int(eval(input()))
a=list(map(int,input().split()))
q=int(eval(input()))
bc=[tuple(map(int,input().split())) for _ in range(q)]
d={}
for ai in a:
    if ai not in d:
        d[ai]=0
    d[ai]+=1
# print(d)
for bi,ci in bc:
    if bi in d:
        tmp=d[bi]
        d[bi]=0
        if ci not in d:
            d[ci]=0
        d[ci]+=tmp
    ans=0
    for key in d:
        ans+=key*d[key]
    print(ans)


# Optimized solution
n=int(eval(input()))
a=list(map(int,input().split()))
q=int(eval(input()))
bc=[tuple(map(int,input().split())) for _ in range(q)]
d={}
for ai in a:
    if ai not in d:
        d[ai]=0
    d[ai]+=1
# print(d)

ans=0
for key in d:
    ans+=key*d[key]

for bi,ci in bc:
    if bi in d:
        tmp=d[bi]
        d[bi]=0
        ans-=bi*tmp
        if ci not in d:
            d[ci]=0
        d[ci]+=tmp
        ans+=ci*tmp
    print(ans)
# Original solution
import collections
n = int(eval(input()))
l = list(map(int, input().split()))
a = int(eval(input()))
c = collections.Counter(l)
for i in range(a):
    j,k =  list(map(int, input().split()))
    save = c[j]
    del c[j]
    c[k] += save
    answer = 0
    for q,r in list(c.items()):
        answer += int(q)*int(r)
    print(answer)


# Optimized solution
import collections
n = int(eval(input()))
l = list(map(int, input().split()))
a = int(eval(input()))
c = collections.Counter(l)
answer = sum(l)
for i in range(a):
    j,k =  list(map(int, input().split()))
    save = c[j]
    del c[j]
    c[k] += save
    answer = answer + k*save - j*save
    print(answer)
# Original solution
N = int(eval(input()))
A = list(map(int,input().split()))
Q = int(eval(input()))



for i in range(Q):
    X,Y = list(map(int,input().split()))

    ans = 0

    for j in range(N):
        if A[j] == X:
            ans += Y
            A[j] = Y
        else:
            ans += A[j]

    print(ans)


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

cnt_dict = {}


s = sum(A)

ans = 0

for item in A:
    if item not in cnt_dict:
        cnt_dict[item] = 0
    cnt_dict[item] += 1

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

    if X not in cnt_dict:
        cnt_dict[X] = 0
    if Y not in cnt_dict:
        cnt_dict[Y] = 0

    s -= cnt_dict[X] * X
    s += cnt_dict[X] * Y

    cnt_dict[Y] += cnt_dict[X]
    cnt_dict[X] = 0


    print(s)





'''
#初期案　遅すぎ

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



for i in range(Q):
    X,Y = map(int,input().split())

    ans = 0

    for j in range(N):
        if A[j] == X:
            ans += Y
            A[j] = Y
        else:
            ans += A[j]

    print(ans)

    '''
# Original solution
n = int(eval(input()))
As = list(map(int, input().split()))
q = int(eval(input()))
Ds = [0] * (10 ** 5 + 1)
for a in As:
  Ds[a] += 1
for i in range(q):
  b, c = list(map(int, input().split()))
  Ds[c] += Ds[b]
  Ds[b] = 0
  ans = 0
  for x, i in enumerate(Ds):
    ans += x * i
  print(ans)

# Optimized solution
n = int(eval(input()))
As = list(map(int, input().split()))
q = int(eval(input()))
Ds = [0] * (10 ** 5 + 1)
Es = [i for i in range(10 ** 5 + 1)]
for a in As:
  Ds[a] += 1
ans = sum(As)
for i in range(q):
  b, c = list(map(int, input().split()))
  ans += Ds[b] * (c-b)
  Ds[c] += Ds[b]
  Ds[b] = 0
  print(ans)
# Original solution
import collections

N = int(eval(input()))
A = list(map(int, input().split()))
S = sum(A)
Q = int(eval(input()))
T = collections.Counter(A)
for i in range(Q):
    b, c = list(map(int, input().split()))
    S += T[b]*(c-b)
    T[c] += T[b]
    T[b] = 0
    print(S)

# Optimized solution
def main():
    n, *i = list(map(int, open(0).read().split()))
    a = i[:n]
    _ = i[n]
    s = sum(a)

    m = [0] * (10 ** 5 + 1)
    for x in a:
        m[x] += 1

    ans = []
    for b, c in zip(*[iter(i[n + 1:])] * 2):
        m[c] += m[b]
        s += (c - b) * m[b]
        m[b] = 0
        ans.append(s)

    print(("\n".join(map(str, ans))))

if __name__ == '__main__':
    main()
# Original solution
import collections

N=int(eval(input()))
A=list(map(int,input().split()))
Q=int(eval(input()))
bcs=[list(map(int,input().split())) for i in range(Q)]
total=sum(A)
c=collections.Counter(A)
ans=[0 for i in range(Q)]




for i in range(Q):
    B=bcs[i][0]
    C=bcs[i][1]
    if c[B]!=0:
        tempcounterpl=collections.Counter([C for j in range(c[B])])
        total=total+c[B]*(C-B)
        ans[i]=total
        c[B]=0
        c=c+tempcounterpl
    else:
        ans[i]=total
for i in range(Q):
    print((ans[i]))

# Optimized solution
import collections

N=int(eval(input()))
A=list(map(int,input().split()))
Q=int(eval(input()))
bcs=[list(map(int,input().split())) for i in range(Q)]
total=sum(A)
c=collections.Counter(A)
ans=[0 for i in range(Q)]


for i in range(Q):
    B=bcs[i][0]
    C=bcs[i][1]
    if c[B]!=0:
        total+=c[B]*(C-B)
        ans[i]=total
        c[C]+=c[B]
        c[B]=0
    else:
        ans[i]=total
for i in range(Q):
    print((ans[i]))