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):
n = int(input())
a = list(map(int,input().split()))
b = {}
for x in a:
  try:
    if b[x]:
      b[x] += 1
  except:
    b[x] = 1

ans = sum(a)
q = int(input())
for _ in range(q):
  x,y = 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)

Solution (Python):





import math
import fractions
import sys
import bisect
import heapq  
import collections
from collections import Counter
from collections import deque
import pprint

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr



def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod



def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a


inf = 10 ** 18
mod = 10 ** 9 + 7

n = ir()
a = lr()
q = ir()
dic = Counter(a)
total = sum(a)
for i in range(q):
    b,c = lr()
    if c in dic:
        dic[c] += dic[b]
    else:
        dic[c] = dic[b]
    total += (c-b)*dic[b]
    dic[b] = 0
    print(total)

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

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

sm = 0
d = {}
for a in A:
    if a in d:
        d[a] += 1
    else:
        d[a] = 1
    sm += a

for _ in range(Q):
    b, c = map(int, input().split())
    if b in d:
        sm += (c-b) * d[b]
        if c in d:
            d[c] += d[b]
        else:
            d[c] = d[b]
        d.pop(b)
    print(sm)

Solution (Python):
from collections import Counter


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    N = read_int()
    counter = Counter(read_ints())
    T = sum(k*v for (k, v) in counter.items())
    Q = read_int()
    for _ in range(Q):
        b, c = read_ints()
        b_count = counter[b]
        counter[b] = 0
        counter[c] += b_count
        T = T-b*b_count+c*b_count
        print(T)


if __name__ == '__main__':
    solve()

Solution (Python):
def main():
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    D = [0] * (10**5+1)
    for i in range(N):
        D[A[i]] += 1

    ans = sum(A)
    for _ in range(Q):
        B, C = map(int, input().split())
        if D[B] != 0:
            ans -= B * D[B]
            ans += D[B] * C
            D[C] += D[B]
            D[B] = 0
            print(ans)
        else:
            print(ans)




if __name__ == "__main__":
    main()

Solution (Python):
import sys
from collections import Counter, defaultdict

n = int(sys.stdin.buffer.readline())
aaa = list(map(int, sys.stdin.buffer.readline().split()))
q = int(sys.stdin.buffer.readline())
bc = map(int, sys.stdin.buffer.read().split())

cnt = defaultdict(int, Counter(aaa))
ans = sum(a * c for a, c in cnt.items())
buf = []

for b, c in zip(bc, bc):
    if b not in cnt:
        buf.append(ans)
        continue
    ans += cnt[b] * (c - b)
    cnt[c] += cnt[b]
    del cnt[b]
    buf.append(ans)

print('\n'.join(map(str, buf)))

Solution (Python):
def resolve():
    n=int(input())
    a=list(map(int,input().split()))
    l=[0]*(10**5+1)
    for i in a:
        l[i]+=1
    ans=sum(a)
    q=int(input())
    for i in range(q):
        b,c=map(int,input().split())
        x=l[b]
        ans+=(c-b)*x
        l[c]+=x
        l[b]=0
        print(ans)

if __name__ == '__main__':
    resolve()

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]
q = int(input())
from collections import Counter
f = Counter(a)
t = sum(a)

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

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

ans=sum(a)
for i in range(q):
    if ac[bc[i][0]]!=None:
        if ac[bc[i][1]]!=None:
            ans-=bc[i][0]*ac[bc[i][0]]
            ans+=bc[i][1]*ac[bc[i][0]]
            ac[bc[i][1]]+=ac[bc[i][0]]
            ac[bc[i][0]]=0
        else:
            ans-=bc[i][0]*ac[bc[i][0]]
            ans+=bc[i][1]*ac[bc[i][0]]
            ac[bc[i][1]] = ac[bc[i][0]]
            ac[bc[i][0]]=0

    print(ans)

Solution (Python):
counter = {}
start_sum = 0
n = int(input())
a_list = list(map(int, input().split()))
q = int(input())
b_list, c_list = [], []
for _ in range(q):
    b_in, c_in = map(int, input().split())
    b_list.append(b_in)
    c_list.append(c_in)
    counter[b_in] = 0
    counter[c_in] = 0
    

