Problem:
 Score : 400 points    Problem Statement You have a sequence A composed of N positive integers: A_{1}, A_{2}, \cdots, A_{N} .  You will now successively do the following Q operations:   In the i -th operation, you replace every element whose value is B_{i} with C_{i} .   For each i  (1 \leq i \leq Q) , find S_{i} : the sum of all elements in A just after the i -th operation.      Constraints  All values in input are integers.  1 \leq N, Q, A_{i}, B_{i}, C_{i} \leq 10^{5}  B_{i} \neq C_{i}         Input Input is given from Standard Input in the following format:  N  A_{1}  A_{2}  \cdots  A_{N}  Q  B_{1}  C_{1}  B_{2}  C_{2}  \vdots  B_{Q}  C_{Q}       Output Print Q integers S_{i} to Standard Output in the following format:  S_{1}  S_{2}  \vdots  S_{Q}   Note that S_{i} may not fit into a 32 -bit integer.        Sample Input 1 4
1 2 3 4
3
1 2
3 4
2 4      Sample Output 1 11
12
16  Initially, the sequence A is 1,2,3,4 .  After each operation, it becomes the following:   2, 2, 3, 4  2, 2, 4, 4  4, 4, 4, 4        Sample Input 2 4
1 1 1 1
3
1 2
2 1
3 5      Sample Output 2 8
4
4  Note that the sequence A may not contain an element whose value is B_{i} .       Sample Input 3 2
1 2
3
1 100
2 100
100 1000      Sample Output 3 102
200
2000

Solution (Python):
from collections import Counter

N = int(input())
arr = list(map(int, input().split()))
summ = sum(arr)
val = Counter(arr)
Q = int(input())
for q in range(Q):
    B, C = list(map(int, input().split()))
    summ = summ + C * val[B] - B * val[B]
    val[C] += val[B]
    val[B] = 0
    print(summ)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
q = int(input())

m = 10**5+1
nums = [0]*m
ans = 0
for i in a:
  nums[i] += 1
  ans += i
  
for _ in range(q):
  b,c = map(int, input().split())
  ans -= b*nums[b]
  ans += c*nums[b]
  nums[c] += nums[b]
  nums[b] = 0
  print(ans)

Solution (Python):
from sys import stdin
input=stdin.readline
input()
a=list(map(int,input().split()))
ans=sum(a)
b=[0]*(10**5+1)
for i in a:
  b[i]+=1
c=int(input())
for i in range(c):
  n,m=map(int,input().split())
  if b[n]>0:
    x=b[n]
    if x>0:
      b[m]+=x
      b[n]=0
      ans+=(m-n)*x
  print(ans)

Solution (Python):


n=int(input())
a=list(map(int,input().split()))
w=int(input())
c=[]
for i in range(w):
    c.append(list(map(int,input().split())))

b=[0]*10**5
for i in range(n):
    b[a[i]-1]+=1
ans=sum(a)
for i in range(w):
    ans=ans+b[c[i][0]-1]*(c[i][1]-c[i][0])
    cnt=b[c[i][0]-1]
    b[c[i][1]-1]+=cnt
    b[c[i][0]-1]=0
    
    print(ans)

Solution (Python):
from collections import Counter
def solve():
  N = int(input())
  A = list(map(int, input().split()))
  c = Counter(A)
  Q = int(input())
  ans = [0]*Q
  total = sum(A)
  for i in range(Q):
    x,y = map(int, input().split())
    total += c[x]*(y-x)
    c[y] += c[x]
    c[x] = 0
    ans[i] = total
  return ans
print(*solve(),sep='\n')

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
tot = sum(A)
D = {}
for i in range(N):
    a = A[i]
    if a not in D:
        D[a] = 0
    D[a] += 1
Q = int(input())
for _ in range(Q):
    b,c = map(int,input().split())
    if b not in D:
        print(tot)
    else:
        d = c-b
        if c in D:
            n = D[b]
            D[c] += D[b]
            del D[b]
        else:
            n = D[b]
            D[c] = D[b]
            del D[b]
        tot += n*d
        print(tot)

Solution (Python):
n = int(input())
al = list(map(int, input().split()))

