CodeEff/ECCO huggingface dataset:
# Original solution
from collections import Counter
from collections import defaultdict
def main():
    n = int(eval(input()))
    a = list(map(int, input().split()))
    q = int(eval(input()))

    ac = Counter(a)
    acl = defaultdict(int)
    for ae in ac:
        acl[ae] = ac[ae]

    for _ in range(q):
        b, c = list(map(int, input().split()))
        rmed = acl[b]
        acl[c] += rmed
        acl[b] = 0
        r = sum([i * v for i, v in list(acl.items())])
        print(r)

if __name__ == '__main__':
    main()


# Optimized solution
from collections import Counter
from collections import defaultdict
def main():
    n = int(eval(input()))
    a = list(map(int, input().split()))
    q = int(eval(input()))
    asum = sum(a)
    ac = Counter(a)
    acl = defaultdict(int)
    for ae in ac:
        acl[ae] = ac[ae]

    for _ in range(q):
        b, c = list(map(int, input().split()))
        rmed = acl[b]
        acl[c] += rmed
        acl[b] = 0
        asum -= b * rmed
        asum += c * rmed
        print(asum)

if __name__ == '__main__':
    main()
# Original solution
# D - Replacing
from collections import defaultdict


def main():
    N, *X = list(map(int, open(0).read().split()))
    A, BC = X[:N], X[N + 1 :]
    cnt = defaultdict(int)
    for a in A:
        cnt[a] += 1
    total = sum(A)
    res = []
    for b, c in zip(*[iter(BC)] * 2):
        total += (c - b) * cnt[b]
        res.append(total)
        cnt[c] += cnt[b]
        cnt[b] = 0
    print(("\n".join(map(str, res))))


if __name__ == "__main__":
    main()


# Optimized solution
# D - Replacing
def main():
    N, *X = list(map(int, open(0).read().split()))
    A, BC = X[:N], X[N + 1 :]
    cnt = [0] * (10 ** 5 + 1)
    for a in A:
        cnt[a] += 1
    total = sum(A)
    res = []
    for b, c in zip(*[iter(BC)] * 2):
        total += (c - b) * cnt[b]
        res.append(total)
        cnt[c] += cnt[b]
        cnt[b] = 0
    print(("\n".join(map(str, res))))


if __name__ == "__main__":
    main()
# Original solution
if __name__=='__main__':
    N = int(eval(input()))
    A = input().split(" ")
    #A_i = [int(s) for s in A]
    Q=int(eval(input()))
    str_list=[]
    S=[]
    for i in range(Q):
        str_list.append(list(input().split()))
        
    for j in range(Q):
        A=[s.replace(str_list[j][0],str_list[j][1]) for s in A]
        A_i = [int(s) for s in A]
        print((sum(A_i)))

# Optimized solution
if __name__=='__main__':
    N = int(eval(input()))
    A = input().split(" ")
    #A_i = [int(s) for s in A]
    Q=int(eval(input()))
    str_list=[]
    S=[]
    for i in range(Q):
        str_list.append(list(input().split()))

    for j in range(Q):
        if str_list[j][0] in A:
            A=[s.replace(str_list[j][0],str_list[j][1]) for s in A]

        A_i = [int(s) for s in A]
        print((sum(A_i)))
# Original solution
N = int(eval(input()))
A = [int(x) for x in input().split()]
Q = int(eval(input()))
B_to_C = []
for i in range(Q):
  B_to_C.append([int(x) for x in input().split()])

dict_A = {}

for a in A:
  if a in dict_A:
    dict_A[a] += 1
  else:
    dict_A[a] = 1
    
for B, C in B_to_C:
  if B in dict_A:
    if C in dict_A:
      dict_A[C] += dict_A[B]
      del dict_A[B]
    else:
      dict_A[C] = dict_A[B]
      del dict_A[B]
      
  sums = 0
  for k, v in list(dict_A.items()):
    sums += k*v
  print(sums)
  
  

# Optimized solution
N = int(eval(input()))
A = [int(x) for x in input().split()]
Q = int(eval(input()))
B_to_C = []
for i in range(Q):
  B_to_C.append([int(x) for x in input().split()])

dict_A = {}
sums = 0

for a in A:
  if a in dict_A:
    dict_A[a] += 1
  else:
    dict_A[a] = 1
    
