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



Sample input:
4
1 2 3 4
3
1 2
3 4
2 4


Sample output:
11
12
16

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
query = [list(map(int,input().split())) for _ in range(q)]

all_sum  = sum(a)

cnt = {}
for i in a:
    if i not in cnt:
        cnt[a] = 0
    cnt[a] += 1

for (b, c) in query:
    if b in cnt:
        all_sum -= b*cnt[b]
        all_sum += c * cnt[b]
        if c not in cnt:
            cnt[c]=0 
    print(all_sum)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum=sum(a)
a2,a3=[],[]
import bisect as bs

for i in set(a):
    a2.append(i)
    a3.append(a.count(i))

#print(sum)
#print(a2,a3)

def idx(x):
    return bs.bisect_left(a2,x)

for i in range(q):
    b,c=map(int,input().split())
    if b in a2:
        b_idx=idx(b)
        if c in a2:
            c_idx=idx(c)
        else:
            a2.append(c)
            a3.append(0)
            c_idx=-1
        sum+=(c-b)*a3[b_idx]
        a3[c_idx]+=a3[b_idx]
        del a2[b_idx]
        del a3[b_idx]
#    print(a2,a3)
    print(sum)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum=sum(a)
a2,a3=[],[]
import bisect as bs

for i in set(a):
    a2.append(i)
    a3.append(a.count(i))

#print(sum)
#print(a2,a3)

def idx(l,x):
    return bs.bisect_left(l,x)

for i in range(q):
    b,c=map(int,input().split())
    if b in a2:
        b_idx=idx(a2,b)
        if c in a2:
            c_idx=idx(a2,c)
        else:
            a2.append(c)
            a3.append(0)
            c_idx=-1
        sum+=(c-b)*a3[b_idx]
        a3[c_idx]+=a3[b_idx]
        del a2[b_idx]
        del a3[b_idx]
#        a3[b_idx]=0
#    print(a2,a3)
    print(sum)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum=sum(a)
a2,a3=[],[]
import bisect as bs

for i in set(a):
    a2.append(i)
    a3.append(a.count(i))

#print(sum)
#print(a2,a3)

def idx(l,x):
    return bs.bisect_left(l,x)

for i in range(q):
    b,c=map(int,input().split())
    if b in a2:
        b_idx=idx(a2,b)
        if c in a2:
            c_idx=idx(a2,c)
        else:
            a2.append(c)
            a3.append(0)
            c_idx=-1
        sum+=(c-b)*a3[b_idx]
        a3[c_idx]+=a3[b_idx]
        a3[b_idx]=0
#    print(a2,a3)
    print(sum)

Submission status: Runtime Error

Solution code:
n = int(input())
arr = list(map(int, input().split()))
k = int(input())
s = sum(arr)
d = {}
for i in arr:
    if i in d:
        d[i] += 1
    else:
        d[i] = 1
for _ in range(k):
    v = 0
    a,b = map(int, input().split())
    s += (b-a) * d[a]
    if b in d:
        d[b] += d[a]
    else:
        d[b] = d[a]
    print(s)
    del d[a]

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum=sum(a)
a2,a3=[],[]
import bisect as bs

for i in set(a):
    a2.append(i)
    a3.append(a.count(i))

#print(sum)
#print(a2,a3)

def idx(l,x):
    return bs.bisect_left(l,x)

for i in range(q):
    b,c=map(int,input().split())
    if b in a2 and c in a2:
        b_idx=idx(a2,b)
        c_idx=idx(a2,c)
        sum+=(c-b)*a3[b_idx]
        a3[c_idx]+=a3[b_idx]
        a3[b_idx]=0
    elif b in a2:
        a2.append(c)
        b_idx=idx(a2,b)
        sum+=(c-b)*a3[b_idx]
        a3.append(a3[b_idx])
        a3[b_idx]=0
#    print(a2,a3)
    print(sum)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum=sum(a)
a2,a3=[],[]
import bisect as bs

for i in set(a):
    a2.append(i)
    a3.append(a.count(i))

