CodeEff/ECCO huggingface dataset:
# Original solution
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    S=ST()
    
    can = set([""])
    ans = set()
    for s in S:
        for c in deepcopy(can):
            res = c+s
            if len(res) == 3:
                ans.add(res)
            elif len(res) < 3:
                can.add(res)
    print(len(ans))

if __name__ == '__main__':
    main()


# Optimized solution
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def main():
    N=int(input())
    S=input()[:-1]
    
    can = set([""]) ## 2文字以下で現在構築可能な文字列(最大10+10*10 = 1010)
    ans = set() ## 3文字で現在構築可能な文字列(最大10*10*10 = 1,000)
    for s in S: 
        for c in can.copy(): #途中でcanという集合に追加されるのでcopyする
            res = c+s #2文字以下 + 1文字 で結合する
            if len(res) == 3: #3文字なので暗証番号の候補
                ans.add(res)
            elif len(res) < 3: #2文字以下なのでcanに挿入
                can.add(res)
    print(len(ans))

    """
    copyする回数 = N * len(can) <= 10^3 * N
    for文の走査回数 = N * len(can) <= 10^3 * N
    
    全体で  2 * (10^3 * N)
    N<=3*10^4なので
    2*3* 10^3 * 10^4 = 6 * 10^7 -> 間に合わない？んですか・・・？
    """

if __name__ == '__main__':
    main()
# Original solution
n = int(eval(input()))
s = list(map(int, input().rstrip()))

ans = 0
for i in range(1000):
    if i < 10:
        x, y, z = 0, 0, i
    elif i < 100:
        x, y, z = 0, i // 10, i % 10
    else:
        x = i // 100
        y = (i - x * 100) // 10
        z = i % 10
    flg = 0
    for a in range(len(s) - 2):
        if flg == 1:
            break
        elif x != s[a]:
            continue
        else:
            for b in range(a + 1, len(s) - 1):
                if flg == 1:
                    break
                elif y != s[b]:
                    continue
                else:
                    for c in range(b + 1, len(s)):
                        if z != s[c]:
                            continue
                        else:
                            ans += 1
                            flg = 1
                            break

print(ans)

# Optimized solution
n = int(eval(input()))
s = list(map(int, input().rstrip()))

ans = 0
for i in range(10):
    if i in s[:n - 2]:
        x = s.index(i)

        for j in range(10):
            if j in s[x + 1:n - 1]:
                y = x + s[x + 1:n - 1].index(j) + 1

                for k in range(10):
                    if k in s[y + 1:]:
                        ans += 1

print(ans)
# Original solution
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip.split())
def main():
    n = I()
    s = list(S())
    itr = itertools.combinations(s,3)
    st = set()
    for i in itr:
        st.add(i)
    print((len(st)))
main()

# Optimized solution
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip.split())
def main():
    sys.setrecursionlimit(10**5)
    n = I()
    s = S()
    global cnt
    cnt = 0
    def tansaku(st,depth):
        global cnt
        for i in range(10):
            index = st.find(str(i))
            if index != -1:
                if depth == 0:
                    cnt += 1
                    # print(i)
                # print(st,i,index,depth)
                else:
                    tansaku(st[index+1:],depth-1)
    tansaku(s,2)

    print(cnt)

main()
# Original solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N = int(readline())
S = readline().decode().rstrip()

ans = 0
for i in range(1000):
    i_ = str(i).zfill(3)
    idx = 0

    for s in S:
        if s == i_[idx]:
            idx += 1
        if idx == 3:
            ans += 1
            break

print(ans)


# Optimized solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N = int(readline())
S = readline().decode().rstrip()

ans = 0
for i in range(1000):
    num = str(i).zfill(3)
    idx = 0
    flag = True

    for s in num:
        acc = S[idx:].find(s)
        if acc == -1:
            flag = False
            break
        else:
            idx += acc + 1
    if flag:
        ans += 1

print(ans)
# Original solution
from sys import stdin
n = int(stdin.readline().rstrip())
s = stdin.readline().rstrip()
point = 0
for i in range(1000):
    bango = str(i).zfill(3)
    count = 0
    for j in range(n):
        if s[j] == bango[count]:
            count += 1
        if count == 3:
            point += 1
            break
print(point)

# Optimized solution
from sys import stdin
n = int(stdin.readline().rstrip())
s = stdin.readline().rstrip()