for k, v in list(dict_A.items()):
  sums += k*v
  
for B, C in B_to_C:
  if B in dict_A:
    count_of_num = dict_A[B]
    if C in dict_A:
      dict_A[C] += dict_A[B]
      del dict_A[B]
    else:
      dict_A[C] = dict_A[B]
      del dict_A[B]
      
    sums = sums - B*count_of_num + C*count_of_num
    print(sums)
    
  else:
    print(sums)
# Original solution
n=int(eval(input()))
elements= input().split()
q=int(eval(input()))
actions=[]
for i in range(q):
    actions.append(input().split())
 
number_list=[]
count_list=[]
for e in elements:
    if int(e) in number_list:
        count_list[number_list.index(int(e))]+=1
    else:
        number_list.append(int(e))
        count_list.append(1)
 
sum=0
for i in range(len(count_list)):
    sum+=number_list[i]* count_list[i]

for act in actions:
    if int(act[0]) in number_list:
        index0=number_list.index(int(act[0]))
        
        if int(act[1]) in number_list:
            index1=number_list.index(int(act[1]))
            sum+=(number_list[index1]-number_list[index0])* count_list[index0]
            count_list[index1]+=count_list[index0]
        else:
            sum+=(int(act[1])-number_list[index0])* count_list[index0]
            number_list.append(int(act[1]))
            count_list.append(count_list[index0])

        count_list[index0]=0
 
 
    print((str(sum)))

# Optimized solution
n=int(eval(input()))
elements= input().split()
q=int(eval(input()))
actions=[]
for i in range(q):
    actions.append(input().split())
 
dict={}
for e in elements:
    count=dict.get(e,0)
    count+=1
    dict[e]=count
 
sum=0
for key in list(dict.keys()):
    sum+=int(key)* dict[key]

for act in actions:
    count=dict.get(act[0],0)
    sum+=(int(act[1])-int(act[0]))*count
    count+=dict.get(act[1],0)
    dict[act[1]]=count
    dict[act[0]]=0
 
    print((str(sum)))
# Original solution
# import sys
# input = sys.stdin.readline
# import re
# import itertools
import collections

def main():
    n = int(eval(input()))
    s = input_list()
    sc = collections.Counter(s)
    q = int(eval(input()))
    ans = []
    for i in range(q):
        b, c = input_list()
        sc[c] += sc[b]
        if b in sc:
            sc.pop(b)
        a = 0
        for k, v in list(sc.items()):
            a += k*v
        ans.append(a)
    for a in ans:
        print(a)


def count_tento(l, cur):
    count = 0
    for v in l:
        if v > cur:
            count += 1
    return count

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

def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

if __name__ == '__main__':
    main()


# Optimized solution
# import sys
# input = sys.stdin.readline
# import re
# import itertools
import collections

def main():
    n = int(eval(input()))
    s = input_list()
    sc = collections.Counter(s)
    q = int(eval(input()))
    data = []
    for i in range(q):
        b, c = input_list()
        sc[c] += sc[b]
        if i > 0:
            data.append(c*sc[b] - b * sc[b])
        if b in sc:
            sc.pop(b)
    ans = 0
    for k, v in list(sc.items()):
        ans += k*v
    aa = [ans]
    for i, v in enumerate(data[::-1]):
        aa.append(aa[i] - v)
    for a in aa[::-1]:
        print(a)


def count_tento(l, cur):
    count = 0
    for v in l:
        if v > cur:
            count += 1
    return count

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

def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

if __name__ == '__main__':
    main()
# Original solution
from collections import Counter
N = int(eval(input()))
val2ind = Counter(list(map(int, input().split())))
Q = int(eval(input()))

for _ in range(Q):
    B, C = list(map(int, input().split()))
    if B in val2ind:
        cand = val2ind[B]
        del val2ind[B]
        if C not in val2ind:
            val2ind[C] = cand
        else:
            val2ind[C] += cand
    print((sum([key*val2ind[key] for key in val2ind])))

# Optimized solution
from collections import Counter
N = int(eval(input()))
val2ind = Counter(list(map(int, input().split())))
ans = sum([key*val2ind[key] for key in val2ind])
Q = int(eval(input()))