for i in range(q):
    b,c=map(int,input().split())
    if b in a2 and c in a2:
        b_idx=bs.bisect_left(a2,b)
        c_idx=bs.bisect_left(a2,c)
        sum+=(c-b)*a3[b_idx]
        a3[c_idx]+=a3[b_idx]
        a3[b_idx]=0
    elif b in a2:
        a2.append(c)
        a3.append(0)
        b_idx=bs.bisect_left(a2,b)
        sum+=(c-b)*a3[b_idx]
        a3[-1]+=a3[b_idx]
        a3[b_idx]=0
    print(sum)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum=sum(a)
a2,a3=[],[]
for i in set(a):
    a2.append(i)
    a3.append(a.count(i))

for i in range(q):
    b,c=map(int,input().split())
    if b in a2 and c in a3:
        sum+=(c-b)*a3[a2.index(b)]
        a3[a2.index(c)]+=a3[a2.index(b)]
        a3[a2.index(b)]=0
    elif b in a2:
        a2.append(c)
        a3.append(0)
        sum+=(c-b)*a3[a2.index(b)]
        a3[a2.index(c)]+=a3[a2.index(b)]
        a3[a2.index(b)]=0
    print(sum)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
C = sum(A)
D = [0]*(10**5)
for n in range(N):
    D[A[n]] += 1
Q = int(input())
Z = []
for q in range(Q):
    x, y = map(int, input().split())
    if D[x]:
        C = C - x*D[x]
        C = C + y*D[x]
        Z.append(C)
        if D[y]:
            D[y] += 1*D[x]
        else:
            D[y] = D[x]
        D[x] = 0
    else:
        Z.append(C)
for z in Z:
    print(z)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
C = sum(A)
D = [0]*(10**5)
for n in range(N):
    D[A[n]] += 1
Q = int(input())
Z = []
for q in range(Q):
    x, y = map(int, input().split())
    if D[x]:
        C = C - x*D[x]
        C = C + y*D[x]
        Z.append(C)
        D[y] += 1*D[x]
        D[x] = 0
    else:
        Z.append(C)
for z in Z:
    print(z)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
dis_list = []
for i in range(Q):
    s = list(map(int, input().split()))
    dis_list.append(s)
for j in dis_list:

Submission status: Runtime Error

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
q = int(input())
change_list = [list(map(int, input().split())) for _ in range(q)]

cnt = [0] * 10**5
tot = sum(a_list)

for i in range(n):
    cnt[a_list[i]] += 1
#print(cnt)

for j in range(q):
    tot += cnt[change_list[j][0]] * (change_list[j][1]-change_list[j][0])
    cnt[change_list[j][1]] += cnt[change_list[j][0]]
    cnt[change_list[j][0]] = 0
    print(tot)

Submission status: Runtime Error

Solution code:
import numpy as np

def main():
  N = int(input())
  A = list(map(int,input().split()))
  A.sort()
  d = np.zeros(A[-1]+1, dtype=np.unit64)
  
  for i in range(N):
    a = A[i]
    a1 = A[i-1]
    if a != a1:
      if a != a1:
        d[a::a] += 1
      else:
        d[a] += 1
  print(np.count_nonzero(d[A] == 1))
  
main()

Submission status: Runtime Error

Solution code:
N = int(input())

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

Q = int(input())

goukei_AAA = 0
 for j in AAA:
    goukei_AAA = goukei_AAA + j

for i in range(0, Q):
    BC = list(map(int, input().split()))
    if not BC[0] in AAA:
        print(goukei_AAA)
        pass
    else:
        how_many_changes = AAA.count(BC[0])

        AAA = [BC[1] if m == BC[0] else m for m in AAA]
        goukei_AAA = goukei_AAA + (how_many_changes * (BC[1] - BC[0]))
        print(goukei_AAA)

Submission status: Runtime Error

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
q = int(input())
b_list = [list(map(int, input().split())) for _ in range(q)]
 
j = 0
while j < q:
  a_list = [b_list[j][1] if i == b_list[j][0] else i for i in a_list]
  s = sum(a_replace)
  print(s)
  j += 1

Submission status: Runtime Error

Solution code:
N = int(input())
A = sorted(map(int, input().split()))
ans = sum(A)
nums = [A.count(num) for num in range(10 ** 5)]

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

Submission status: Runtime Error

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

nums = [A.count(num) for num in range(10 ** 5)]
ans = sum(i * num for i, num in enumerate(nums))

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

Submission status: Runtime Error

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

