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):
import sys
input = sys.stdin.readline
from collections import *

N = int(input())
A = list(map(int, input().split()))
S = sum(A)
cnt = Counter(A)
Q = int(input())

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

Solution (Python):
import collections

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


q=int(input())

anscol = collections.Counter(a_list)


goukei=sum(a_list)

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

Solution (Python):
from collections import Counter

n = int(input())
aa = tuple(map(int, input().split()))
actr = Counter(aa)
q = int(input())

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

Solution (Python):
import collections,sys
input=sys.stdin.readline
n=int(input())
l=list(map(int,input().split()))

c = collections.Counter(l)
q=int(input())
ans=0
for x in c:
  ans+=x*c[x]
for _ in range(q):
  a,b=map(int,input().split())
  if not a in c:
    print(ans)
  elif c[a]==0:
    print(ans)
  else:
    if b in c:
      c[b]+=c[a]
    else:
      c[b]=c[a]
    ans+=(b*c[a]-a*c[a])
    print(ans)
    c[a]=0

Solution (Python):
from collections import defaultdict

n = int(input())
A = list(map(int, input().split()))
q = int(input())
BC = [tuple(map(int, input().split())) for _ in range(q)]
total = sum(A)
d = defaultdict(int)
for a in A:
    d[a] += 1
for b, c in BC:
    total = total - b*d[b] + c*d[b]
    d[c] += d[b]
    d[b] = 0
    print(total)

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

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

def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

def main():
    N = Z()
    A = ZZ()
    D = collections.Counter(A)
    s = sum(A)

    Q = Z()
    ans = []
    for _ in range(Q):
        b, c = ZZ()
        if D[b]:
            s += (c-b)*D[b]
            D[c] += D[b]
            D[b] = 0
        ans.append(s)
    for i in range(Q):
        print(ans[i])

    return

if __name__ == '__main__':
    main()

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

A.sort()
s = sum(A)
A_dict = dict()
A_dict[A[0]] = 1
for i, value in enumerate(A):
    if i == 0: continue
    bA = A[i-1]
    if value == bA:
        A_dict[A[i]] += 1
    else:
        A_dict[A[i]] = 1



for i in range(Q):
    B, C = map(int, input().split())
    if B in A_dict:
        s += (C - B) * A_dict[B]
        if C in A_dict:
            A_dict[C] += A_dict.pop(B)
        else:
            A_dict[C] = A_dict.pop(B)
    print(s)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
M = max(a)
counts = [0 for _ in range(100000+1)]
s = sum(a)
for i in range(n):
  counts[a[i]] += 1
for i in range(int(input())):
  b, c = map(int, input().split())
  move = counts[b]
  counts[b] = 0
  counts[c] += move
  s += (c - b)*move
  print(s)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
q = int(input())
bcl = [list(map(int,input().split())) for nesya in range(q)]
hoge = dict()
for num in a:
  if num in hoge:
    hoge[num] += 1
  else:
    hoge[num] = 1
total = 0
for k,v in hoge.items():
  total += k*v
for bc in bcl:
  b = bc[0]
  c = bc[1]
  if b in hoge:
    total += (c-b)*hoge[b]
  print(total)
  if b in hoge:
    if c in hoge:
      hoge[c] += hoge[b]
    else:
      hoge[c] = hoge[b]
    hoge[b] = 0

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

Solution (Python):
from collections import Counter, defaultdict


_ = int(input())

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

nrows = int(input())

grid = []
for _ in range(nrows):
    grid.append(list(map(int,input().split())))

c = Counter(lst)
res = sum(lst)

for old, new in grid:
    diff = new - old
    res += c[old]*diff
    print(res)
    c[new] = c[new] + c[old]
    c[old] = 0

Solution (Python):
import collections
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
a = collections.Counter(A)
tmp = sum(A)

for i in range(Q):
  B,C = map(int,input().split())
  tmp += (a[B]*C -a[B]*B)
  if C in a:
    a[C] += a[B]
  else:
    a[C]=a[B]
  del a[B]
  print(tmp)

Solution (Python):
from collections import defaultdict
N=int(input())
A=list(map(int, input().split()))
D=defaultdict(int)
for a in A:
    D[a]+=1

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

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

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

ls = [0 for _ in range(10**5 + 1)]
ans = 0
for a in an:
  ls[a] += 1
  ans += a