def count(poi):
    point = 0
    for i in range(poi):
        bango = str(i).zfill(3)
        count = 0
        for j in range(n):
            if s[j] == bango[count]:
                count += 1
            if count == 3:
                point += 1
                break
    return point

print((count(1000)))
# Original solution
from itertools import groupby
def main():
    n=int(eval(input()))
    aa=sorted(map(int,input().split()))
    d={}
    for k,g in groupby(aa):
        d[k] = len(list(g))
    q=int(eval(input()))
    bc=[]
    for _ in range(q):
        b,c=list(map(int,input().split()))
        bc.append((b,c))
    for b,c in bc:
        if b in d:
            d[c] = d.setdefault(c, 0) + d[b]
            del d[b]
        print((sum(k*v for k,v in list(d.items()))))
main()

# Optimized solution
from itertools import groupby
def main():
    n=int(eval(input()))
    aa=sorted(map(int,input().split()))
    d={}
    for k,g in groupby(aa):
        d[k] = len(list(g))
    q=int(eval(input()))
    bc=[]
    for _ in range(q):
        b,c=list(map(int,input().split()))
        bc.append((b,c))
    s = sum(k*v for k,v in list(d.items()))
    for b,c in bc:
        if b in d:
            s += (c-b)*d[b]
            d[c] = d.setdefault(c, 0) + d[b]
            del d[b]
        print(s)
main()
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))

dicts = {}
for tmp in a:
  if tmp in list(dicts.keys()):
    dicts[tmp] += 1
  else:
    dicts[tmp] = 1
    
m = int(eval(input()))
result = 0
for key, value in list(dicts.items()):
  result += key*value

for _ in range(m):
  b,c = list(map(int, input().split()))
  if b in list(dicts.keys()):
    tmp = dicts[b]
    del dicts[b]
    if c in list(dicts.keys()):
      dicts[c] += tmp
    else:
      dicts[c] = tmp
    result = 0
    for key, value in list(dicts.items()):
      result += key*value
    print(result)
  else:
    print(result)
  

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

dicts = {}
for tmp in a:
  if tmp in list(dicts.keys()):
    dicts[tmp] += 1
  else:
    dicts[tmp] = 1
    
m = int(eval(input()))
result = 0
for key, value in list(dicts.items()):
  result += key*value

for _ in range(m):
  b,c = list(map(int, input().split()))
  if b in list(dicts.keys()):
    tmp = dicts[b]
    diff = dicts[b]*b
    add = tmp*c
    
    del dicts[b]
    if c in list(dicts.keys()):
      dicts[c] += tmp
      
    else:
      dicts[c] = tmp
      
    result -= diff
    result += add
    print(result)
  else:
    print(result)
# Original solution
from collections import defaultdict

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


dict_n = defaultdict(lambda:0)

for i in alist:
    dict_n[i] += 1

bclist = []
for i in range(q):
    b,c  = list(map(int,input().split()))
    bclist.append([b,c])


for i in range(q):
    
    b = bclist[i][0]
    c = bclist[i][1]

    dict_n[c] += dict_n[b]
    del(dict_n[b])
    
    sum = 0
    for key in dict_n:
        sum += key * dict_n[key]
    print(sum)

# Optimized solution
from collections import defaultdict

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


dict_n = defaultdict(lambda:0)

for i in alist:
    dict_n[i] += 1

bclist = []
for i in range(q):
    b,c  = list(map(int,input().split()))
    bclist.append([b,c])

sum = sum(alist)
for i in range(q):
    
    b = bclist[i][0]
    c = bclist[i][1]

    sum += dict_n[b]*c - dict_n[b]*b
    dict_n[c] += dict_n[b]
    del(dict_n[b])

    print(sum)
# Original solution
n = int(input().rstrip())

line = input().rstrip().split(' ')

a = []
for i in range(n):
    a.append(int(line[i]))

a.sort()

q = int(input().rstrip())

b = []
c = []

for i in range(q):
    line = input().rstrip().split(' ')
    b = int(line[0])
    c = int(line[1])

    for j in range(len(a)):
        if a[j] == b:
            a[j] = c

    ans = 0
    for j in range(len(a)):
        ans += a[j]

    print(ans)


# Optimized solution
n = int(input().rstrip())

line = input().rstrip().split(' ')

a = {}
ans = 0
for i in range(n):
    n = int(line[i])
    ans += n
    if n in a:
        a[n] += 1
    else:
        a[n] = 1

q = int(input().rstrip())