ans = sum(A)
nums = [A.count(num) for num in range(10 ** 5)]

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

Submission status: Runtime Error

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

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

s = sum(a)

for i in range(q):
    s += (c[i]　-　b[i])　*　num[b[i]]
    num[c[i]] += num[b[i]]
    num[b[i]] = 0
    print(s)

Submission status: Runtime Error

Solution code:
n = int(input())
_a = list(map(int, input().split()))
a = {}
for i in range(n):
    if _a[i] in a:
        a[_a[i]] += 1
    else:
        a[_a[i]] = 1
q = int(input())
for i in range(q):
    b, c = map(int, input().split())
    if b in a:
        if c not in a:
            a[c] = 0
        tmp = a[b]
        a[c] += tmp
        a[b] = 0
        sum_ = 0
        for k in a:
            sum_ += k * a[k]
    print(sum_)

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int,input().split()))
q = int(input())
num = []
summ = 0
for i in range(10**5):
  num.append(0)
for i in range(N):
  num[a[i]-1] += 1
  summ += a[i]
for i in range(q):
  b,c = map(int,input().split())
  summ += (c-b)*a[b-1]
  a[c-1] += a[b-1]
  a[b-1] = 0
  print(summ)

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int,input().split()))
q = int(input())
num = []
summ = 0
for i in range(10**5):
  num.append(0)
for i in range(N):
  num[a[i]-1] += 1
  summ += 1
for i in range(q):
  b,c = map(int,input().split())
  summ += (c-b)*a[b-1]
  a[c-1] += a[b-1]
  a[b-1] = 0
  print(summ)

Submission status: Runtime Error

Solution code:
import sys
from collections import Counter

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N = int(readline())
    A = list(map(int, readline().split()))
    Q, *BC = map(int, read().split())

    total = sum(A)
    d = [0] * (N+1)
    for a in A:
        d[a] += 1

    for b, c in zip(BC[::2], BC[1::2]):
        total += (c - b) * d[b]
        d[c] += d[b]
        d[b] = 0
        print(total)

    return


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
C = [0] * 10001
s = 0
for i in A:
    C[i] += 1
    s += i

K = int(input())

for i in range(K):
    f, t = map(int, input().split())
    if f == t:
        continue
    C[t] += C[f]
    s += t * C[f]
    s -= f * C[f]
    C[f] = 0
    print(s)

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
C = [0] * 10001
s = 0
for i in A:
    C[i] += 1
    s += i

K = int(input())

for i in range(K):
    f, t = map(int, input().split())
    C[t] += C[f]
    s += t * C[f]
    s -= f * C[f]
    C[f] = 0
    print(s)

Submission status: Runtime Error

Solution code:
N = int(input())
A =[0] * 10001
s = 0
for i in map(int, input().split()):
    A[i] += 1
    s += i

K = int(input())

for i in range(K):
    f, t = map(int, input().split())
    A[t] += A[f]
    s += t * A[f]
    s -= f * A[f]
    A[f] = 0
    print(s)

Submission status: Runtime Error

Solution code:
def main():
    from collections import Counter
    n, *i = map(int, open(0).read().split())
    a = i[:n]
    _ = i[n]
    d = Counter(a)
    s = sum(k * v for k, v in d.items())
    ans = []
    for b, c in zip(*[iter(i[n + 1:])] * 2):
        v, w = d.get(b), d.get(c)
        if v is not None:
            s += (c - b) * v
        ans.append(s)
        d[c] = v if w is None else v + w

    print(*ans, sep="\n")


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 21 23:57:41 2020

