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: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
ans = []
for i in range(Q):
  B, C = map(int, input().split())
  for j in range(len(A)):
    if A[j] == B:
      A[j] = C
  ans.append(sum(A))

for k in ans:
  print(k)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
bc=[list(map(int,input().split())) for i in range(q)]
s=0
for i in range(n):
    s+=a[i]
for i in range(q):
    res=a.count(bc[i][0])
    s+=(bc[i][1]-bc[i][0])*res
    a=[bc[i][1] if ai==bc[i][0] else ai for ai in a]
    print(s)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
li_a = list(map(int, input().split()))
dic_a = {}

for a in li_a:
    dic_a[a] = dic_a.get(a, 0) + 1

q = int(input())
li_bc = list()

for i in range(q):
    li_bc.append(tuple(map(int, input().split())))

for l in li_bc:
    b = l[0]
    c = l[1]
    if b in dic_a.keys():
        dic_a[c] = dic_a.get(c, 0) + dic_a.get(b, 0)
        dic_a[b] = 0
    sum = 0
    for key, val in dic_a.items():
        sum += (key * val)
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
li_a = list(map(int, input().split()))
q = int(input())
li_bc = list()

for i in range(q):
    li_bc.append(tuple(map(int, input().split())))

for l in li_bc:
    li_a = [a if a != l[0] else l[1] for a in li_a]
    print(sum(li_a))

Submission status: Time Limit Exceeded

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

for j in range(q):
    for k in range(n):
        if a[k] == b[j]:
            a[k] = c[j]
    print(sum(a))

Submission status: Time Limit Exceeded

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

B=[]
C=[]
for i in range(q):
    b1,c1=map(int, input().split())
    B.append(b1)
    C.append(c1)

# print(A)
for i in range(q):
    A=[ C[i] if B[i]==a else a for a in A ]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
n0 = int(1e5)+1
x = [0]*n0

n = int(input())
a0 = list(map(int,input().split()))
q = int(input())
bcs = []
for i in range(q):
    bcs.append(list(map(int,input().split())))

for a in a0:
    x[a] += 1

for bc in bcs:
    x[bc[1]] += x[bc[0]]
    x[bc[0]] = 0
    sum0 = 0
    for i in range(n0):
        sum0 += i*x[i]
    print(sum0)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
q = int(input())
bcs = []
for i in range(q):
    bcs.append(list(map(int,input().split())))
for bc in bcs:
    a = [bc[1] if i == bc[0] else i for i in a]
    print(sum(a))

Submission status: Time Limit Exceeded

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

S=[]
S.append(sum(A))

for i in range(Q):
    b,c=map(int,input().split())
    num=A.count(b)
    for j in range(num):
        index_A=A.index(b)
        A[index_A]=c
   
    S.append(S[i]+num*(c-b))

for i in range(1,1+Q):
    print(S[i])

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()[:n]))
q=int(input())
b=[]
c=[]
for i in range(q):
    l= list(map(int,input().split()))
    b.append(l[0])
    c.append(l[1])    
res_sum=[]
for i in range(q):
    for j in range(len(a)):
        if a[j]==b[i]:
            a[j]=c[i]
    res_sum.append(sum(a))
for i in res_sum:
    print(i)

Submission status: Time Limit Exceeded

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

B=[]
C=[]
S=[]
for i in range(Q):
    b,c=map(int,input().split())
    num=A.count(b)
    if num!=0:
        for j in range(num):
            index_A=A.index(b)
            A[index_A]=c
   
    S.append(sum(A))

for i in range(Q):
    print(S[i])

Submission status: Time Limit Exceeded

Solution code:
import bisect,collections,copy,heapq,itertools,math,string
import numpy as np
from numba import njit
import sys
sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
_A = LI()
A = np.array(_A,dtype='int')
Q = I()
BC = [LI() for _ in range(Q)]
# print(BC)

def convert(a,mapping):
    for i in range(N):
        if a[i]==mapping[0]:
            a[i]=mapping[1]
    return a

for i in range(Q):
    A = convert(A,BC[i])
    print(A.sum())

# def convert(n):
#     return d[n]