for b, c in bc:
  x = ls[b]
  ls[c] += x
  ls[b] = 0
  ans = ans - x * b + x * c
  print(ans)

Solution (Python):
from sys import stdin,stdout
input = stdin.readline
print = stdout.write
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
c = Counter(a)
s = sum(a)
for _ in range(int(input())):
    b1,c1 = map(int,input().split())
    if b1 in c and c[b1]:
      s+=c[b1]*c1-c[b1]*b1
      c[c1]= c.get(c1,0)+c[b1]
      c[b1]=0
    print(str(s)+'\n')

Solution (Python):
n=int(input())

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

s=sum(l)
d={}
for i in l:
    d[i]=d.get(i,0)+1

for i in range(int(input())):

    a,b=map(int,input().split())
    if a not in d:
        print(s)
        continue
    
    x=d[a]
    d[a]-=x
    d[b]=d.get(b,0)+x
    s-=x*a
    s+=x*b
    print(s)

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



A = list(A)

hashmap = {}
s = 0
for i in range(len(A)):
	s += A[i]
	hashmap[A[i]] = hashmap.get(A[i], [0, A[i]]) 
	hashmap[A[i]][0] += 1

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

	if B in hashmap:
		num = hashmap[B][0]
		del hashmap[B]
		s -= num * B
		s += num * C

		hashmap[C] = hashmap.get(C, [0, C])
		hashmap[C][0] += num
	print(s)

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

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

Counts = Counter(ListA)
Sum = sum(ListA)

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

Solution (Python):
from collections import defaultdict

N=int(input())
A=[int(x) for x in input().split()]
Q=int(input())
D=[0 for i in range(100001)]
ans=0
for a in A:
    D[a]+=1
for i in range(100001):
    ans+=i*D[i]

for i in range(Q):
    B,C=[int(x) for x in input().split()]
    tmp1=D[B]*B
    D[C]+=D[B]
    tmp2=D[B]*C
    D[B]=0
    ans=ans-tmp1+tmp2
    print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
cnt = [0]*100010
for i in range(N):
    cnt[A[i]] += 1

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

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

D={}
for x in A:
    if x in D:
        D[x]+=1
    else:
        D[x]=1

S=sum(A)

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

    if B in D:
        S+=(C-B)*D[B]
        if C in D:
            D[C]+=D[B]
        else:
            D[C]=D[B]

        D[B]=0

    print(S)

Solution (Python):
from collections import defaultdict
import sys

def read_int():
    l = sys.stdin.readline().rstrip('\n').split(' ')
    return int(l[0])

def read_int_list():
    l = sys.stdin.readline().rstrip('\n').split(' ')                                                  
    return [int(i) for i in l]

N = read_int()
A = read_int_list()
Q = read_int()                                                                                        
BC = []
for _ in range(Q):                                                                                    
    BC.append(read_int_list())                                                                        

_sum = sum(A)                                                                                         

n2c = defaultdict(int)                                                                                

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

for bc in BC:                                                                                         
    b = bc[0]                                                                                         
    c = bc[1]                                                                                         
    b_cnt = n2c[b]                                                                                    
    n2c[b] = 0                                                                                        
    n2c[c] += b_cnt                                                                                   
    _sum += b_cnt * (c - b)                                                                           
    print(_sum)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
num = 100002
count = [0]*num
tot = 0
for a in A:
    count[a] += 1
    tot += a
    
Q = int(input())

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

Solution (Python):
n = int(input())
alist = list(map(int,input().split()))
query = int(input())
bc = [list(map(int,input().split()))for i in range(query)]

ssum = 0
amx = max(alist)
bcmx = 0
for i in range(query):
    if bcmx < bc[i][1]:
        bcmx = bc[i][1]
mx = max(amx,bcmx)
qlist = [0]*(mx+1)
for a in alist:
    qlist[a] += 1
    ssum += a
anslist = []
for q in range(query):
    b = bc[q][0]
    c = bc[q][1]
    ssum += qlist[b]*(c-b)
    qlist[c] += qlist[b]
    qlist[b] = 0
    anslist.append(ssum)
for ans in anslist:
    print(ans)


'''
adic = {}
ssum = 0
for a in alist:
    if a not in adic:
        adic[a] = 0
    adic[a] += 1
    ssum += a
for q in range(query):
'''

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
f = {}
s = 0
for i in a:
    s += i
    if i not in f:
        f[i] = 0
    f[i]+=1