for a in a_list:
    start_sum += a
    if not (counter.get(a) is None):
        counter[a] += 1

for b, c in zip(b_list, c_list):
    counter[c] += counter[b]
    start_sum += (c-b)*counter[b]
    counter[b] = 0
    print(start_sum)

Solution (Python):
from collections import defaultdict


N = int(input())
input_list = list(map(int, input().split()))
a_list = sorted(input_list)

a_sum = sum(a_list)
a_dict = defaultdict(int)
for a in a_list:
    a_dict[a] += 1

Q = int(input())
b_list, c_list = [], []
for _ in range(Q):
    input_list = list(map(int, input().split()))
    b, c = input_list
    
    replace_time = a_dict[b]
    a_dict[c] += a_dict[b]
    a_dict[b] = 0
    
    a_sum += c * replace_time - b * replace_time    
    print(a_sum)

Solution (Python):
import numpy as np

N = int(input())
lis = np.zeros(10**5)

As = np.array(input().split()).astype(np.int64)
NUM = As.sum()

for n in As:
  lis[n-1] += 1

Q = int(input())
for q in range(Q):
  B, C = map(int, input().split())
  nnn = lis[B-1]
  lis[B-1] = 0
  lis[C-1] += nnn
  NUM = NUM - B*nnn + C*nnn
  print(int(NUM))

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

s=sum(a)
d=[0]*(10**5+1)
for i in a:
    d[i-1]+=1

for _ in range(q):
    b,c=map(int,input().split())
    tmp=0
    k=d[b-1]
    d[c-1]+=k
    d[b-1]=0

    tmp = k*c-k*b
    s+=tmp
    print(s)

Solution (Python):
import collections
N = int(input())
A = list(map(int,input().split()))
ans = sum(A)
d = collections.Counter(A)
Q = int(input())
for i in range(Q):
    B,C = map(int,input().split())
    b = d[B]
    ans = ans + (C-B)*b
    print(ans)
    d[B] = 0
    d[C] = d[C] + b

Solution (Python):
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))

from collections import Counter

n = ii()
A = iil()
s = sum(A)
d = Counter(A)

q = ii()

for i in range(q):
    b,c = iim()
    m = d.pop(b,0)
    if m:
        d[c] = d.get(c,0)+m
    s += (c-b)*m
    print(s)

Solution (Python):






import sys
input = sys.stdin.readline

import math

import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')
AZ = "abcdefghijklmnopqrstuvwxyz"






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()


def DD(arg): return defaultdict(arg)



def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr


def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors


def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


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


def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count




N = I()
A = IL()
Q = I()

dic = DD(int)
for a in A:
  dic[a] += 1
  
ans = 0
for i in range(10**5+1):
  ans += dic[i]*i
  
for _ in range(Q):
  b,c = IL()
  ans -= dic[b]*b
  ans += dic[b]*c
  print(ans)
  dic[c] += dic[b]
  dic[b] = 0

Solution (Python):
from collections import Counter
input()
a = [int(x) for x in input().split()]

sum_ = sum(a)
d = Counter(a)

q = int(input())
for i in range(q):
    
    b, c = map(int, input().split())
    n = d[b]
    d[b] = 0
    if(c in d):
        d[c] += n
    else:    
        d[c] = n
    
    print(sum_ + n * (c - b))
    sum_ += n * (c - b)

Solution (Python):
from collections import defaultdict
n=int(input())
num_dict=defaultdict(int)
a_sum=0
for a in map(int,input().split()):
    num_dict[a]+=1
    a_sum+=a
for i in range(int(input())):
    b,c=map(int,input().split())
    a_sum+=c*num_dict[b]-b*num_dict[b]
    num_dict[c]+=num_dict[b]
    num_dict[b]=0
    print(a_sum)

Solution (Python):
import sys
import collections
sys.setrecursionlimit(10**6)