# d = {1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9}
# for i in range(Q):
#     for key, value in d.items():
#         if BC[i][0] == value:
#             d[key]=BC[i][1]
#     ans = np.array(list(map(convert,A)),dtype='int')
#     print(ans.sum())


# ans = list(map(convert,A))
# for item in ans:
#     print(item)
# print('\n'.join(str(ans)))

#A,B = zip(*AB)
#Ap = np.array(A)
#C = np.zeros(N + 1)

# if ans:
#     print('Yes')
# else:
#     print('No')

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
def stupid():
    N=input()
    N=int(N)
    A=input()
    A=A.split(" ")
    A=[int(i) for i in A]
    A=np.array(A,dtype=np.uint64)
    Q=input()
    Q=int(Q)
    BC=np.empty((Q,2))
    for i in range(Q):
        buf=input().split(" ")
        buf=[int(i) for i in buf]
        BC[i]=buf
    for i in range(Q):
        A[BC[i][0]==A]=BC[i][1]
        print(np.sum(A))

if __name__ == "__main__":
    stupid()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
ar=list(map(int,input().split()))
di={}
for i in range(n):
    if ar[i] not in di:
        di[ar[i]]=1
    else:
        di[ar[i]]+=1
q=int(input())
for i in range(q):
    b,c=map(int,input().split())
    if(b in di):
        x=di[b]
        del di[b]
        if(c in di):
            di[c]+=x
        else:
            di[c]=x
    s=0
    for j in di.keys():
        s+=j*(di[j])
    print(s)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = [int(x) for x in input().split()]
m = {x : 0 for x in l}

for x in l:
  m[x] += 1

for i in range(int(input())):
  b, c = map(int, input().split())
  if b not in m:
    print(sum([k * v for k, v in m.items()]))
    continue
  
  cnt = m[b]
  m[b] = 0
  m[c] = m[c] + cnt if c in m else cnt
  
  print(sum([k * v for k, v in m.items()]))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A_list = list(map(int, input().split()))
q = int(input())

s = [0]*q

for i in range(0, q):
    b, c = map(int, input().split())
    for j in range(0, n):
        if A_list[j] == b:
            A_list[j] = c
        s[i] = s[i] + A_list[j]
    print(s[i])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
q = int(input())
b = []
c = []
for i in range(q):
    ans = 0
    x,y = map(int,input().split())
    for j in range(n):
        if a[j] == x:
            a[j] = y
        ans += a[j]
    print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

m = 10**5+1
nums = [0]*m
for i in a: nums[i] += 1

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

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
input()
a=map(int,stdin.readline().rstrip().split())
b=int(input())
for i in range(b):
  n,m=map(int,stdin.readline().rstrip().split())
  a=[m if i==n else i for i in a]
  print(sum(a))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
lst = [int(i) for i in input().split()]
dic = {}
for i in range(n):
  key = lst[i]
  if key not in dic:
    dic[key] = 0
  dic[key] += 1
#print(dic)
m = int(input())
for _ in range(m):
  sum = 0
  lst_s = list(map(int, input().split()))
  number = dic.get(lst_s[0])
  if number is not None:
    if lst_s[1] in dic:
      dic[lst_s[1]] += number
    else:
      dic[lst_s[1]] = number
    dic.pop(lst_s[0])
  for key, value in dic.items():
    sum += key * value
  print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int, input().split()))
A = sorted(A)
q = int(input())
lst = [0] * q
for i in range(q):
    lst[i] = list(map(int, input().split()))
for i in range(q):
    for j in range(n):
        if A[j] > lst[i][0]:
            break
        elif A[j] == lst[i][0]:
            A[j] = lst[i][1]
    print(sum(A))
    A = sorted(A)

Submission status: Time Limit Exceeded

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

    a_dict = {}
    for i in A:
        if i in a_dict:
            a_dict[i] += 1
        else:
            a_dict[i] = 1

    for b, c in BC:
        if b in a_dict:
            if c in a_dict:
                a_dict[c] += a_dict.pop(b)
            else:
                a_dict[c] = a_dict.pop(b)
        print(sum([k * v for k, v in a_dict.items()]))

if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
B = []
C = []
l = [list(map(int, input().split())) for _ in range(Q)]
for i in range(Q):
    B.append(l[i][0])
for i in range(Q):
    C.append(l[i][1])