q = int(input())
for _ in range(q):
    b,c = map(int,input().split())
    if b not in f:
        print(s)
        continue
    s += f[b]*(c-b)
    print(s)
    if c not in f:
        f[c] = 0
    f[c] += f[b]
    f[b] = 0

Solution (Python):
import math

import queue
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit


ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n = int(ipt())
    a = [int(i) for i in ipt().split()]
    d = defaultdict(int)
    sum = 0
    for i in a:
        sum += i
        d[i] += 1
    q = int(ipt())
    for _ in range(q):
        b,c = map(int,ipt().split())
        sum += d[b]*(c-b)
        print(sum)
        d[c] += d[b]
        d[b] = 0
    return

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
X = [0] * (10 ** 5 + 1)
wa = 0
for i in range (N):
    X[A[i]] += 1
    wa += A[i]
Q = int(input())
for i in range(Q):
    b,c = map(int,input().split())
    wa = wa + X[b] * (c - b)
    print(wa)
    X[c] += X[b]
    X[b] = 0

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
num = {}
ans = 0
for i in a:
    num.setdefault(i, 0)
    num[i] += 1
    ans += i
q = int(input())
for i in range(q):
    b, c = map(int, input().split())
    num.setdefault(b, 0)
    ans += (c - b) * num[b]
    if c in num.keys():
        num[c] += num[b]
        del num[b]
    else:
        num[c] = num[b]
        del num[b]
    print(ans)

Solution (Python):




















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

count = {}
for x in a:
	if x in count:
		count[x] += 1
	else:
		count[x] = 1

s = sum(a)

Q = int(input())
for q in range(Q):
	a, b = map(int, input().split())
	acount = count[a] if a in count else 0
	s -= a * acount 
	s += b * acount
	count[a] = 0
	if b not in count:
		count[b] = acount
	else:
		count[b] += acount
	print(s)

Solution (Python):
import sys
from collections import defaultdict

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prl = lambda x: print(*x ,sep='\n')

n = ni()
a = nl()
q = ni()
list_ = defaultdict(lambda: 0)
for i in a:
    list_[i] += 1
res = sum(a)
for i in range(q):
    b,c = nm()
    res = res + (c*(list_[b])-b*list_[b])
    x = list_[b]
    list_[b] = 0
    list_[c] += x

    print(res)

Solution (Python):
n=int(input())
a=dict()
ans=0
for i in map(int,input().split()):
    ans+=i
    if i in a:
        a[i]+=1
    else:
        a[i]=1
q=int(input())
for i in range(q):
    b,c=map(int,input().split())
    if c not in a:
        a[c]=0
    if b not in a:
        a[b]=0
    ans=ans-b*a[b]+c*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()))

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

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

ans = sum(a)

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

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    _LI = lambda : [-int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()
    DD = ((1,0),(0,1),(-1,0),(0,-1))

    N = NI()
    c = [0] * (10**5+1)
    ans = 0
    for x in sys.stdin.readline().split():
        x = int(x)
        c[x] += 1
        ans += x

    Q = NI()
    for _ in range(Q):
        i,j = LI()
        k = c[i]
        ans += (j-i) * k
        c[j] += k
        c[i] = 0
        print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys, os, math, bisect, itertools, collections, heapq, queue

from decimal import Decimal
from collections import defaultdict



sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)


MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    A = il()
    Q = ii()
    cnt = collections.Counter(A)
    sm = sum(A)

    for q in range(Q):
        B, C = il()
        sm += (C - B) * cnt[B]
        print(sm)
        cnt[C] += cnt[B]
        cnt[B] = 0


if __name__ == '__main__':
    main()

Solution (Python):
import sys
N = int(sys.stdin.readline().rstrip())
A = list(map(int, sys.stdin.readline().rstrip().split()))
S = [0]*(10**5)

ans = 0
for n in range(N):
    a = A[n]
    S[a-1] += a
    ans += a

Q = int(sys.stdin.readline().rstrip())

for q in range(Q):
    B, C = list(map(int, sys.stdin.readline().rstrip().split()))
    up = C * S[B-1]//B
    down = S[B-1]
    S[C-1] += C * S[B-1]//B
    S[B-1] = 0
    ans = ans + up - down
    print(ans)