for _ in range(Q):
    B, C = list(map(int, input().split()))
    if B in val2ind:
        cand = val2ind[B]
        del val2ind[B]
        if C not in val2ind:
            val2ind[C] = cand
        else:
            val2ind[C] += cand
        ans = ans+(C-B)*cand
    print(ans)
# Original solution
from collections import defaultdict
d = defaultdict(int)

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

for el in a:
    d[el] += 1

def s_sum(dd):
    res = 0
    for k,v in list(dd.items()):
        res += k * v
    return res

for ss in bc:
    b = ss[0]
    c = ss[1]

    b_cnt = d[b]
    c_cnt = d[c]
    d[b] = 0
    d[c] += b_cnt
    print((s_sum(d)))


# Optimized solution
from collections import defaultdict
d = defaultdict(int)

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

#print(bc)

for el in a:
    d[el] += 1

def s_sum(dd):
    res = 0
    for k,v in list(dd.items()):
        res += k * v
    return res

wk = s_sum(d)

for ss in bc:
    b = ss[0]
    c = ss[1]

    b_cnt = d[b]
    c_cnt = d[c]
    d[b] = 0
    d[c] += b_cnt
    wk = wk + b_cnt*c - b_cnt*b
    print(wk)
# Original solution
import collections 
_ = eval(input())
a = [*list(map(int, input().split()))]
q = int(eval(input()))
b = [[*list(map(int,input().split()))] for _ in range(q)]
c = collections.Counter(a)
for i,j in b:
    if j not in c:
        c[j] = 0
    if i not in c:
        c[i] = 0
    c[j]+=c[i]
    c[i] = 0
    print((sum(k*v for k,v in list(c.items()))))

# Optimized solution
import collections
_ = eval(input())
a = [*list(map(int, input().split()))]
q = int(eval(input()))
s = sum(a)
b = [[*list(map(int,input().split()))] for _ in range(q)]
c = collections.Counter(a)
for i,j in b:
    if j not in c:
        c[j] = 0
    if i not in c:
        c[i] = 0
    s += (j-i)*c[i]
    c[j]+=c[i]
    c[i] = 0
    print(s)
# Original solution
import sys
import collections

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    N = int(eval(input()))
    A = [int(x) for x in input().split()]
    Q = int(eval(input()))
    BC = [[int(x) for x in input().split()] for _ in range(Q)]

    cnt = collections.Counter(A)

    ans = sum(A)

    for b, c in BC:
        ans -= cnt[b] * b
        cnt[c] += cnt[b]
        ans += cnt[b] * c
        cnt[b] = 0

        print(ans)




if __name__ == '__main__':
    main()


# Optimized solution
import sys
import collections

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    N = int(eval(input()))
    A = [int(x) for x in input().split()]
    Q = int(eval(input()))
    BC = [[int(x) for x in input().split()] for _ in range(Q)]

    ans = sum(A)
    cnt = collections.Counter(A)

    for i in range(Q):
        b, c = BC[i]
        ans -= cnt[b] * b
        ans += c * cnt[b]
        cnt[c] += cnt[b]
        cnt[b] = 0
        print(ans)



    

if __name__ == '__main__':
    main()
# Original solution
import collections
n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
dic = collections.Counter(a)
for i in range(q):
    b, c = list(map(int, input().split()))
    if b in dic and c not in dic:
        dic[c] = dic[b]
        del dic[b]
    elif b in dic and c in dic:
        dic[c] += dic[b]
        del dic[b]
    sum_list = [x * y for (x, y) in list(dic.items())]
    print((sum(sum_list)))


# Optimized solution
import collections
n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
dic = collections.Counter(a)
sum_list = [x * y for (x, y) in list(dic.items())]
sum = sum(sum_list)
for i in range(q):
    b, c = list(map(int, input().split()))
    if b in dic and c not in dic:
        sum -= (b - c) * dic[b]
        print(sum)
        dic[c] = dic[b]
        del dic[b]
    elif b in dic and c in dic:
        sum -= (b - c) * dic[b]
        print(sum)
        dic[c] += dic[b]
        del dic[b]
    else:
        print(sum)
# Original solution
def sum_dict(d, b, c):
    val = 0
    sum = 0
    if b in d:
        val = d[b]
        del d[b]
        d[c] = d.get(c, 0) + val
    for i in list(d.items()):
        sum += (i[0] * i[1])
    return sum, d
    