for i in range(Q):
    for j in range(N):
        if A[j] == B[i]:
            A[j] = C[i]
    print(sum(A))

Submission status: Time Limit Exceeded

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

for i in range(Q):
    l = list(map(int, input().split()))
    S = 0
    for j in range(N):
        if A[j] == l[0]:
            A[j] = l[1]
        S += A[j]
    print(S)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    l = list(map(int, input().split()))
    for j in range(N):
        if A[j] == l[0]:
            A[j] = l[1]
    S = sum(A)
    print(S)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
B=[]
C=[]
for i in range(Q):
    b,c=input().split()
    B.append(int(b))
    C.append(int(c))
S=0
D=[0]
for i in range(1,10**5+1):
    k=A.count(i)
    D.append(k)
    S=S+i*k
for i in range(Q):
    n=D[B[i]]
    S=S+n*(C[i]-B[i])
    D[C[i]]=D[C[i]]+n
    print(S)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    for j in range(N):
        if A[j] > BL[i]:
            break
        elif A[j] == BL[i]:
            A[j] = CL[i]
    print(sum(A))
    A = sorted(A)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
A = np.array((list(map(int, input().split()))))
Q = int(input())
BL = []
CL = []
for _ in range(Q):
    B, C = map(int, input().split())
    BL.append(B)
    CL.append(C)

for i in range(Q):
    A = np.where(A == int(BL[i]), int(CL[i]), A)
    print(sum(A))

Submission status: Time Limit Exceeded

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

for i in range(Q):
    A = [CL[i] if j == BL[i] else j for j in A]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:

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


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

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

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

Submission status: Time Limit Exceeded

Solution code:
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())

num=[0]*(10**5)
for i in range(10**5):
  num[i]=A.count(i+1)
sum=sum(A)
for j in range(Q):
  sum+=num[B[j]-1]*(C[j]-B[j])
  print(sum)
  num[C[j]-1]+=num[B[j]-1]
  num[B[j]-1]=0

Submission status: Time Limit Exceeded

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

A_dict = {}
for a in A:
    A_dict[a] = A.count(a)

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

Submission status: Time Limit Exceeded

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

for b, c in zip(B, C):
    A = list(map(lambda x: c if x == b else x, A))
    ans = sum(A)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,a,q,*bc,=open(0).read().rstrip().split('\n')
N=int(N)
A=[int(s) for s in a.split()]
Q=int(q)
BC=[list(map(int,s.split())) for s in bc]
S=[0]*Q
for i in range(Q):
    for j in range(N):
        if A[j]==BC[i][0]:
            A[j]=BC[i][1]
    S[i]=sum(A)
for k in S:
    print(k)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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


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

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

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

main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
B = []
C = []
S = []
for i in range(Q):
    b,c = map(int,input().split())
    B.append(b)
    C.append(c)
for j in range(Q):
    X = []
    for x in range(len(A)):
        if A[x] == B[j]:
            X.append(x)
            A[x] = ''
    for k in range(len(X)):
        A[X[k]] = C[j]
    s = sum(A)
    S.append(s)
for y in range(len(S)):
    print(S[y])

Submission status: Time Limit Exceeded

Solution code:
def Convert(A, B, C):
    for i in range(len(A)):
        if A[i] == B:
            A[i] = C
    return sum(A)

N = int(input())
A = list(map(int,input().split()))
Q = int(input())
S = []
for i in range(Q):
    B, C = map(int, input().split())
    S.append(Convert(A, B, C))

for i in range(Q):
    print(S[i])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
A = input().split()
q = int(input())
dict_A = Counter(A)
sum_list = 0
for key, value in dict_A.items():
        sum_list += int(key) * value

for i in range(q):
    b, c = map(int, input().split())
    if str(b) not in dict_A:
        print(sum_list)
    else:
        dict_A[str(c)] += dict_A[str(b)]
        dict_A[str(b)] = 0
        sum_list = 0
        for key, value in dict_A.items():
            sum_list += int(key) * value
        print(sum_list)

Submission status: Time Limit Exceeded

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

    for i in range(Q):
        b, c = map(int, input().split())
        B.append(b)
        C.append(c)
    for i, b in enumerate(B):
        for j, a in enumerate(A):
            if b==a:
                A[j]=C[i]
        print(sum(A))