def main(input, print):
    n = int(input())
    a = list(map(int, input().split()))
    counter = collections.Counter(a)
    q = int(input())
    sum = 0
    for key in counter:
        sum += key * counter[key]
    for i in range(q):
        b, c = map(int, input().split())
        cnt = counter[b]
        del counter[b]
        counter[c] += cnt
        sum += (c-b)*cnt

        print(sum)


if __name__ == '__main__':
    main(sys.stdin.readline, print)

Solution (Python):
from collections import defaultdict

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

D = defaultdict(int)
S = 0

for i in A:
  D[i] += 1
  S += i

for _ in range(Q):
  B, C = map(int, input().split())
  r = D[B]
  D[B] = 0
  D[C] += r
  S += (C - B)*r
  print(S)

Solution (Python):
from collections import Counter

n = int(input())
a_array = [int(x) for x in input().split()]
d = Counter(a_array)
res = 0
for key, value in d.items():
    res += key * value
q = int(input())
bc_array = [[int(x) for x in input().split()] for y in range(q)]
for b, c in bc_array:
    d.setdefault(b, 0)
    res -= b * d[b]
    res += c * d[b]
    print(res)
    d[c] += d[b]
    d[b] = 0

Solution (Python):
n = int(input())
A = list(map(int,input().split()))
m = int(input())
Q = [list(map(int,input().split())) for _ in range(m)]
L = [0 for _ in range(10**5+1)]
for a in A:
    L[a]+=1
ans = sum(A)

for q in Q:
    num_b = L[q[0]]
    L[q[0]] -= num_b    
    L[q[1]] += num_b
    ans -= (num_b)*q[0]
    ans += (num_b)*q[1]
    print(ans)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
s = sum(a)
x = [0 for _ in range(10**5+1)]
for ai in a:
  x[ai] += 1
q = int(input())
for _ in range(q):
  b,c = map(int,input().split())
  xb = x[b]
  x[b] = 0
  s+= xb*(c-b)
  x[c] += xb
  print(s)

Solution (Python):
n = int(input())
li_a = list(map(int, input().split()))
q = int(input())
sum_ = sum(li_a)
di = {}
for i in range(1, 10**5+1):
    di[i] = 0
for i in li_a:
    di[i] += 1
for _ in range(q):
    b, c = map(int, input().split())
    tmp = di[b]
    di[b] = 0
    di[c] += tmp
    sum_ = sum_ + c*tmp - b*tmp
    print(sum_)

Solution (Python):
import collections
N = int(input())
A = list(map(int,input().split()))
counter = collections.Counter(A)
ans = sum(A)
Q = int(input())
R = []
for _ in range(Q):
    b,c = map(int,input().split())
    ans += (c-b)*counter[b]
    counter[c] +=counter[b]
    del counter[b]
    R.append(ans)
for r in R:
    print(r)

Solution (Python):

from collections import Counter

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

Solution (Python):
from collections import Counter
import sys
input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc = [list(map(int, input().split())) for _ in range(q)]

ans = sum(a)
a = Counter(a)
for b, c in bc:
    ans = ans + (c - b) * a[b]
    a[c] += a[b]
    a[b] = 0
    print(ans)

Solution (Python):
from collections import Counter 

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

dic = Counter(As)

rlt = 0
for k in dic:
  rlt += k*dic[k]

for _ in range(Q):
  b,c = map(int, input().split())
  rlt += (c-b)*dic[b]
  dic[c] += dic[b]
  dic[b] = 0
  print(rlt)

Solution (Python):
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
counts = Counter(a)
q = int(input())

total = sum(a)

for i in range(q):
    b, c = map(int, input().split())
    if b < c:
        total += (c - b) * counts[b]
        counts[c] += counts[b]
        counts[b] = 0
    else:
        total -= (b - c) * counts[b]
        counts[c] += counts[b]
        counts[b] = 0
    print(total)

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

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

Solution (Python):
n=int(input())
a=list(map(int, input().split()))
q=int(input())
num=[0]*(10**5)
tot=0
for i in range(n):
    num[a[i]-1]+=1
    tot+=a[i]

for _ in range(q):
    b,c=map(int, input().split())
    tot+=(num[b-1]*(c-b))
    num[c-1]+=num[b-1]
    num[b-1]=0
    print(tot)

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

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








    