for i in range(q):
    line = input().rstrip().split(' ')
    b = int(line[0])
    c = int(line[1])

    if b in a:
        ab = a[b]
        a[b] = 0
        if c in a:
            ac = a[c]
            a[c] += ab
        else:
            a[c] = ab
    else:
        ab = 0

    ans -= ab * b
    ans += ab * c

    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)]
# print(N)
# print(A)
# print(Q)
# print(B)

total = sum(A)

for i in range(Q):
    Bi = BC[i][0]
    Ci = BC[i][1]

    diff = 0
    for j in range(len(A)):
        if A[j] == Bi:
            diff += Ci - A[j]
            A[j] = Ci

    total += diff
    print(total)


# Optimized solution
import itertools

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(N)
# print(A)
# print(Q)
# print(B)

distribution = {}
max_A = max(A)
max_BC = max(list(itertools.chain.from_iterable(BC)))
maximum = max([max_A, max_BC])
# print(max_A, max_BC, maximum)
for i in range(maximum + 1):
    distribution[i] = 0

for Ai in A:
    distribution[Ai] += 1
# print(distribution)

total = sum(A)
for i in range(Q):
    Bi = BC[i][0]
    Ci = BC[i][1]
    num_Bi = distribution.get(Bi)
    diff = (Ci - Bi) * num_Bi

    distribution[Bi] = 0
    # print(num_Bi, distribution.get(Bi), Ci)
    distribution[Ci] += num_Bi

    total += diff
    print(total)
# Original solution
from collections import Counter

R = lambda: list(map(int, input().split()))
R()
d = Counter(R())
s = sum(k * v for k,v in list(d.items()))
q, = R()
for _ in range(q):
  b, c = R()
  s += (c - b) * d[b]
  print(s)
  d[c]+=d[b]
  d[b] = 0


# Optimized solution
from collections import Counter

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

R = lambda: list(map(int, input().split()))
R()
d = Counter(R())
s = sum(d.elements())
q, = R()
for _ in range(q):
  b, c = R()
  s += (c - b) * d[b]
  print(s)
  d[c]+=d[b]
  d[b] = 0


# Optimized solution
R = lambda: list(map(int, input().split()))
R()
a = [0] * 100001
s = 0
for x in R():
  s += x
  a[x] += 1
q, = R()
for _ in range(q):
  b, c = R()
  s += (c - b) * a[b]
  print(s)
  a[c]+=a[b]
  a[b] = 0
# Original solution
import collections
N = int(eval(input()))

Line = [int(s) for s in input().split()]
c = collections.Counter(Line)

# print(c)

Q = int(eval(input()))


for _ in range(Q):
    Line = [int(s) for s in input().split()]
    try:
        c[Line[1]] += c[Line[0]]
        c.pop(Line[0])
    except:
        pass
        
        # print('Error')
    # print(c)
    
    s=0
    for i in c:
        # print(i)
        s += i * c[i]
    print(s)    


# Optimized solution
import collections
N = int(eval(input()))

Line = [int(s) for s in input().split()]
c = collections.Counter(Line)

# print(c)
v = 0

for i in c:
    # print(i)
    v += i * c[i]
# print(v)    

Q = int(eval(input()))


for _ in range(Q):
    Line = [int(s) for s in input().split()]
    try:
        c[Line[1]] += c[Line[0]]
        
        # print(c[Line[1]],c[Line[0]])
        v += c[Line[0]] * (Line[1] - Line[0])
        c.pop(Line[0])
        # v += c[Line[0]] * (C[Line[1]-C[Line[0]]])
    except:
        pass
        
        # print('Error')
    # print(c)

    # s=0
    # for i in c:
    #     # print(i)
    #     s += i * c[i]
    print(v)
# Original solution
from collections import Counter

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

d = Counter(As)

Q = int(eval(input()))
for _ in range(Q):
    B, C = list(map(int, input().split()))
    if B in d:
        d[C] += d[B]
        d[B] = 0
    
    print((sum(k*v for k, v in list(d.items()))))

# Optimized solution
from collections import Counter

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

d = Counter(As)
S = sum(k*v for k, v in list(d.items()))
Q = int(eval(input()))
for _ in range(Q):
    B, C = list(map(int, input().split()))
    if B in d:
        S += d[B] * (C-B)
        d[C] += d[B]
        d[B] = 0
    print(S)
# Original solution
N = int(eval(input()))
numbers = list(map(int,input().split()))
numOfOperation = int(eval(input()))
sums = sum(numbers)
for i in range(numOfOperation):
    B,C = list(map(int,input().split()))
    cnt = 0
    for j in range(len(numbers)):
        if numbers[j] == B : 
            cnt += 1
            numbers[j] = C
    sums += cnt * (C - B)
    print(sums)