Submission status: Time Limit Exceeded

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

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

for i in range(max(a)+1):
	a_cnt[i]=a.count(i)

a_goukei=sum(a)

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
q = int(input())
bc=[]
for i in range(q):
    bc.append([int(x) for x in input().split()])
a_cnt=[0]*(10**7)
for i in range(max(a)+1):
	a_cnt[i]=a.count(i)

a_goukei=sum(a)

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

Submission status: Time Limit Exceeded

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

for gyou in range(q):
	a=[bc[gyou][1] if i==bc[gyou][0] else i for i in a]
	print(sum(a))

Submission status: Time Limit Exceeded

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


for gyou in range(q):
    a = [bc[gyou][1] if i==bc[gyou][0] else i for i in a]
    print(sum(a))

Submission status: Time Limit Exceeded

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

for i in range(q):
    o = list(map(int, input().split()))
    for j in range(n):
        if a[j] == o[0]:
            a[j] = o[1]
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int,input().split()))
Q = int(input())
result = 0
for i in a:
    result += i
for j in range(Q):
    b,c = map(int,input().split())
    while b in a:
        a[a.index(b)] = c
        result += c-b
    print(result)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = sorted(list(map(int, input().split())))
q = int(input())
l1 = [0]*(10**5+10)
l2 = [0]*(10**5+10)
index = 0
l1[0] = a[0]
l2[0] = 1
for i in a[1:]:
    if i == l1[index]:
        l2[index] += 1
    else:
        index += 1
        l1[index] = i
        l2[index] = 1
s = sum(a)
for _ in range(q):
    b, c = map(int, input().split())

    i = 0
    while i < index + 2:
        if (b == l1[i]):
            break
        i += 1
    if i > index:
        print(s)
        continue

    j = 0
    while j < index + 2:
        if(c == l1[j]):
            break
        j += 1
    if j > index:
        index = j
        l1[index] = c
    s += (c - b) * l2[i]
    l2[j] += l2[i]
    l2[i] = 0
    print(s)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
B=[]
C=[]
for i in range(Q):
    b,c=input().split()
    B.append(int(b))
    C.append(int(c))
S=0
D={}
for i in range(1,10**5+1):
    k=A.count(i)
    D[i]=k
    S=S+i*k
for i in range(Q):
    n=D[B[i]]
    S=S+n*(C[i]-B[i])
    D[C[i]]=D[C[i]]+n
    print(S)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
B=[]
C=[]
for i in range(Q):
    b,c=input().split()
    B.append(int(b))
    C.append(int(c))
Set=set(A)
S=0
D={}
for s in Set:
    D[s]=A.count(s)
    S=S+s*D[s]
for i in range(Q):
    if B[i] in D:
        n=D[B[i]]
        S=S+n*(C[i]-B[i])
        del D[B[i]]
        if C[i] in D:
            D[C[i]]=n+D[C[i]]
        else:
            D[C[i]]=n
    print(S)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
B=[]
C=[]
for i in range(Q):
    b,c=input().split()
    B.append(int(b))
    C.append(int(c))
Set=set(A)
D={}
for s in Set:
    D[s]=A.count(s)
S=sum(A)
for i in range(Q):
    if B[i] in D:
        n=D[B[i]]
        S=S+n*(C[i]-B[i])
        del D[B[i]]
        if C[i] in D:
            D[C[i]]=n+D[C[i]]
        else:
            D[C[i]]=n
    print(S)

Submission status: Time Limit Exceeded

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

for i in range(Q):
  B,C = map(int,input().split())
  M = A.count(B)
  S -= M*B
  S += M*C
  A = [i for i in A if i != "B"]
  A = A + [C]*M
  print(S)

Submission status: Time Limit Exceeded

Solution code:

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

x = {a:A.count(a) for a in set(A)}

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
#a = list(map(int,input().split()))
a = input()
q = int(input())
for i in range(q):
    b,c = input().split()
    a = a.replace(b,c)
    al = list(map(int,a.split()))
    #print(al,type(al))
    su = sum(al)
    print(su)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