num_cnt = {}
c_sum = 0
for a in al:
    num_cnt.setdefault(a,0)
    num_cnt[a] += 1
    c_sum += a

ans = []
q = int(input())

for _ in range(q):
    b,c = map(int, input().split())
    num_cnt.setdefault(b,0)
    num_cnt.setdefault(c,0)
    diff = num_cnt[b]*c - num_cnt[b]*b

    num_cnt[c] += num_cnt[b]
    num_cnt[b] = 0
    c_sum += diff
    ans.append(c_sum)

for a in ans:
    print(a)

Solution (Python):
from collections import Counter
n = int(input())
A = list(map(int, input().split()))
a = Counter(A)
q = int(input())
bc = [list(map(int, input().split())) for _ in range(q)]

ans = sum(A)
for b,c in bc:
    ans -= b*a[b]
    ans += c*a[b]
    if b in a:
        if c in a: a[c] += a[b]
        else: a[c] = a[b]
        a.pop(b)
    print(ans)

Solution (Python):
import sys
input = sys.stdin.buffer.readline

n = int(input())
A = list(map(int, input().split()))
S = sum(A)

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

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

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
BC = [tuple(map(int,input().split())) for i in range(Q)]
from collections import Counter
ctr = Counter(A)
sm = sum(A)

ans = []
for b,c in BC:
    n = ctr[b]
    sm -= n * b
    sm += n * c
    ctr[b] -= n
    ctr[c] += n
    ans.append(sm)
print(*ans, sep='\n')

Solution (Python):



from fractions import gcd
from itertools import combinations,permutations,accumulate 

from collections import deque,defaultdict,Counter
import decimal
import re












import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
A = readInts()
q = I()
sumA = sum(A)
C = Counter(A)
for i in range(q):
    b,c = readInts()
    sumA -= b * C[b]
    sumA += c * C[b]
    C[c] += C[b]
    C[b] = 0
    print(sumA)

Solution (Python):
n=int(input())
a = list(map(int,input().split()))
q=int(input())
bc = [list(map(int, input().split())) for l in range(q)]
cnt=[0]*100001
Sum=0
for i in range(n):
  cnt[a[i]]+=1
  Sum+=a[i]
for i in range(q):
  Sum+=(bc[i][1]-bc[i][0])*cnt[bc[i][0]]                           
  cnt[bc[i][1]]+=cnt[bc[i][0]]
  cnt[bc[i][0]]=0
  print(Sum)

Solution (Python):
import sys
import numpy as np

input = sys.stdin.readline

def minus(a):
    return int(a)-1

M = 10**5
N = int(input())
A = list(map(minus, input().split()))
Q = int(input())


def solve():
    hist = list(np.histogram(A, M, (0, M))[0].astype(int))
    cur = sum(A) + N
    for _ in range(Q):
        b, c = map(minus, input().split())
        dh = hist[b]
        cur += dh*(c-b)
        hist[b] -= dh
        hist[c] += dh
        print(cur)

if __name__ == "__main__":
    solve()

Solution (Python):
ini = lambda : int(input())
inm = lambda : map(int,input().split())
inl = lambda : list(map(int,input().split()))
gcd = lambda x,y : gcd(y,x%y) if x%y else y
nom = [0] * (10**5+1)
n = ini()
a = inl()
ans = sum(a)
for i in a:
        nom[i] += 1
q = ini()
for i in range(q):
        b,c = inm()
        d = c-b
        ans += nom[b] * d
        print(ans)
        nom[c] += nom[b]
        nom[b] = 0

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B, C = [], []
for i in range(Q):
    b, c = map(int, input().split())
    B.append(b)
    C.append(c)
data = {}
SUM = 0
for i in range(N):
    SUM += A[i]
    if A[i] in data:
        data[A[i]] += 1
    else:
        data[A[i]] = 1
for i in range(Q):
    if B[i] in data:
        SUM += data[B[i]]*(C[i]-B[i])
        if C[i] in data:
            data[C[i]] += data[B[i]]
        else:
            data[C[i]] = data[B[i]]
        data[B[i]] = 0
    print(SUM)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B, C = [], []