Solution (Python):

import sys
input = sys.stdin.readline
ipnl = lambda n: [int(input()) for _ in range(n)]
inp = lambda :int(input())
ip = lambda :[int(w) for w in input().split()]

n = inp()
x = ip()
q = inp()
dt = {}
for i in x: dt[i] = dt.get(i,0)+1
sm = sum(x)
for i in range(q):
    b,c = ip()
    ct = dt.get(b,0)
    if ct == 0:
        print(sm)
        continue
    sm -= ct*b
    sm += ct*c
    print(sm)
    dt[b] = 0
    dt[c] = dt.get(c,0) + ct

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
S = sum(A)
howmany = [0 for k in range(10**5+1)]
for k in range(N):
    howmany[A[k]] +=1

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

Solution (Python):
import sys
from collections import Counter


def _i(): return int(sys.stdin.readline().strip())


def _ia(): return map(int, sys.stdin.readline().strip().split())


def main():
    n = _i()
    ai = Counter(_ia())
    s = sum(k * v for k, v in ai.items())
    q = _i()
    for _ in range(q):
        b, c = _ia()
        m = ai.get(b, 0)
        s += (c-b) * m
        ai[b] = 0
        ai[c] += m
        print(s)


if __name__ == "__main__":
    main()

Solution (Python):



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

from collections import deque,defaultdict,Counter











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()
dic = Counter(A)
SUM = sum(A)

for i in range(q):
    b,c = readInts()
    SUM -= b * dic[b]
    SUM += c * dic[b]
    dic[c] += dic[b]
    dic[b] = 0
    print(SUM)

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

def main():
    n=int(input())
    A=list(map(int,input().split()))
    q=int(input())
    BC=[list(map(int,input().split())) for _ in range(q)]
    C=Counter(A)
    ans=sum(A)
    for b,c in BC:
        cnt=C[b]
        ans+=(c-b)*cnt
        C[b]=0
        C[c]+=cnt
        print(ans)
    
if __name__=='__main__':
    main()

Solution (Python):
from heapq import *
import sys
from collections import *
from itertools import *
from decimal import *
import copy
from bisect import *
import math
sys.setrecursionlimit(4100000)
def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))
input=lambda :sys.stdin.readline().rstrip()

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

suma=sum(A)
dicA=defaultdict(int)
for a in A:
    dicA[a]+=1

for b,c in BC:
    D=(c-b)*dicA[b]
    suma+=D
    e=dicA[b]
    dicA[b]=0
    dicA[c]+=e
    print(suma)

Solution (Python):


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n=I()
lis=LI()
q=I()
cou=Counter(lis)
ans=sum(lis)
for i in range(q):
    b,c=MI()
    x=cou[b]
    
    ans+=(c-b)*x
    print(ans)
    cou[c]+=cou[b]
    cou[b]=0

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

q = int(input())
for i in range(q):
  b, c = map(int, input().split())
  if b in key and key[b] > 0:
    sub = key[b]
    ans += (c-b)*sub
    if c in key:
      key[c] += sub
    else:
      key[c] = sub
    key[b] = 0
  print(ans)

Solution (Python):


import sys
input=sys.stdin.readline

def main():
    N=int(input())
    A=list(map(int,input().split()))
    Q=int(input())
    cnt=[0]*(10**5+1)
    sumA=sum(A)
    for a in A:
        cnt[a]+=1
        
    for query in range(Q):
        b,c=map(int,input().split())
        sumA+=cnt[b]*(c-b)
        cnt[c]+=cnt[b]
        cnt[b]=0
        print(sumA)
        
    
    
    
if __name__=="__main__":
    main()

Solution (Python):
N=int(input())
L=list(map(int,input().split()))
D={}
for i in L:
    if i not in D:
        D[i]=1
    else:
        D[i]+=1



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


Q=int(input())

for i in range(Q):
    a,b=map(int,input().split())
    if a not in D:
        print(ans)
        continue
    else:
        if b not in D:
            ans-=a*D[a]
            ans+=b*D[a]
            D[b]=D[a]
            
            D[a]*=0
            print(ans)
        else:
            ans-=a*D[a]
            ans+=b*D[a]
            D[b]+=D[a]
            D[a]*=0
            print(ans)