n = int(eval(input()))
a = [int(x) for x in input().split()]
d = {}
for i in a:
    d[i] = d.get(i, 0) + 1
s = sum(a)
q = int(eval(input()))
for i in range(q):
    b, c = list(map(int, input().split()))
    s, d = sum_dict(d, b, c)
    print(s)

# Optimized solution
def sum_dict(d, sum, b, c):
    val = 0
    if b in d:
        val = d[b]
        sum -= (b * val)
        del d[b]
        d[c] = d.get(c, 0) + val
        sum += (c * val)
    return sum, d
    
n = int(eval(input()))
a = [int(x) for x in input().split()]
d = {}
for i in a:
    d[i] = d.get(i, 0) + 1
s = sum(a)
q = int(eval(input()))
for i in range(q):
    b, c = list(map(int, input().split()))
    s, d = sum_dict(d, s, b, c)
    print(s)
# 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)] 
ans = [0]*(10**5+1)
for i in range (len(A)):
    ans[A[i]] += 1
maxlen = max(max(A),max(max(BC)))
for i in range(Q):
    ans[BC[i][1]] += ans[BC[i][0]]
    ans[BC[i][0]] = 0 

    S = 0
    for i in range(maxlen+1):
        S += ans [i]*i
    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)] 
ans = [0]*(10**5+1)

for i in range (len(A)):
    ans[A[i]] += 1
maxlen = max(max(A),max(max(BC)))
S = 0
for i in range(maxlen+1):
        S += ans [i]*i
for i in range(Q):
    ans[BC[i][1]] += ans[BC[i][0]]
    S = S + ans[BC[i][0]] *BC[i][1] - ans[BC[i][0]]*BC[i][0]
    ans[BC[i][0]] = 0
    print(S)
# Original solution
n = int(eval(input()))
a = list(map(int,input().split()))
b = {}
for x in a:
  try:
    if b[x]:
      b[x] += 1
  except:
    b[x] = 1
#print(b)
ans = sum(a)
q = int(eval(input()))
for _ in range(q):
  x,y = list(map(int,input().split()))
  try:
    if b[x] != -1:
      bx = b[x]
      b[x] = 0
  except:
    #print(b)
    print(ans)
    continue
  try:
    if b[y] != -1:
      b[y] += bx
  except:
    b[y] = bx
  #print(b)
  ans += bx * (y-x)
  print(ans)

# Optimized solution
n = int(eval(input()))
a = list(map(int,input().split()))
b = {}
for x in a:
  try:
    if b[x] != -1:
      b[x] += 1
  except:
    b[x] = 1
q = int(eval(input()))
ans = sum(a)
for _ in range(q):
  x,y = list(map(int,input().split()))
  try:
    if b[x] != -1:
      bx = b[x]
      b[x] = 0
  except:
    print(ans)
    continue
  try:
    if b[y] != -1:
      b[y] += bx
  except:
    b[y] = bx
  ans += bx * (y-x)
  print(ans)
# Original solution
n = int(eval(input()))
a = list(map(int,input().split()))
b = [0 for _ in range(100001)]
for x in a:
  b[x] += 1
ans = sum(a)
q = int(eval(input()))
for _ in range(q):
  x,y = list(map(int,input().split()))
  ans += (y-x) * b[x]
  b[x],b[y] = 0,b[x]+b[y]
  print(ans)

# Optimized solution
#15:30
n = int(eval(input()))
a = list(map(int,input().split()))
b = {}
for x in a:
  b[x] = 0
q = int(eval(input()))
inp = []
for _ in range(q):
  x,y = list(map(int,input().split()))
  b[x] = 0
  b[y] = 0
  inp.append([x,y])
for x in a:
  b[x] += 1
ans = sum(a)
for z in inp:
  x,y = z
  ans += (y-x) * b[x]
  b[x],b[y] = 0,b[x]+b[y]
  print(ans)
# Original solution
n=int(eval(input()))
a=list(map(int,input().split()))
sum_num=sum(a)
mylist=[]
for i in range(1,10**5+1):
    mylist.append(a.count(i))
q=int(eval(input()))
for i in range(q):
    b,c=list(map(int,input().split()))
    sum_num+=(c-b)*mylist[b-1]
    print(sum_num)
    mylist[c-1]+=mylist[b-1]
    mylist[b-1]=0