for i in range(Q):
    b, c = map(int, input().split())
    B.append(b)
    C.append(c)
data = {}
SUM = sum(A)
for i in range(N):
    if A[i] in data:
        data[A[i]] += 1
    else:
        data[A[i]] = 1
for i in range(Q):
    if B[i] in data:
        SUM += data[B[i]]*(C[i]-B[i])
        if C[i] in data:
            data[C[i]] += data[B[i]]
        else:
            data[C[i]] = data[B[i]]
        data[B[i]] = 0
    print(SUM)

Solution (Python):
import collections

N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())
B = [0] * Q
C = [0] * Q
for i in range(Q):
  B[i], C[i] = [int(x) for x in input().split()]

count = collections.defaultdict(lambda: 0)
current = 0
for a in A:
  count[a] += 1
  current += a

for b, c in zip(B,C):
  current += (c - b) * count[b]
  print(current)
  count[c] += count[b]
  del count[b]

Solution (Python):
import math
import sys
import collections
import bisect
readline = sys.stdin.readline


def main():
    n = int(readline().rstrip())
    A = list(map(int, readline().rstrip().split()))
    counter = collections.Counter(A)
    q = int(readline().rstrip())
    BCList = [list(map(int, readline().rstrip().split())) for _ in range(q)]
    aList = [0] * (10**5 + 10)
    sumA = sum(A)
    for i in range(n):
        aList[A[i] - 1] += 1

    for i in range(q):
        b, c = BCList[i][0], BCList[i][1]
        if aList[b - 1] == 0:
            print(sumA)
            continue
        sumA += (c - b) * aList[b - 1]
        aList[c - 1] += aList[b - 1]
        aList[b - 1] = 0
        print(sumA)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
lst = [int(i) for i in input().split()]
dic = {}
for i in range(n):
  key = lst[i]
  if key not in dic:
    dic[key] = 0
  dic[key] += 1

sum = 0
for key, value in dic.items():
  sum += key * value

m = int(input())
for _ in range(m):
  lst_s = list(map(int, input().split()))
  number = dic.get(lst_s[0])
  if number is not None:
    if lst_s[1] in dic:
      dic[lst_s[1]] += number
    else:
      dic[lst_s[1]] = number
    sum += lst_s[1] * number
    sum -= lst_s[0] * number
    dic.pop(lst_s[0])
  print(sum)

Solution (Python):
from collections import Counter
N = int(input())

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

counter = Counter(A)
sum_res = sum(A)

for _ in range(Q):
    bf,af = map(int,input().split())
    sum_res -= bf*counter[bf]
    sum_res += af*counter[bf]
    
    counter[af] += counter[bf]
    counter[bf] = 0
    
    print(sum_res)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
A = sorted(A)
q = int(input())
lst = [0] * q
for i in range(q):
    lst[i] = list(map(int, input().split()))
s = sum(A)
num = [0] * 10**5
for i in A:
    num[i-1] += 1
for i in range(q):
    s += num[lst[i][0]-1] * (lst[i][1] - lst[i][0])
    num[lst[i][1]-1] += num[lst[i][0]-1]
    num[lst[i][0]-1] = 0
    print(s)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
dic = {}
ans = 0
for  x in a:
    if  x not in dic:
        dic[x] = 1
    else:
        dic[x]+=1
    ans += x

for _ in range(q):
    b,c=map(int,input().split())
    if b not in dic:
        print(ans)
        continue
    ans += (c-b)*dic[b]
    if c not in dic:
        dic[c] = dic[b]
    else:
        dic[c]+=dic[b]
    dic[b]=0
    print(ans)

Solution (Python):
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    BC = [list(map(int, input().split())) for _ in range(Q)]

    a = [0] * 100001
    for i in A:
        a[i] += i

    ans = sum(a)
    for b, c in BC:
        if a[b] == 0:
            print(ans)
            continue
        move = a[b] // b
        a[b] = 0
        a[c] += c * move
        ans += (c - b) * move
        print(ans)

if __name__ == "__main__":
    resolve()

Solution (Python):
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque

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

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

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