Solution (Python):
if __name__ == '__main__':
    n = int(input())
    arr = list(map(int, input().split()))
    d = {}
    for i in range(len(arr)):
        if arr[i] in d:
            d[arr[i]] += 1
        else:
            d[arr[i]] = 1
    sums = sum(arr)
    q = int(input())
    for i in range(q):
        b, c = map(int, input().split())
        if b in d:
            sums -= (b * d[b])
            sums += (c * d[b])
        if c not in d and b in d:
            d[c] = d[b]
        elif b in d:
            d[c] += d[b]
        if b in d:
            d[b] = 0
        print(sums)

Solution (Python):
from collections import Counter
X = open(0).readlines()
n = int(X[0])
*A, = map(int, X[1].split())
counter = Counter(A)
q = int(X[2])
s = sum(A)
for x in X[3:]:
    b, c = map(int, x.split())
    if b in counter:
        t = counter.pop(b)
        s += c*t - b*t
        counter[c] = counter.get(c, 0) + t
    print(s)

Solution (Python):

from collections import*
(n,), a, q, *d = [list(map(int, o.split())) for o in open(0)]
C = Counter(a)
s = sum(a)
for b, c in d:
    bf = C[b]
    C[c] += bf
    C[b] = 0
    s += (c - b) * bf
    print(s)

Solution (Python):
from collections import Counter

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

counter = Counter(A)
tmp_sum = sum(A)
for i in range(Q):
    B, C = map(int, input().split())
    B_cnt = counter[B]
    counter[B] = 0
    counter[C] += B_cnt
    tmp_sum = tmp_sum + (C - B) * B_cnt
    print(tmp_sum)

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

from collections import defaultdict

D = defaultdict(int)

res = 0

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

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

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

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

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

ans = sum(A)

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

Solution (Python):


import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = LI()
cnt = Counter(a)
s = sum(a)
q = I()
for _ in range(q):
    b, c = LI()
    s -= b * cnt[b]
    s += c * cnt[b]
    cnt[c] += cnt[b]
    cnt[b] = 0
    print(s)

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

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

Solution (Python):
import heapq

n = int(input())
a = [int(i) for i in input().split()]
q = int(input())
bc = [[int(i) for i in input().split()] for j in range(q)]

dp = [0 for _ in range(100001)]

for i in a:
    dp[i] += 1

total = sum(a)

for i in range(q):
    total += (bc[i][1] - bc[i][0]) * dp[bc[i][0]]
    print(total)
    dp[bc[i][1]] += dp[bc[i][0]]
    dp[bc[i][0]] = 0

Solution (Python):
from collections import defaultdict
d = defaultdict(int)
N = int(input())
A = list(map(int, input().split()))
for a in A:
    d[a] += 1
s = sum(A)
Q = int(input())

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

Solution (Python):
import sys
from collections import Counter

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 19
MOD = 10 ** 9 + 7
EPS = 10 ** -10

N = INT()
A = LIST()
C = Counter(A)

cur = sum(A)
Q = INT()
for _ in range(Q):
    b, c = MAP()
    cur -= b * C[b]
    cur += c * C[b]
    print(cur)
    C[c] += C[b]
    C[b] = 0

Solution (Python):
array = [0 for i in range(100001)]
N = int(input())
A = list(map(int, input().split()))
Q = int(input())

for a in A:
    array[a] += 1
souwa = sum(A)

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

for i in range(Q):
    souwa = souwa + (C[i]-B[i])*array[B[i]]
    array[C[i]] += array[B[i]]
    array[B[i]] = 0
    print(souwa)

Solution (Python):
import sys
from collections import defaultdict
dic1 = defaultdict(int)
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

n = int(readline())
lst1 = list(map(int,readline().split()))
q = int(readline())

ans = 0
for i in lst1:
    dic1[i] += 1
    ans += i

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

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
from math import floor, sqrt, factorial, hypot, log  
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
from fractions import gcd
from random import randint


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


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()
    A = LI()
    Q = I()
    AI = [0]*(10**5+1)
    ans = 0
    for a in A:
        AI[a] += 1

    for i in range(10 ** 5 + 1):
        ans += i * AI[i]
    for _ in range(Q):
        B, C = MI()
        ans = ans - B * AI[B] + C * AI[B]
        AI[C] += AI[B]
        AI[B] = 0
        print(ans)


if __name__ == '__main__':
    main()