# Optimized solution
from collections import Counter
n=int(eval(input()))
a=list(map(int,input().split()))
sum_num=sum(a)
cnt=Counter(a)
q=int(eval(input()))
for i in range(q):
    b,c=list(map(int,input().split()))
    sum_num+=(c-b)*cnt[b]
    print(sum_num)
    cnt[c]+=cnt[b]
    cnt[b]=0
# Original solution
nb=int(eval(input()))
liste = [int(x) for x in input().split()]
liste2= [0]*((10**5)+1)
for loop in range(len(liste)):
  liste2[liste[loop]] +=1
nba = int(eval(input()))
somme = sum(liste)
for loop in range(nba):
  n,r = list(map(int,input().split()))
  roger = liste2[n]
  for loop in range(len(liste)):
    if liste[loop]==n:
      liste[loop] = r
  diff = r-n
  somme += diff * roger
  liste2[r] += liste2[n]
  liste2[n] = 0
  print(somme)

# Optimized solution
nb=int(eval(input()))
liste = [int(x) for x in input().split()]
liste2= [0]*((10**5)+1)
for loop in range(len(liste)):
  liste2[liste[loop]] +=1
nba = int(eval(input()))
somme = sum(liste)
for loop in range(nba):
  n,r = list(map(int,input().split()))
  roger = liste2[n]
  diff = r-n
  somme += diff * roger
  liste2[r] += liste2[n]
  liste2[n] = 0
  print(somme)
# Original solution
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  #空白あり
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし

from collections import Counter

N = I()
A = LI()
sum = sum(A)
a = Counter(A)

Q = I()
for i in range(Q):
    b,c = LI()
    sum += a[b]*(c-b)
    print(sum)
    a[c] += a[b]
    a[b] = 0

# Optimized solution
import sys

def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり

from collections import Counter

N = I()
A = LI()
sum = sum(A)
a = Counter(A)

Q = I()
for i in range(Q):
    b,c = LI()
    sum += a[b]*(c-b)
    print(sum)
    a[c] += a[b]
    a[b] = 0
# Original solution
n = int(eval(input()))
li_a = list(map(int, input().split()))
dic_a = {}

for a in li_a:
    dic_a[a] = dic_a.get(a, 0) + 1

q = int(eval(input()))
li_bc = list()

for i in range(q):
    li_bc.append(tuple(map(int, input().split())))

for l in li_bc:
    b = l[0]
    c = l[1]
    if b in list(dic_a.keys()):
        dic_a[c] = dic_a.get(c, 0) + dic_a.get(b, 0)
        dic_a[b] = 0
    sum = 0
    for key, val in list(dic_a.items()):
        sum += (key * val)
    print(sum)




# Optimized solution
n = int(eval(input()))
li_a = list(map(int, input().split()))
dic_a = {}

for a in li_a:
    dic_a[a] = dic_a.get(a, 0) + 1

q = int(eval(input()))
li_bc = list()

for i in range(q):
    li_bc.append(tuple(map(int, input().split())))


answer = sum(li_a)
for l in li_bc:
    b = l[0]
    c = l[1]

    diff = (c - b) * dic_a.get(b, 0)
    if b in list(dic_a.keys()):
        dic_a[c] = dic_a.get(c, 0) + dic_a.get(b, 0)
        dic_a[b] = 0
        answer += diff
    else:
        pass
    print(answer)
# Original solution
n=int(eval(input()))
a=sorted([int(i) for i in input().split()])
m={}
for i in range(n):
    x=m.get(a[i],0)
    m[a[i]]=x+1
q=int(eval(input()))
b=[]
c=[]
for i in range(q):
    b,c=[int(i) for i in input().split()]
    x=m.get(b,0)
    y=m.get(c,0)
    m[b]=0
    m[c]=x+y
    print((sum([x*m[x] for x in list(m.keys())])))


# Optimized solution
n=int(eval(input()))
m={}
s=0
for i in input().split():
    i=int(i)
    x=m.get(i,0)
    m[i]=x+1
    s+=i
q=int(eval(input()))
for i in range(q):
    b,c=[int(i) for i in input().split()]
    x=m.get(b,0)
    y=m.get(c,0)
    m[b]=0
    m[c]=x+y
    s=s-(b*x)+(c*x)
    print(s)