q = int(input())

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

    if b in d:
        m += (c - b)*d[b]

        if c not in d:
            d[c] = d[b]
        else:
            d[c] += d[b]

        d[b] = 0

    print(m)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
freq = [0]*(10**5+1)

for a in A:
    freq[a] += 1
q = int(input())
ans = sum(A)
for i in range(q):
    b, c = map(int, input().split())
    ans -= freq[b] * b
    freq[c] += freq[b]
    ans += freq[b] * c
    freq[b] = 0
    print (ans)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
q = int(input())

num_sum = sum(A)
B = [0]*(10**5+1)

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


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

Solution (Python):
import collections
n = int(input())
a = list(map(int,input().split()))
q = int(input())
l = [list(map(int,input().split())) for i in range(q)]
b,c = [list(i) for i in zip(*l)]
x = collections.Counter(a)
y = sum(a)
for i in range(q):
    if b[i] in x:
        y -= x[b[i]] * b[i]
        y += x[b[i]] * c[i]
        x[c[i]] += x[b[i]]
        x[b[i]] = 0
    print(y)

Solution (Python):
n = int(input())
arr = [int(x) for x in input().split()]
q = int(input())

dictA = {}
for i in arr:
    if dictA.get(i): dictA[i] += 1
    else: dictA[i] = 1

summ = sum(arr)

for i in range(q):
    b, c = list(map(int, input().split()))
    if dictA.get(b):
        summ = summ - (dictA[b]*b) + (dictA[b]*c)
        if dictA.get(c): dictA[c] += dictA[b]
        else: dictA[c] = dictA[b]
        del dictA[b]
    print(summ)

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

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

AllSum=sum(A)

for a in A:
  NumList[a]+=1

for i in range(Q):
  B,C=map(int,input().split())
  AllSum=AllSum + (C-B)*NumList[B]
  NumList[C]+=NumList[B]
  NumList[B]=0
  
  print(AllSum)

Solution (Python):

from collections import Counter

N = int(input())
X = list(map(int, input().split()))
Q = int(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)

Solution (Python):

def sum_dict(d):
    num = 0
    for k, v in d.items():
        num += k * v
    return num


def create_dict(A):
    d = {}
    for a in A:
        if d.get(a) is None:
            d[a] = 1
        else:
            d[a] += 1
    return d


if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    BC = []
    for i in range(Q):
        BC.append(tuple(map(int, input().split())))

    d = create_dict(A)

    ans = sum_dict(d)
    for b, c in BC:
        db = d.get(b)
        if db is None:
            db = 0
        if d.get(c) is None:
            d[c] = 0
        d[c] += db
        d[b] = 0

        ans += (c - b) * db
        print(ans)

Solution (Python):
N=int(input())
array=[int(x) for x in input().split()]
dict={}
for i in array:
	if i in dict:
		dict[i]+=1
	else:
		dict[i]=1
total=sum(array)
for i in range(int(input())):
	A,B=[int(x) for x in input().split()]
	if A not in dict:
		dict[A]=0
	if B not in dict:
		dict[B]=0
	total=total-A*dict[A]+B*(dict[A])
	dict[B]=dict[B]+dict[A]
	dict[A]=0
	print(total)

Solution (Python):
from collections import defaultdict

N = int(input())
A = [int(s) for s in input().split()]
Q = int(input())
B = []
C = []
for _ in range(Q):
    b, c = [int(s) for s in input().split()]
    B.append(b)
    C.append(c)

A_dict = defaultdict(int)
for a in A:
    A_dict[a] += 1

A_sum = sum(A)
for b, c in zip(B, C):
    n = A_dict[b]
    A_dict[b] = 0
    A_dict[c] += n
    A_sum += (c - b) * n
    print(A_sum)

Solution (Python):
n = int(input())
A = [0] * (10**5+1)

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

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

m = int(input())

sum_a = sum(B)

for _ in range(m):
    a, b = map(int, input().split())
    sum_a = sum_a + (b - a) * A[a]
    A[b] += A[a]
    A[a] = 0
    print(sum_a)