@author: Administrator
"""
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
while(Q):
    B,C=map(int,input().split())
    A.sort()
    lo = 0
    hi = N-1
    while(lo<hi):
        mid = lo + (hi-lo)//2
        if A[mid]>=B:
            hi = mid
        else:
            lo = mid + 1
    while(A[lo]==B):
        A[lo]=C
        lo += 1
    #print(A)
    print(sum(A))
    Q -= 1

Submission status: Runtime Error

Solution code:
import collections

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

collect_dict = dict(collections.Counter(a))

now = sum(a)

ans = []
for i in range(q):
    b, c = map(int,input().split())
    now -= collect_dict[b] * b
    tmp = collect_dict[b]
    collect_dict.pop(b)
    if c not in collect_dict.keys():
        collect_dict[c] = 0
    collect_dict[c] += tmp
    now += c * tmp
    ans.append(now)

for i in ans:
    print(i)

Submission status: Runtime Error

Solution code:
import collections

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

collect_dict = dict(collections.Counter(a))

now = sum(a)

ans = []
for i in range(q):
    b, c = map(int,input().split())
    now -= collect_dict[b] * b
    tmp = collect_dict[b]
    collect_dict.pop(b)
    collect_dict[c] += tmp
    now += c * tmp
    ans.append(now)

for i in ans:
    print(i)

Submission status: Runtime Error

Solution code:
from sys import stdin

N = stdin.readline().rstrip()
#N = int(input())
lists = list(map(int,input().split()))
Q=stdin.readline().rstrip()
#Q = int(input())
listA=[] #appendのために宣言が必要
while True:
    try:
        listA.append(list(map(int,input().split())))

    except:
        break;
#print(listA[0])最初のリストが返される
for i in range(Q): 
    lista = []*2
    lista =listA[i]
    Ar= [lista[1] if lista[0] == x else x for x in lists]
    print(sum(Ar))
    lists = Ar

Submission status: Runtime Error

Solution code:
nb=int(input())
liste = [int(x) for x in input().split()]
liste2= [0]*(10**5)
for loop in range(len(liste)):
  liste2[liste[loop]] = liste.count(liste[loop])
nba = int(input())
somme = sum(liste)
for loop in range(nba):
  n,r = map(int,input().split())
  roger = liste2[n]
  for loop in range(len(liste)):
    if liste[loop]==n:
      liste[loop] = r
  diff = r-n
  somme += diff * roger
  liste2[r] += liste2[n]
  liste2[n] = 0
  print(somme)

Submission status: Runtime Error

Solution code:
import collections
n = int(input())
a = list(map(int, input().split()))
q = int(input())
dic = collections.Counter(a)
sum_list = [x * y for (x, y) in dic.items()]
sum = sum(sum_list)
for i in range(q):
    if b in dic:
        b, c = map(int, input().split())
        print(sum - (b - c) * dic[b])

Submission status: Runtime Error

Solution code:
n = int(input())
lis = list(map(int,input().split()))
q = int(input())
x = {}
for i in lis:
    if i not in x:
        x[i]=1
    else:
        x[i]+=1
total = sum(lis)
for i in range(q):
    a,b=map(int,input().split())
    diff = b-a
    total += diff*x[a]
    x[b]+=x[a]
    print(total)

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(_) for _ in input().split()]
Q = int(input())
b_to_c = [[int(_) for _ in input().split()] for i in range(Q)]

sum = 0
memo = [0] * (10 ** 5)
for i in A:
    memo[i] += 1
    sum += i

for bc in b_to_c:
    b = bc[0]
    c = bc[1]
    sum -= (b * memo[b])
    sum += (c * memo[b])
    memo[c] += memo[b]
    memo[b] = 0
    print(sum)

Submission status: Runtime Error

Solution code:
import collections
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = []
C = []
for i in range(Q):
    input_q = list(map(int, input().split()))
    B.append(input_q[0])
    C.append(input_q[1])
    
A_sum = sum(A)
CONST_MAX = 100000
count_list = [0] * CONST_MAX

# for i in range(CONST_MAX):
#     count_list[i] = A.count(i)
c = collections.Counter(A)
for k, v in c.items():
    # print(k, v)
    count_list[k] = v


for i in range(Q):
    count_list[C[i]] += count_list[B[i]]
    A_sum += (C[i] - B[i]) * count_list[B[i]]
    count_list[B[i]] = 0
    # print(count_list)

    print(A_sum)

Submission status: Runtime Error

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
list = [int(x) for x in stdin.readline().rstrip().split()]
Q = int(stdin.readline().rstrip())
#print(list)
for i in range(Q):
  s, t = [int(x) for x in stdin.readline().rstrip().split()]
  sum1 = 0
  for j in range(len(list)):
    if list[j] == s:
      list[j] = t
    sum1 = sum(list[j])
  print(sum1)

Submission status: Runtime Error

Solution code:

if __name__=='__main__':
    N = int(input())
    A = input().split(" ")
    A = [int(s) for s in A]
    Q=int(input())
    str_list=[]
    S=[]
    sum=0
    count=[0]*100000
    for i in range(len(A)):
         count[A[i]]=count[A[i]]+1

    for i in range(len(count)):
         sum=sum+i*count[i]

    for i in range(Q):
        str_list.append(list(input().split()))

    for j in range(Q):
        motonum=int(str_list[j][0])
        chnum=int(str_list[j][1])
        #print(motonum)
        #print(chnum)
        if count[motonum]!=0:
            sum=sum+(chnum-motonum)*count[motonum]
            count[chnum]=count[motonum]+count[chnum]
            count[motonum] = 0

        print(sum)

Submission status: Runtime Error

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

a = Counter(a)
for _ in range(q):
    b, c = map(int, input().split())
    a[c] += a[b]
    a.pop(b)
    ans = [x * y for x, y in a.items()]
    print(sum(ans))

Submission status: Runtime Error

Solution code:
import collections
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = []
C = []
for i in range(Q):
    input_q = list(map(int, input().split()))
    B.append(input_q[0])
    C.append(input_q[1])
    
A_sum = sum(A)
CONST_MAX = 100000
count_list = [0] * CONST_MAX

for i in range(CONST_MAX):
    count_list[i] = A.count(i)

for i in range(Q):
    count_list[C[i]] += count_list[B[i]]
    A_sum += (C[i] - B[i]) * count_list[B[i]]
    count_list[B[i]] = 0

    print(A_sum)

Submission status: Runtime Error

Solution code:
def solve():
    N = int(input())
    a = list(map(int, input().split()))
    s = sum(a)
    q = int(input())
    n = [0 for _ in range(100000)]
    for i in a:
        n[i] += 1
    for _ in range(q):
        b, c = map(int, input().split())
        s += (c - b) * n[b]
        print(s)
        n[c] += n[b]
        n[b] = 0


if __name__ == '__main__':
    solve()

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
s = [list(map(int, input().split())) for i in range(q)]

ans = sum(a)
from collections import Counter
cnt = Counter(a)

for i, j in s:
    
    if j in cnt:
        cnt[j] += cnt[i]
    else:
        cnt[j] = cnt[i]
        
    ans = ans - i * cnt[i] + cnt[i] * j
    cnt.pop(i)

    print(ans)

Submission status: Runtime Error

Solution code:
import collections
#入力
N = int(input())
n = 100000
Alist = list(map(int,input().split()))
Q = int(input())
Blist = []
Clist = []
for i in range(Q):
  B, C = map(int,input().split())
  Blist.append(B)
  Clist.append(C)

Adic = dict(collections.Counter(Alist))
numlist = [0]*n

for j in range(n):
  if j in Adic:
    numlist[j] = Adic[j]
sum = sum(Alist)
for k in range(Q):
  sum += (Clist[k]-Blist[k])*numlist[Blist[k]]
  numlist[Clist[k]] += numlist[Blist[k]] 
  numlist[Blist[k]] = 0

  print(sum)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
BC = [map(int, input().split()) for _ in range(Q)]
B, C = [list(i) for i in zip(*BC)]


#print(N)
#print(A)
#print(Q)
#print(x)
#print(y)

#取得完了

sum = sum(A)

#print(sum)

for i in range (Q):
  k = A.count(B[i])
  sum = sum + k * (C[i]-B[i])
  A.translate(str.maketrans(B[i]: C[i]))
  print(sum)

Submission status: Runtime Error

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

print(A)
Q = int(input())
for i in range(Q):
  S=0
  B,C = map(int, input().split())
  A[C-1] = A[C-1]+A[B-1]
  A[B-1] = 0
  for j in range(N):
    S = S+((j+1)*A[j])
  print(S)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc = [list(map(int, input().split())) for _ in range(q)]
s = 0
for g in range(len(a)):
    s += a[g]
e = [0] * 10001
for i in a:
    e[i] += 1
for j in range(q):
    s += (bc[j][1] - bc[j][0]) * e[bc[j][0]]
    e[bc[j][1]] += e[bc[j][0]]
    e[bc[j][0]] = 0
    print(s)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
s = 0
for g in range(len(a)):
    s += a[g]
e = [0] * 10001
for i in a:
    e[i] += 1
q = int(input())
bc = [list(map(int, input().split())) for _ in range(q)]
for j in range(q):
    s += (bc[j][1] - bc[j][0]) * e[bc[j][0]]
    e[bc[j][1]] += e[bc[j][0]]
    e[bc[j][0]] = 0
    print(s)

Submission status: Runtime Error

Solution code:
from collections import Counter

import sys

readline = sys.stdin.buffer.readline

def solver(int N, list A, int Q, list BC):
    d = Counter(A)

    sub_s = sum(A)

    for b, c in BC:
        sub_s += d[b] * (c - b)
        d[c] += d[b]
        d[b] = 0

        print(sub_s)


def run():
    N = int(readline())
    A = list(map(int, readline().split()))

    Q = int(readline())
    BC = [list(map(int, readline().split())) for _ in range(Q)]

    solver(N, A, Q, BC)

run()

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
b = []
c = []
s = []
for i in range(100000):
    s.append(a.count(i))
ans = sum(a)

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

Submission status: Runtime Error

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
list = [int(x) for x in stdin.readline().rstrip().split()]
Q = int(stdin.readline().rstrip())
#print(list)
for i in range(Q):
  s, t = [int(x) for x in stdin.readline().rstrip().split()]
  sum = 0
  for j in range(len(list)):
    if s in list
      if list[j] == s:
        list[j] = t
      sum += list[j]
  print(sum)

Submission status: Runtime Error

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

total = sum(a)
num = [0 for _ in range(10000+1)]

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

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc =[list(map(int, input().split())) for _ in range(q)]
temp = [0] * (10**5 + 1)

for i in a:
    temp[i] += 1
ans = sum(a)
for i in range(q):
    b,c = bc[i][0],bc[i][1]
    ans = ans - temp[b] * b + temp[b] * c 
    print(sum(temp))
    temp[c] += tremp[b]

Submission status: Runtime Error

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

total = sum(a)
num = [0 for _ in range(10+1)]

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

Submission status: Runtime Error

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

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

d = [0] * 10
for a in A:
    d[a] += 1
sum_a = sum(A)
for j in range(Q):
    d[C[j]] += d[B[j]]
    sum_a -= d[B[j]] * B[j]

    sum_a += (d[C[j]] - d[B[j]]) * C[j]
    d[B[j]] = 0
    print(sum_a)

Submission status: Runtime Error

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

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

dic = {}
for i in A:
    dic[i] = dic.get(i,0) + 1

sum = sum(A)

for i in range(Q):
    sum += dic[B[i]] * (C[i] -B[i])
    dic[C[i]] = dic.get(C[i], 0) + dic[B[i]]
    dic[B[i]] = 0
    print(sum)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
A = [0]*100000

for i in a:
    A[i] += 1
    
ans = sum(a)

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

Submission status: Runtime Error

Solution code:
import collections
N = int(input())
D = [0]*100000
A = list(map(int, input().split()))
a = sum(A)
for j in A:
    D[j] += 1
Q = int(input())
start = sum(A)
for i in range(Q):
    x,y=[int(i) for i in input().split()]
    a += D[x]*(y-x)
    print(a)
    D[y] +=D[x]
    D[x] = 0

Submission status: Runtime Error

Solution code:
2
1 2
3
1 100
2 100
100 1000

Submission status: Runtime Error

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

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

dic = {}
for i in A:
    dic[i] = dic.get(i,0) + 1

sum = sum(A)

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

Submission status: Runtime Error

Solution code:
a=list(map(int,input().split()))
q=int(input())
for i in range(q):
    b,c=map(int,input().split())
    for i in range(n):
        if a[i]==b:
            a[i]=c
    print(sum(a))

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <iomanip>
#include <algorithm>

using namespace std;

using ll = long long;
using ld = long double;
using lli = long long int;
using ull = unsigned long long;

#define PI 3.14159265358979323846264338327950L

int main(){
  ull N, Q, A_i, B_i, C_i, temp, sum=0;
  vector<ull> A_num(100001, 0);

  cin >> N;
  for(ull i=0; i<N; i++){
    cin >> temp;
    A_num.at(temp) += 1;
    sum += temp;
  }

  cin >> Q;
  for(ull i=0; i<Q; i++){
    cin >> B_i >> C_i;
    if(A_num[B_i] != 0){
      sum += A_num[B_i] * (C_i - B_i) ;
      A_num[C_i] += A_num[B_i];
      A_num[B_i] = 0;
      cout << sum << endl;
    }
  }
  
  return 0;
}

Submission status: Runtime Error

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

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

S=sum(A)
#まずはじめの合計求める

num=[0]
#これに数の出現回数をプロットする
for i in range(1,10**5+1):
    cnt=A.count(i)
    num[i]=cnt

for i in range(Q):
    B=trans[i][0]
    C=trans[i][1]
    change=num[B]*(C-B)
    num[C]+=num[B]
    num[B]-=num[B]
    S+=change
    print(S)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc =[list(map(int, input().split())) for _ in range(q)]
temp = [0] * (max(a)+1)

for i in a:
    temp[i] += i

for i in range(q):
    b,c = bc[i][0],bc[i][1]
    temp[c] += (temp[b]//b) * c
    temp[b] = 0
    print(sum(temp))

Submission status: Runtime Error

Solution code:
N = int(input())


A = list(map(int,input().split()))
ok = [0]*pow(10,5)
Q = int(input())

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

f):
    ok[A[i]]+=1
memo1=0
memo2=0

S = sum(A)
for i in range(Q):

    S+=ok[li[i][0]]*(li[i][1]-li[i][0])
    print(S)
    a = ok[li[i][0]]
    ok[li[i][1]]+=a
    ok[li[i][0]]=0
    
    if max(ok)==N:
        memo1=i
        memo2=ok.index(N)
        break



for i in range(memo1+1,Q):
    if li[i][0]==memo2:
        print(N*li[i][1])
        memo2=li[i][1]
    else:
        print(memo2*N)

Submission status: Runtime Error

Solution code:
from collections import deque, Counter
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
L = deque()
for _ in range(Q):
    b, c = map(int, input().split())
    L.append([b, c])
d = [0] * 10**5
count = Counter(A)
sum_a = sum(A)
for j in range(Q):
    d[L[j][1]] = count[L[j][0]]

    sum_a -= d[L[j][1]] * L[j][0]
    sum_a += d[L[j][1]] * L[j][1]

    print(sum_a)

Submission status: Runtime Error

Solution code:
from collections import Counter

N = int(input())
data = list(map(int,input().split()))
Q = int(input())
pala = list()
for i in range(Q):
    b = list(map(int,input().split()))
    pala.append(b)

count = Counter(data)
flag = 0


sum = 0
for k , v in count.items():
    sum += (k * v)

for B,C in pala:
    if B in count:
      diff = (C - B) * count[B]
      count[C] += count.pop(B)

    res = sum + diff
    sum = res
    print(res)
    diff = 0

Submission status: Runtime Error

Solution code:
from collections import Counter

N = int(input())
data = list(map(int,input().split()))
Q = int(input())
pala = list()
for i in range(Q):
    b = list(map(int,input().split()))
    pala.append(b)

count = Counter(data)
flag = 0


sum = 0
for k , v in count.items():
    sum += (k * v)

for B,C in pala:
    if B in count:
      diff = (C - B) * count[B]
      count[C] = count.pop(B)

    res = sum + diff
    sum = res
    print(res)
    diff = 0

Submission status: Runtime Error

Solution code:
N = int(input())

A = list(map(int,input().split()))
ok = [0]*pow(10,5)
Q = int(input())

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

for i in range(N):
    ok[A[i]]+=1
memo=0

S = sum(A)
for i in range(Q):
    print(S + ok[li[i][0]]*(li[i][1]-li[i][0]))
    a = ok[li[i][0]]
    ok[li[i][1]]+=a
    ok[li[i][0]]=0

Submission status: Runtime Error

Solution code:
N=int(input())
listA=list(map(int, input().split()))
Q=int(input())

listB=[] #appendのために宣言が必要
while True:
    try:
        listB.append(list(map(int,input().split())))

    except:
        break;
        
sum=sum(listA)
numlist=[0]*(10*5+1)
for i in range(10*5+1):
  numlist[i]=listA.count(i)
  
for i in range(Q):
  B=listB[i][0]
  C=listB[i][1]
  sum=sum+(C-B)*numlist[B]
  numlist[C]+=numlist[B]
  numlist[B]=0
  # print(numlist)
  print(sum)