from collections import Counter

N = int(input())
A = LI()
ans = sum(A)
Q = int(input())
A_c = Counter(A)

for _ in range(Q):
    B, C = MI()
    tmp_B = A_c[B]
    tmp_C = A_c[C]
    A_c[B] = 0
    
    if B in A_c.keys():
        A_c[C] += tmp_B
    else:
        A_c[C] = tmp_B
    
    ans = ans - B * tmp_B + C * (A_c[C] - tmp_C)
    print(ans)

Solution (Python):
n = int(input())
a = [int(e) for e in input().split()]

d = {}
sum = 0
for e in a:
    sum += e
    if e in d:
        d[e] += 1
    else:
        d[e] = 1


q = int(input())
for i in range(q):
    b, c = [int(e) for e in input().split()]

    if b in d:
        i = d.pop(b)
        if c in d:
            d[c] += i
        else:
            d[c] = i
        sum += (c - b) * i
    
    print(sum)

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

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

from collections import Counter
C = Counter(A)

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

Solution (Python):
from collections import Counter
n = int(input())
a = [int(i) for i in input().split()]
cnt = Counter(a)
sm = sum(a)
q = int(input())
for i in range(q):
    b,c = [int(i) for i in input().split()]
    diff = c - b
    sm += diff * cnt[b]
    print(sm)
    cnt.setdefault(b, 0)
    cnt.setdefault(c, 0)
    cnt[c] += cnt[b]
    cnt[b] = 0

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
D=dict()
for i in range(N):
    try:
        D[A[i]]+=1
    except:
        D[A[i]]=1

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

Q=int(input())
for _ in range(Q):
    b,c=map(int,input().split())
    if b in D:
        total=total-D[b]*b+D[b]*c
        print(total)
        try:
            D[c]+=D[b]
        except:
            D[c]=D[b]
        D[b]=0
    else:
        print(total)
        continue

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

n = int(readline())
a = list(map(int,readline().split()))
from collections import Counter
d = Counter(a)
s = sum(a)
q = int(readline())
for i in range(q):
  b,c = map(int,readline().split())
  m = d[b]
  s = s-m*(b-c)
  print(s)
  d.update({b:-m})
  d.update({c:m})

Solution (Python):
n = int(input())
lisA = list(map(int,input().split()))
dic  = {}
for item in lisA:
    if item in dic:
        dic[item] += 1
    else:
        dic[item] = 1
q = int(input())
ans = sum(lisA)
for i in range(q):
    b,c = map(int,input().split())
    if b not in dic:
        print(ans)
    else:
        tmp = dic[b]
        if c in dic:
            dic[c] += dic[b]
        else:
            dic[c] = dic[b]
        dic[b] = 0
        if b > c:
            ans -= (b-c) * tmp
        else:
            ans += (c-b) * tmp
        print(ans)

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

memo = defaultdict(int)
result = 0
for a in A:
    memo[a]+=1
    result += a

for _ in range(Q):
    bc = list(map(int, input().split()))
    b = bc[0]
    c = bc[1]
    tmp = memo[b]
    memo[c] += tmp
    memo[b] = 0
    result = result + c*tmp - b*tmp
    print(result)

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

from sys import stdin
input = stdin.readline

cnt = [0]*(10**5 + 1)
total = 0
for i in range(n):
  cnt[A[i]] += 1
  total += A[i]
  
for i in range(q):
  b,c = map(int, input().split())
  cnt[c] += cnt[b]
  total -= cnt[b]*b
  total += cnt[b]*c
  cnt[b] = 0
  print(total)

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

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

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

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

Q =  int(input())

SUM = sum(A)


for i in range(Q):
  b, c = map(int, input().split())
  
  cnt_b = lst[b]
  SUM += cnt_b * (c  - b)
  print(SUM)
  lst[c] += cnt_b
  lst[b] =  0

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

d={}
for i in range(N):
    if A[i] in d:
        d[A[i]]+=1
    else:
        d[A[i]]=1

ans=sum(A)
Q=int(input())