q = int(input())
for _ in range(q):
    b, c = list(map(int, input().split()))
    a_list.sort()
    index = []
    for i,j in enumerate(a_list):
        if j>b:
            break
        elif j==b:
            a_list[i]=c
    print(sum(a_list))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
for i in range(Q):
    b, c = map(int, input().split())
    A = [c if t == b else t for t in A]
    print(sum(A))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:

n=int(input())
a=list(map(int,input().split()))
q=int(input())
bc=[list(map(int,input().split())) for i in range(q)]
num=[a.count(i+1) for i in range(10**5)]
sum=sum(a)
for i in range(q):
    x=bc[i][0]
    y=bc[i][1]
    sum+=(y-x)*num[x-1]
    num[y-1]+=num[x-1]
    num[x-1]=0
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
def fun(d):
	cnt = 0
	for i in d:
		cnt+= i*d[i]
	return cnt

from collections import defaultdict
d = defaultdict(int)
n = int(input())
A = list(map(int,input().split()))
for i in range(n):
	d[A[i]]+=1
q = int(input())
for i in range(q):
	b,c = map(int,input().split())
	if b in d:
		d[c]+=d[b]
		d[b]=0
	print(fun(d))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
bc=[list(map(int,input().split())) for i in range(q)]
sum=sum(a)
for i in range(q):
    x=bc[i][0]
    y=bc[i][1]
    c=a.count(x)
    if x in a:
      ind=[j for j,m in enumerate(a) if m == x]
      sum+=(y-x)*c
      for j in ind:
        a[j]=bc[i][1]
    else:
      pass
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
bc=[list(map(int,input().split())) for i in range(q)]
sum=sum(a)
for i in range(q):
    c=a.count(bc[i][0])
    if bc[i][0] in a:
      ind=[j for j,x in enumerate(a) if x == bc[i][0]]
      sum+=(bc[i][1]-bc[i][0])*c
      for j in ind:
        a[j]=bc[i][1]
    else:
      pass
    print(sum)

Submission status: Time Limit Exceeded

Solution code:
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

N=int(input())
A=list(map(int,input().split()))
Q=int(input())
List=[i for i in range(10**5+1)]
Uf=UnionFind(10**5+1)
for i in range(Q):
    B,C=map(int,input().split())
    List[B]=C
    Uf.union(B,C)
    for i in Uf.members(B):
        index=i
        while index!=List[index]:
            index=List[index]
            if index==i:
                break
            # print(index)
            # print(List)
        List[i]=index
    ChangedA=[List[a] for a in A]
    print(sum(ChangedA))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
List=[i for i in range(10**5+1)]
Used=[]
for i in range(Q):
    B,C=map(int,input().split())
    List[B]=C
    Used.append(B)
    for i in Used:
        index=i
        while index!=List[index]:
            index=List[index]
            if index==i:
                break
            # print(index)
            # print(List)
        List[i]=index
    ChangedA=[List[a] for a in A]
    print(sum(ChangedA))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
List=[i for i in range(10**5+1)]
for i in range(Q):
    B,C=map(int,input().split())
    List[B]=C
    for i in  range(1,len(List)):
        index=i
        while index!=List[index]:
            index=List[index]
            if index==i:
                break
            # print(index)
            # print(List)
        List[i]=index
    ChangedA=[List[a] for a in A]
    print(sum(ChangedA))

Submission status: Time Limit Exceeded

Solution code:
def csum(c):
  r = 0
  for index, item in enumerate(c):
    r += index * item
  return r

n = int(input())
l= list(map(int,input().split()))
c = [0] * 100001
for i in l:
  c[i] += 1
n = int(input())
for i in range(0, n):
  be,af = map(int,input().split())
  c[af] += c[be]
  c[be] = 0
  print(csum(c))

Submission status: Time Limit Exceeded

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

import collections
a = collections.Counter(a)
for i in range(q):
    b,c = list(map(int,input().split()))
    d = a[b]
    a[b] = 0
    a[c] += d
    print(sum([key*value for key,value in a.items()]))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
b, c = [None] * q, [None] * q
ans = [0] * q
for i in range(q):
    b[i], c[i] = list(map(int, input().split()))
    al = list(map(lambda x: c[i] if x == b[i] else x, a))
    ans[i] = sum(al)
    a = al
for i in ans:
    print(i)