# Optimized solution
N = int(eval(input()))
numbers = list(map(int,input().split()))
numOfOperation = int(eval(input()))
dic = {}

for num in numbers:
    if num in dic : dic[num] += 1
    else: dic[num] = 1
    

sums = sum(numbers)
for i in range(numOfOperation):
    B,C = list(map(int,input().split()))
    if (B in dic) : 
        cnt = dic[B]
        dic[B] = 0
        if (C not in dic) : dic[C] = cnt
        else : dic[C] += cnt
        sums += cnt * (C - B)
    print(sums)
# Original solution
n=int(eval(input()))
ar=list(map(int,input().split()))
di={}
for i in range(n):
    if ar[i] not in di:
        di[ar[i]]=1
    else:
        di[ar[i]]+=1
q=int(eval(input()))
for i in range(q):
    b,c=list(map(int,input().split()))
    if(b in di):
        x=di[b]
        del di[b]
        if(c in di):
            di[c]+=x
        else:
            di[c]=x
    s=0
    for j in list(di.keys()):
        s+=j*(di[j])
    print(s)

        
        
    

# Optimized solution
n=int(eval(input()))
ar=list(map(int,input().split()))
di={}
for i in ar:
    di[i]=di.get(i,0)+1
sm=sum(ar)
q=int(eval(input()))
for i in range(q):
    b,c=list(map(int,input().split()))
    if(b not in di):
        print(sm)
    elif(b in di):
        x=di[b]
        del di[b]
        if(c in di):
            di[c]+=x
        else:
            di[c]=x
        s=0
        s=sm-(b*x)+(x*c)
        sm=s
        print(s)
# Original solution
def main():
    n = int(eval(input()))

    As = list(map(int, input().split()))
    nums = [0] * (10**5+1)
    ans = 0
    for a in As:
        ans += a
        nums[a] += 1

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


if __name__ == "__main__":
    main()


# Optimized solution
def main():
    n = int(eval(input()))
    As = list(map(int, input().split()))
    As_Count = [0] * (10**5+1)
    q = int(eval(input()))
    bcs = [list(map(int, input().split())) for _ in range(q)]
    for A in As:
        As_Count[A] += 1
    ans = sum(As)
    for b, c in bcs:
        ans -= As_Count[b] * b
        ans += c * As_Count[b]
        As_Count[c] += As_Count[b]
        As_Count[b] = 0
        print(ans)


if __name__ == "__main__":
    main()
# Original solution
from collections import Counter


def solve(string):
    n, *aqbc = list(map(int, string.split()))
    a, _, bc = aqbc[:n], aqbc[n], aqbc[n + 1:]
    s = sum(a)
    t = Counter(a)
    ans = []
    for b, c in zip(*[iter(bc)] * 2):
        if c in list(t.keys()):
            t[c] += t[b]
        else:
            t[c] = t[b]
        s += (c - b) * t[b]
        del t[b]
        ans.append(s)
    return "\n".join(map(str, ans))


if __name__ == '__main__':
    import sys
    print((solve(sys.stdin.read().strip())))


# Optimized solution
from collections import Counter


def solve(string):
    n, *aqbc = list(map(int, string.split()))
    a, _, bc = aqbc[:n], aqbc[n], aqbc[n + 1:]
    s = sum(a)
    t = Counter(a)
    ans = []
    for b, c in zip(*[iter(bc)] * 2):
        s += (c - b) * t[b]
        t[c] += t[b]
        t[b] = 0
        ans.append(s)
    return "\n".join(map(str, ans))


if __name__ == '__main__':
    import sys
    print((solve(sys.stdin.read().strip())))
# Original solution
from collections import Counter


def solve(string):
    n, *aqbc = list(map(int, string.split()))
    a, _, bc = aqbc[:n], aqbc[n], aqbc[n + 1:]
    s = sum(a)
    t = Counter(a)
    ans = []
    for b, c in zip(*[iter(bc)] * 2):
        s += (c - b) * t[b]
        t[c] += t[b]
        t[b] = 0
        ans.append(s)
    return "\n".join(map(str, ans))


if __name__ == '__main__':
    import sys
    print((solve(sys.stdin.read().strip())))


# Optimized solution
from collections import Counter