Solution (Python):
def main():
    N=int(input())
    A=list(map(int,input().split()))
    Q=int(input())
    B=[]
    C=[]

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

    maxA=max(A)
    maxC=max(C)
    if maxA>=maxC:
        m=maxA
    else:
        m=maxC
    dp=[0 for i in range(m+1)]

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

    res=sum(A)


    for _ in range(Q):
        i=B[_]
        j=C[_]

        res-=dp[i]*i
        dp[j] += dp[i]
        res+=j*dp[i]
        dp[i]=0
        print(res)

if __name__=="__main__":
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
List = [0 for _ in range(10**5)]
for x in A:
  List[x-1] += 1
ans = sum(A)
for i in range(Q):
  B, C = map(int, input().split())
  if List[B-1] != 0:
    ans += (C-B)*List[B-1]
    List[C-1] += List[B-1]
    List[B-1] = 0
  print(ans)

Solution (Python):

from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


def solve():
    N = int(input())
    A = list(map(int, input().split()))

    ans = 0
    d = defaultdict(int)
    for a in A:
        d[a] += 1
        ans += a

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

        if d[B] != 0:
            ans -= B * d[B]
            ans += C * d[B]

        d[C] += d[B]
        d[B] = 0

        print(ans)


def main():
    solve()


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    n = int(input())
    As = list(map(int, input().split()))
    As_Count = [0] * (10**5+1)
    q = int(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()

Solution (Python):
import collections
N=int(input())
A=list(input().split())
a=collections.Counter(A)
A=[int(i) for i in A]
S=sum(A)
Q=int(input())
for i in range(Q):
    B,C=input().split()
    if B in a:
        b=a[B]
        S-=int(B)*b
        S+=int(C)*b
        del a[B]
        if C in a:
            a[C]+=b
        else:
            a[C]=b
    print(S)

Solution (Python):
def resolve():
    n = int(input())
    a = list(map(int, input().split()))

    q = int(input())
    num_table = [0] * 100010

    for ai in a:
        num_table[ai] += 1

    ans = sum(a)

    for _ in range(q):
        bi, ci = map(int, input().split())
        ans += num_table[bi] * (ci - bi)
        num_table[ci] += num_table[bi]
        num_table[bi] = 0
        print(ans)
        
resolve()

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
B = []
C = []
for i in range(Q):
    b,c = map(int,input().split())
    B.append(b)
    C.append(c)
S = sum(A)
NUM = [0] * 100005
for a in A:
    NUM[a] += 1
for k in range(Q):
    S += (C[k]-B[k]) * (NUM[B[k]])
    NUM[C[k]] += NUM[B[k]]
    NUM[B[k]] = 0
    print(S)

Solution (Python):
def main():
    N = int(input().strip())
    A = [int(_a) for _a in input().strip().split()]
    Q = int(input().strip())
    BCS = []
    for i in range(Q):
        BCS.append([int(_bc) for _bc in input().strip().split()])

    Amap = {}
    for a in A:
        if a not in Amap:
            Amap[a] = 0
        Amap[a] += 1

    total = 0
    for k,v in Amap.items():
        total += (k*v)

    for bc in BCS:
        b = bc[0]
        c = bc[1]
        if b in Amap:
            num = Amap[b]

            del Amap[b]
            if c not in Amap:
                Amap[c] = 0
            Amap[c] += num

            total = total + num*c - num*b

        print(total)

main()

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum1=sum(a)
d={}
for x in range(n):
  s=a[x]
  if s not in d:
    d[s]=0
  d[s]+=1

for y in range(q):
  h,g=map(int,input().split())
  if h in d:
    r=d[h]
    sum1=sum1-r*h
    
    if g not in d:
      d[g]=0
    d[g]+=r
    sum1+=r*g
    d[h]=0
    
  print(sum1)

Solution (Python):
from collections import Counter

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

ans = sum(a)
a_c = Counter(a)

for i in range(q):
    b_num = a_c[b[i]]
    ans += (c[i] - b[i]) * b_num
    a_c[c[i]] += b_num
    a_c[b[i]] = 0
    print(ans)

Solution (Python):
n = int(input())
ls = [0 for i in range(10 ** 5)]
lsa = list(map(int,input().split()))

for i in range(len(lsa)):
    ls[lsa[i] - 1] += 1

ans1 = sum(lsa)
ansls = []

q = int(input())

for i in range(q):
    b, c = map(int,input().split())
    if i == 0:
        ans = ans1 + ((c - b) * ls[b - 1])
    else:
        ans = ansls[-1] + ((c - b) * ls[b - 1])
    ansls.append(ans)
    ls[c - 1] += ls[b - 1]
    ls[b - 1] = 0

for i in range(q):
    print(ansls[i])

Solution (Python):

def solve():
    n=int(input())
    a=list(map(int,input().split()))
    cnt=[0]*(int(1e5)+9)
    s=0
    for i in range(0,n) :
        s+=a[i]
        cnt[a[i]]+=1
    q=int(input())
    for q in range(0,q) :
        b,c=map(int,input().split())
        ct=cnt[b]
        s+=ct*(c-b)
        cnt[b]=0
        cnt[c]+=ct
        print(s)

solve()

Solution (Python):
from collections import Counter
n = int(input())
A = list(map(int, input().split()))
q = int(input())
dict_A = Counter(A)
sum_list = sum(A)
for i in range(q):
    b, c = map(int, input().split())
    if b not in dict_A:
        print(sum_list)
    else:
        b_num = dict_A[b]
        dict_A[c] += dict_A[b]
        dict_A[b] = 0
        sum_list = sum_list + (c - b) * b_num
        print(sum_list)

Solution (Python):
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
ac = Counter(a)

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

Solution (Python):
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
ac = Counter(a)

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

Solution (Python):
import collections
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
query = []
for _ in range(Q):
    query.append(tuple(map(int, input().split())))

dict_A = collections.Counter(A)

ans = sum(A)
for i in range(Q):
    if query[i][0] in dict_A.keys():
        val = dict_A.pop(query[i][0])
        if query[i][1] in dict_A.keys():
            dict_A[query[i][1]] += val
        else:
            dict_A[query[i][1]] = val
        ans = ans - (val*query[i][0]) + (val*query[i][1])
    print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))