# Original solution
f=lambda:[*list(map(int,input().split()))]
f(); l,q=f(),f()[0]
from collections import *
C,s=Counter(l),sum(l)
for _ in range(q): b,c=f(); s+=(c-b)*C[b]; C[c]+=C[b]; C[b]=0; print(s)

# Optimized solution
f=lambda:[*list(map(int,input().split()))]
f()
s,*l=[0]*100002
for i in f(): s+=i; l[i]+=1
q=f()[0]
for _ in range(q): b,c=f(); s+=(c-b)*l[b]; l[c]+=l[b]; l[b]=0; print(s)
# Original solution
f=lambda:list(map(int,input().split()))
f()
s,*l=[0]*100002
for i in f(): s+=i; l[i]+=1
q,=f()
for _ in range(q): b,c=f(); s+=(c-b)*l[b]; l[c]+=l[b]; l[b]=0; print(s)

# Optimized solution
n,*L=list(map(int,open(0).read().split()))
A,q,Q=L[:n],L[n],L[n+1:]
s,*l=[0]*100002
for i in A: s+=i; l[i]+=1
for b,c in zip(*[iter(Q)]*2): s+=(c-b)*l[b]; l[c]+=l[b]; l[b]=0; print(s)
# Original solution
n = int(eval(input()))
a = list(map(int,input().split()))
q = int(eval(input()))
a_set = set(a)
a_dict = dict([(i, a.count(i))for i in a_set])
a_sum = sum(a)

for i in range(q):
    b,c = list(map(int,input().split()))
    if not b in a_dict:
        print(a_sum)
        continue
    a_sum = a_sum - a_dict[b]*b + a_dict[b]*c
    if c in a_dict:
        a_dict[c] = a_dict[c] + a_dict[b]
    else:
        a_dict[c] = a_dict[b]
    a_dict[b] = 0
    print(a_sum)

# Optimized solution
n = int(eval(input()))
a = list(map(int,input().split()))
q = int(eval(input()))
a_dict = dict()
for i in a:
    if i in a_dict:
        a_dict[i] = a_dict[i] + 1
    else:
        a_dict[i] = 1
a_sum = sum(a)

for i in range(q):
    b,c = list(map(int,input().split()))
    if not b in a_dict:
        print(a_sum)
        continue
    a_sum = a_sum - a_dict[b]*b + a_dict[b]*c
    if c in a_dict:
        a_dict[c] = a_dict[c] + a_dict[b]
    else:
        a_dict[c] = a_dict[b]
    a_dict[b] = 0
    print(a_sum)
# Original solution
n = int(eval(input()))
A = list(map(int, input().split()))
q = int(eval(input()))
B,C = [], []

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

d = {}
for a in A:
    d.setdefault(a, 0)
    d[a] += 1
for b, c in zip(B,C):
    d.setdefault(b, 0)
    d.setdefault(c, 0)
    d[c] += d[b]
    d[b] = 0
    ans = 0
    for i, v in list(d.items()):
        ans += i*v
    print(ans)


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

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

ans = sum(A)

d = {}
for a in A:
    d.setdefault(a, 0)
    d[a] += 1

for b, c in zip(B,C):
    d.setdefault(b, 0)
    d.setdefault(c, 0)
    ans -= d[c] * c
    ans -= d[b] * b
    d[c] += d[b]
    d[b] = 0
    ans += d[c] * c
    ans += d[b] * 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)]

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

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

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

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

sum_tmp = sum(a)
for i in range(q):
  b = bc[i][0]
  c = bc[i][1]
  A[c] += A[b]
  sum_tmp += (c-b) * A[b]
  A[b] = 0
  
  print(sum_tmp)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
dic = dict()

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

for i in range(Q):
    b, c = list(map(int, input().split()))
    if b in dic:
        if c in dic:    
            dic[c] += dic.pop(b)
        else:
            dic[c] = dic.pop(b)
    ans = 0
    for num in dic:
        ans += num * dic[num]
    print(ans)

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

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

ans = 0
for num in dic:
    ans += num * dic[num]

for i in range(Q):
    b, c = list(map(int, input().split()))
    x = 0
    if b in dic:
        x = dic.pop(b)
        if c in dic:    
            dic[c] += x
        else:
            dic[c] = x
    ans += x * (c-b)
    print(ans)