def solve(string):
    n, *aqbc = list(map(int, string.split()))
    a, _, bc = aqbc[:n], aqbc[n], aqbc[n + 1:]
    s = sum(a)
    t = Counter(a)
    ans = []
    for b, c in zip(*[iter(bc)] * 2):
        if b not in list(t.keys()):
            ans.append(s)
            continue
        s += (c - b) * t[b]
        t[c] += t[b]
        t[b] = 0
        ans.append(s)
    return "\n".join(map(str, ans))


if __name__ == '__main__':
    import sys
    print((solve(sys.stdin.read().strip())))
# Original solution
from collections import Counter


def solve(string):
    n, *aqbc = list(map(int, string.split()))
    a, _, bc = aqbc[:n], aqbc[n], aqbc[n + 1:]
    s = sum(a)
    t = Counter(a)
    ans = []
    for b, c in zip(*[iter(bc)] * 2):
        if b not in list(t.keys()):
            ans.append(s)
            continue
        s += (c - b) * t[b]
        t[c] += t[b]
        t[b] = 0
        ans.append(s)
    return "\n".join(map(str, ans))


if __name__ == '__main__':
    import sys
    print((solve(sys.stdin.read().strip())))


# Optimized solution
from collections import Counter


def solve(string):
    n, *aqbc = list(map(int, string.split()))
    a, _, bc = aqbc[:n], aqbc[n], aqbc[n + 1:]
    s = sum(a)
    t = Counter(a)
    ans = []
    for b, c in zip(*[iter(bc)] * 2):
        if b in list(t.keys()):
            s += (c - b) * t[b]
            t[b], t[c] = 0, t[b] + t[c]
        ans.append(s)
    return "\n".join(map(str, ans))


if __name__ == '__main__':
    import sys
    print((solve(sys.stdin.read().strip())))
# Original solution
from collections import Counter

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

ctr = Counter(A)
ans = sum(A)
for b, c in X:
    v = ctr[b]
    ans = ans - b * v + c * v
    ctr[b] -= v
    ctr[c] += v
    print(ans)


# Optimized solution

from collections import Counter

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

ctr = Counter(X)
ans = sum(X)
for b, c in Y:
    n = ctr[b]
    ctr[b] = 0
    ctr[c] += n
    ans = ans - b * n + c * n
    print(ans)
# Original solution
n = int(eval(input()))
a = list(map(int,input().split()))
q = int(eval(input()))
a_list = [[i, 0] for i in range(10**5 +1)]

for elem in a:
    a_list[elem][1] += 1

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

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

for elem in a:
    if elem not in a_dic:
        a_dic[elem] = 1
    else:
        a_dic[elem] += 1

a_sum = sum(a)

for i in range(q):
    b, c = list(map(int,input().split()))
    if b in a_dic:
        diff = c - b
        a_sum += diff * a_dic[b]
        print(a_sum)
        if c in a_dic:
            a_dic[c] += a_dic[b]
            del a_dic[b]
        else:
            a_dic[c] = a_dic[b]
            del a_dic[b]
    else:
        print(a_sum)
# Original solution
import sys
sys.setrecursionlimit(1000000000)
input = sys.stdin.readline

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

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


for _ in range(q):
    b,c=list(map(int,input().split()))
    if x[b-1]!=0:
        x[c-1]+=x[b-1]//b*c
        x[b-1]=0
        print((sum(x)))
    else:
        print((sum(x)))

# Optimized solution
import sys
sys.setrecursionlimit(1000000000)
input = sys.stdin.readline

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

ans=sum(a)

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


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

N = int(eval(input()))

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

Q = int(eval(input()))

BC = []
for _ in range(Q):
    b, c = list(map(int, input().split()))
    BC.append([b, c])

C = Counter(A)
for bc in BC:
    b = bc[0]
    c = bc[1]
    if b in C:
        if c in C:
            C[c] += C[b]
            C[b] = 0
        else:
            C[c] = C[b]
            C[b] = 0
    print((sum([k*v for k, v in list(C.items())])))


# Optimized solution
from collections import Counter

N = int(eval(input()))

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

Q = int(eval(input()))

BC = []
for _ in range(Q):
    b, c = list(map(int, input().split()))
    BC.append([b, c])

C = Counter(A)
S = sum([k*v for k, v in list(C.items())])
for bc in BC:
    b = bc[0]
    c = bc[1]
    if b in C:
        bf = C[b]
        S += (c-b) * bf
        if c in C:
            C[c] += C[b]
            C[b] = 0
        else:
            C[c] = C[b]
            C[b] = 0
    print(S)