for _ in range(Q):
    b,c=map(int,input().split())
    if b in d:
        ans+=(c-b)*d[b]
        if c in d:
            d[c]+=d[b]
        else:
            d[c]=d[b]
        d.pop(b)
    print(ans)

Solution (Python):
n = int(input())
A = {}
B = list(map(int,input().split()))
S = sum(B)
for i in B:
    if i in A:
        A[i] += 1
    else:
        A[i] = 1

q = int(input())
for _ in range(q):
    b,c = map(int,input().split())
    if (b in A) and A[b] > 0:
        S += (c-b)*A[b]
        if not c in A:
            A[c] = 0
        A[c] += A[b]
        A[b] = 0
    print(S)

Solution (Python):
from collections import Counter
import sys

def input():
    return sys.stdin.readline().strip()

def main():

    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:
        tmp = a[b]
        a[b] = 0
        ans -= b * tmp
        a[c] += tmp
        ans += c * tmp
        print(ans)

if __name__ == "__main__":
    main()

Solution (Python):

import sys
from collections import defaultdict
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

N=int(input())
A=list(map(int,input().split()))
d=defaultdict(int)
for a in A:
    d[a]+=1
ans=sum(A)
Q=int(input())
for _ in range(Q):
    b,c=map(int,input().split())
    ans+=c*d[b]-b*d[b]
    d[c]+=d[b]
    d[b]=0
    print(ans)

Solution (Python):
from collections import defaultdict
n = int(input())
a_list = list(map(int,input().split()))
q = int(input())
d = defaultdict(lambda: 0)
ans = sum(a_list)
for i in a_list:
    d[i] += 1
    
ans = sum(a_list)
for i in range(q):
    a,b = map(int,input().split())
    ans = ans + b*d[a] - a*d[a]
    d[b] += d[a]
    d[a] -= d[a]
    print(ans)

Solution (Python):

import sys
import numpy as np
import collections


def solve(N: int, A: "List[int]", Q: int, B: "List[int]", C: "List[int]"):
    cc = collections.Counter(A)
    ans = sum(A)
    for i in range(1,Q+1):
        cb = cc[B[i-1]]
        cc[B[i-1]] = 0
        cc[C[i-1]] += cb
        ans -= cb * B[i-1]
        ans += cb * C[i-1]
        print(ans)

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    Q = int(next(tokens))  
    B = [int()] * (Q)  
    C = [int()] * (Q)  
    for i in range(Q):
        B[i] = int(next(tokens))
        C[i] = int(next(tokens))
    solve(N, A, Q, B, C)

if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
BC=[list(map(int,input().split())) for i in range(Q)]
from collections import Counter
x=Counter(A)
s=sum([k*v for k,v in x.items()])
for b,c in BC:
    if b in x:
        s+=(c-b)*x[b]
        if c in x:
            x[c]+=x[b]
        else:
            x[c]=x[b]
        del x[b]
    print(s)

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

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

q = int(input())
ans = sum(a)

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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
mp = dict()
ans = 0
for i in range(n):
    try:
        mp[a[i]]+=1
    except:
        mp[a[i]]=1
    ans += a[i]


q= int(input())
while q:
    q-=1
    b,c = map(int, input().split())
    try:
        cnt = mp[b]
        try:
            mp[c]+=cnt
        except:
            mp[c]=cnt
        mp[b]=0
        ans += cnt*(c-b)
    except:
        pass
    print(ans)

Solution (Python):
from collections import Counter
N = int(input())
As = list(map(int, input().split()))

ct = Counter(As)
S = sum(As)

Q = int(input())
for _ in range(Q):
    b, c = map(int, input().split())
    n = ct[b]
    ct[b] = 0
    ct[c] += n
    S += (c-b)*n
    print(S)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
total = sum(A)
from collections import defaultdict
d = defaultdict(lambda: 0)
for a in A:
    d[a] += 1
Q = int(input())
ans = []
for i in range(Q):
    x,y = map(int, input().split())
    total += y*d[x]-x*d[x]
    d[y] += d[x]
    d[x] = 0
    ans.append(total)
print(*ans, sep='\n')

Solution (Python):
from collections import Counter

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

asum=sum(A)