nums = [0 for i in range(1,100010)]

S = sum(A)
for i in range(N):
    nums[A[i]] += 1

Q = int(input())
for i in range(Q):
    B,C = map(int,input().split())
    if nums[B] != 0:
        plus = nums[B]*(C - B)
        S += plus
    print(S)
    nums[C] += nums[B]
    nums[B] = 0

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]
q = int(input())
bc=[]
for i in range(q):
    bc.append([int(x) for x in input().split()])

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

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

a_goukei=sum(a)

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

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]
q = int(input())
bc=[]
for i in range(q):
    bc.append([int(x) for x in input().split()])

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

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

a_goukei=sum(a)

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

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
q = int(input())

table = {}
sum = 0


for i in range(n):
    sum += a[i]
    if a[i] not in table.keys():
        table[a[i]] = 1
    else:
        table[a[i]] += 1
for i in range(q):
    b,c = map(int,input().split())
    if b in table.keys():
        sum -= b * table[b]
        sum += c * table[b]
        
        if c not in table.keys():
            table[c] = table[b]
        else:
            table[c] += table[b]
        table[b] = 0    
    print(sum)

Solution (Python):
import collections

n = int(input())
A = list(map(int,input().split()))
a = collections.Counter(A)
sum_ = sum(A)

Q = int(input())
B,C = [],[]
for i in range(Q):
  lis = list(map(int,input().split()))
  B.append(lis[0])
  C.append(lis[1])

for j,b in enumerate(B):
  sum_+=a[b]*(C[j]-b)
  a[C[j]]+=a[b]
  a[b]=0
  print(sum_)

Solution (Python):
n=int(input())
A=list(map(int,input().split()))
q=int(input())

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

for a in A:
    C[a]+=1

ans=sum(A)

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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
q = int(input())
s = sum(a)

count = [0] * 100000
for i in range(n):
    count[a[i] - 1] += 1
    
for i in range(q):
    o = list(map(int, input().split()))
    s += (o[1] - o[0]) * count[o[0] - 1]
    count[o[1] - 1] += count[o[0] - 1]
    count[o[0] - 1] = 0
    print(s)