C=Counter(A)

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

    asum=asum+(c-b)*C[b]

    C[c]=C[c]+C[b] if c in C else C[b]
    C[b]=0

    print(asum)

Solution (Python):
import collections
dic = collections.defaultdict(int)
n = int(input())
a = [int(i) for i in input().split()]
q = int(input())
sm = 0
for i in range(n):
    dic[a[i]] += 1
    sm += a[i]
queries = []
for i in range(q):
    queries.append([int(j) for j in input().split()])

for i in range(q):
    b = queries[i][0]
    c = queries[i][1]
    sm += dic[b] * (c-b)
    dic[c] += dic[b]
    dic[b] = 0
    print(sm)

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

co = collections.Counter(a)

s = [0]*(q+1)
s[0] = sum(a)

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

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())
    
d=dict(Counter(A))

summ=sum(A)

for i in range(Q):
    if C[i] not in d:
        d[C[i]]=0
    if B[i] in d:
        summ -= d[B[i]]*B[i]
        summ += d[B[i]]*C[i]
        d[C[i]] += d[B[i]]
        d[B[i]]=0
    print(summ)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
q = int(input())
b, c = [], []
smap = {}
sum_ = sum(a)

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

for aa in a:
    smap[aa] = smap.get(aa, 0) + 1



for i in range(q):

    try:
        count = smap[b[i]]
        diff = (c[i] - b[i]) * count
        smap[c[i]] = smap.get(c[i], 0) + count
        del smap[b[i]]
        sum_ += diff
        
    except KeyError:
        pass

    print(sum_)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
t = [0]*(10**5+1)
s = 0
for i in range(n):
    t[a[i]] += 1
    s += a[i]
q = int(input())
for j in range(q):
    b,c = map(int,input().split())
    s += (c-b)*t[b]
    t[c] += t[b]
    t[b] *= 0
    print(s)

Solution (Python):
from collections import Counter

n = int(input())

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


tmpsum = sum(a)

q = int(input())

for i in range(q):
    ans = tmpsum
    before, after = map(int, input().split())

    num_of_before = c[before]
    

    ans += num_of_before * (after - before)
    tmpsum = ans

    c[after] += num_of_before
    c[before] = 0
    print(ans)

Solution (Python):
import numpy as np
from collections import defaultdict

N = int(input())

arr = np.array(list(map(int, input().split())))
unique, cnts = np.unique(arr, return_counts=True)

counts = defaultdict(int)
sum = arr.sum()

for key, val in zip(unique, cnts):
    counts[key] = val

Q = int(input())

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

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

def main():
    N = int( input())
    A = list( map( int, input().split()))
    Q = int( input())
    BC = [ tuple( map( int, input().split())) for _ in range(Q)]
    L = [0]*(10**5+1)
    ans = sum(A)
    for a in A:
        L[a] += 1
    ANS = []
    for b, c in BC:
        ans = ans + L[b]*c - L[b]*b
        L[c] += L[b]
        L[b] = 0
        ANS.append(ans)

    print("\n".join( map( str, ANS)))
if __name__ == '__main__':
    main()

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

from collections import Counter
counter = Counter(A)

for q in range(Q):
  b, c = map(int, input().split())
  b_count = counter.get(b, 0)
  counter[c] = counter.get(c, 0) + b_count
  if b_count:
    del counter[b]
  b_sum = b * b_count
  c_sum = c * b_count
  a_sum = a_sum - b_sum + c_sum
  print(a_sum)

Solution (Python):
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
nums = Counter(a)
q = int(input())
ans = 0
for k, v in nums.items():
    ans += k*v
for _ in range(q):
    b, c = map(int, input().split())
    ans -= nums[b] * b
    ans += nums[b] * c
    nums[c] += nums[b]
    nums[b] = 0
    print(ans)

Solution (Python):
from collections import Counter

import sys


def input():
    return sys.stdin.readline()[:-1]


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

a_sum = sum(A)
Q = int(input())
for _ in range(Q):
    b, c = map(int, input().split())

    if b in ac:
        a_sum += (c-b)*ac[b]
        ac[c] += ac[b]
        del ac[b]
    print(a_sum)