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())
B,C=map(int, input().split())
rep=[i for i, x in enumerate(A) if x == B]
for i in range(len(rep)):
    A[rep[i]]=C
ans=0
for i in range(N):
    ans+=A[i]
print(ans)

for _ in range(Q-1):
    B,C=map(int, input().split())
    rep=[i for i, x in enumerate(A) if x == B]
    for i in range(len(rep)):
        A[rep[i]]=C
    ans+=(C-B)*len(rep)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())	
A_list = list(map(int,input().split()))
Q = int(input())
BC_dic = [list(map(int,input().split())) for i in range(Q)]
for BC in BC_dic:
    A_list = [BC[1] if A == BC[0] else A for A in A_list]
    print(sum(A_list))

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict


def main():
    number = int(input())
    numcount = defaultdict(int)
    for x in input().split():
        numcount[int(x)] += 1
    queries = int(input())
    for _ in range(queries):
        before, after = [int(x) for x in input().split()]
        tmp = numcount[before]
        numcount[before] -= tmp
        numcount[after] += tmp

        print(sum(num * count for num, count in numcount.items()))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
p = input().split()
p_int = [int(n) for n in p]
Q = int(input())
for i in range(Q):
  sum = 0
  B, C = map(int, input().split())
  for n in range(N):
    if p_int[n] == B:
      p_int[n] = C
    sum += p_int[n]
  print(sum)

Submission status: Time Limit Exceeded

Solution code:
def su(l):
    return sum(l)
t=int(input())
l=[int(x) for x in input().split(" ")]
t=int(input())
while t>0:
   s=0
   a,b= [int(x) for x in input().split(" ")]
   l[:]=[int(x) if x!=a else b for x in l]
   s=su(l)
   t -= 1
   print(s)

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    sys.setrecursionlimit(10**9)
    input = sys.stdin.readline
    from collections import Counter

    N = int(input())
    A = list(map(int, input().split()))
    CA = Counter(A)
    Q = int(input())
    for i in range(Q):
        b, c = map(int, input().split())
        CA[c] += CA[b]
        CA[b] = 0
        sumA = 0
        for key, value in CA.items():
            sumA += key*value
        print(sumA)

main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
a = list(map(int,input().split()))
a = np.array(a)
q = int(input())
import collections
co = collections.Counter(a)
# print(co)
for i in range(q):
    b,c = map(int,input().split())
    if b in co.keys():
        co[c]+=co[b]
        co.pop(b)
    s=0
    for k,v in co.items():
        s+=k*v
    print(s)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
lst=list(map(int,input().split()))[:N]
Q=int(input())
for i in range(Q):
    x,y=map(int,input().split())
    for i in range(len(lst)):
        if lst[i]==x:
            lst[i]=y
    print(sum(lst))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
num_list = list(map(int,input().split()))
q = int(input())

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int, input().split()))
Q = int(input())
k = [list(map(int, input().split())) for i in range(Q)]

for i in range(Q):
  A = [int( str(s).replace( str(k[i][0]),str(k[i][1]) ) ) for s in A]
  print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
from numba import njit



readline = sys.stdin.readline
read = sys.stdin.read


@njit('(i8[:], i8, i8[:])',cache=True)
def loop(a,q,bc):
    for i in range(q):
        a = np.where(a == bc[i*2], bc[i*2+1], a)
        print(np.sum(a))

n = int(readline())
a = np.array(readline().split(), dtype=np.int64)
q = int(readline())
bc = np.array(read().split(), dtype=np.int64)
loop(a, q, bc)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())

l = []
for i in range(Q):
    B, C = map(int, input().split())
    A = [C if i == B else i for i in A]
    l.append(sum(A))

for j in range(Q):
    print(l[j])

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(map(int, input().split()))
Q = int(input())
BC = [list(map(int, input().split())) for _ in range(Q)]
S = [0]*Q

for i, bc in enumerate(BC):
  b = bc[0]
  c = bc[1]
  a = [c if i == b else i for i in a]
  S[i] = sum(a)

[print(i) for i in 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):
    
    b,c = map(int, input().split())
    
    for i in range(len(a)):
        if a[i] == b:
            a[i] = c

    print(sum(a))

Submission status: Time Limit Exceeded

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

for i in range(q):
  b = b_c[i][0]
  c = b_c[i][1]
  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=input()
q=int(input())
ans=[]
for _ in range(q):
    b,c = input().split(' ')
    a = a.replace(b,c)
    ans.append(sum(map(int,a.split(' '))))
for s in ans:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

if __name__ == '__main__':
    n = int(sys.stdin.readline().rstrip())
    A = list(map(lambda x: int(x), sys.stdin.readline().split()))
    q = int(sys.stdin.readline().rstrip())
    dic = {}
    for a in A:
        dic[a] = dic.get(a, 0) + 1

    for i in range(q):
        b, c = list(map(lambda x: int(x), sys.stdin.readline().split()))

        dic[c] = dic.get(c, 0) + dic.get(b, 0)
        dic[b] = 0

        summ = 0
        keys = list(dic.keys())
        values = list(dic.values())
        print(np.array(keys).dot(np.array(values)))

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())
lst = list(map(int, input().split()))
q = int(input())
lst_c = lst.copy()
for i in range(q):
    b, c = map(int, input().split())
    lst_c = [c if i == b else i for i in lst_c.copy()]
    print(sum(lst_c))

Submission status: Time Limit Exceeded

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

b = [0 for i in range(q)]
c = [0 for i in range(q)]
ret = []
for i in range(q):
    b[i],c[i] = list(map(int,input().split()))
    a = [c[i] if x == b[i] else x for x in a]
    ret += [str(sum(a))]

print('\n'.join(ret))

Submission status: Time Limit Exceeded

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for q in range(Q):
    B,C = map(int,input().split())
    for a in range(len(A)):
        if A[a] == B:
            A[a] = C
    result.append(sum(A))
[print(i) for i in result]

Submission status: Time Limit Exceeded

Solution code:
t = int(input())
arr = list(map(int, input().split()))
Sum = sum(arr)
t1 = int(input())
ans = []
for i in range(t1):
  b,c = list(map(int, input().split()))
  arr = [c if x==b else x for x in arr]
  print(sum(arr))

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 j == B else j for j in A]
  ans = sum(A)
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

def main():
  # Xはもっとも近い整数，Nは与えられるP
  #X,N = map(int, input().split())
  N = input()

  # listを取得
  arr = list(map(int, input().split()))

  Q = int(input())

  li =  []
  for i in range(Q):
    lis = list(map(int, input().split()))
    li.append(lis)
  
  for i in range(Q):
    ans = 0
    one = li[i][0]
    two = li[i][1]
    for j in range(len(arr)):
      if one == arr[j]:
        arr[j] = two
      ans = ans + arr[j]
    print(ans)
  
  # 正常終了
  sys.exit()

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

n = int(input())
numbers = input().split(" ")
a = [int(x) for x in numbers]
q = int(input())

r = []


for i in range(q):

  numbers = input().split(" ")
  b = int(numbers[0])
  c = int(numbers[1])
  s = 0

  # a.sort()
  for i in range(n):
    if a[i] == b:
      a[i] = c
    s += a[i]
  r.append(s)

for x in r:
  print(x)

Submission status: Time Limit Exceeded

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

dictA = {}

for z in A:
    dictA.setdefault(z, 0)
    dictA[z] += 1


BC = [list(map(int, input().split())) for i in range(Q)]
for x in BC:
    if x[0] in dictA:
        dictA.setdefault(x[1], 0)
        dictA[x[1]] += dictA[x[0]]
        del dictA[x[0]]

    sumA = 0
    for k, v in dictA.items():
        sumA += k * v
    print(sumA)

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() )
listA = []
for q in range(Q):
    listA.append(sum(A))
    for i in range(len(A)):
        if A[i] == B[q]:
            A[i] = C[q]
listA.append(sum(A))
for a in range(1,len(listA)):
    print(listA[a])

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
A.sort()
Q=int(input())
for x in range(Q):
  B,C=map(int,input().split())
  for y in range(N):
    if A[y]>B:
      A.sort()
      break
    if A[y]==B:
      A[y]=C
  print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
import sys
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
BC = []
for q in range(Q):
    bc = list(map(int, input().split()))
    BC.append(bc)


for bc in BC:
    ind = [i for i, x in enumerate(A) if x==bc[0]]
    for i in ind:
        A[i] = bc[1]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]
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 i in BC:
    if i[0] in A:
        indexlist=my_index_multi(A,i[0])
        for j in indexlist:
            A[j]=i[1]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
x=sum(a)
u,v=[],[]
for i in a:
    if not i in u:
        u+=[i]
        v+=[1]
    else:
        v[u.index(i)]+=1
q=int(input())
for i in range(q):
    b,c=map(int,input().split())
    for j in range(len(u)):
        if u[j] == b:
            u[j]=c
            x+=v[j]*(c-b)
    print(x)

Submission status: Time Limit Exceeded

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

for i in range(q):
    b,c = map(int,input().split())
    indexes = [i for i, x in enumerate(a_li) if x == b]
    for i in indexes:
        a_li[i] = c
    print(sum(a_li))

Submission status: Time Limit Exceeded

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

        a = l[:]


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
A = list(map(int, input().split()))
q = int(input())
Acount = Counter(A)
Adic = dict(Acount)
# print(Adic)
s = 0
for key, value in Adic.items():
    s += key*value
for i in range(q):
    b, c = map(int, input().split())
    if not b in set(Adic.keys()):
        Adic[b] = 0
    s += (c - b) * Adic[b]
    if not c in set(Adic.keys()):
        Adic[c] = 0
    Adic[c] += Adic[b]
    Adic[b] = 0
    print(s)

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())
bc = [list(map(int, input().split())) for _ in range(q)]

for i in range(q):
  a = np.where(a==bc[i][0],bc[i][1],a)
  print(np.sum(a))

Submission status: Time Limit Exceeded

Solution code:
N = int(input().strip())
A = list(map(int, input().strip().split()))
Q = int(input().strip())
grid = []
for i in range(Q):
    array = list(map(int, input().strip().split()))
    grid.append(array)

for i in range (Q):
    for j in range (N):
        if A[j]==grid[i][0]:
            A[j]=int(grid[i][1])

    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
given_list = [str(i) for i in input().split()]
Q = int(input())

for _ in range(Q):
    query = input().split()
    query = [str(i) for i in query]

    B = query[0]
    C = query[1]

    given_list_replased = [a.replace(B, C) for a in given_list]
    given_list_replased = [int(i) for i in given_list_replased]
    print(sum(given_list_replased))
    
    given_list = [str(i) for i in given_list_replased]

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for _ in range(q):
    b, c = map(int, input().split())
    if b not in a:
        l.append(sum(a))
        continue
    a = [c if e == b else e for e in a]
    l.append(sum(a))

for a in l:
    print(a)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

s = []
for i in range(Q):
    BC = list(map(int, input().split()))
    for j in range(len(A)):
        if (A[j] == BC[0]):
            A[j] = BC[1]
    s.append(sum(A))

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

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())

for i in range(Q):
    if B[i] in A:
        while B[i] in A:
            A[A.index(B[i])] = C[i]
    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 _ in range(q)]

for i in range(q):
    # print(bc[i][0])
    # index = a.index(bc[i][0])
    index = [j for j, x in enumerate(a) if x == bc[i][0]]
    # print(index)
    for k in index:
        a[k] = bc[i][1]
    # a[index] = bc[i][1]
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(str,input().split()))
q = int(input())
for i in range (q):
    Sum = 0
    b,c = map(str,input().split())
    a.sort()
    a = [s.replace(b,c) for s in a]
    for i in a:
        i= int(i)
        Sum +=i
    print(Sum)

Submission status: Time Limit Exceeded

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

from  collections import Counter

dict_a = Counter(a)

for i in range(q):
  dict_a[operation[i][1]] += dict_a[operation[i][0]]
  dict_a[operation[i][0]] = 0
  s = 0
  for (k, v) in dict_a.items():
    s += k*v
  print(s)

Submission status: Time Limit Exceeded

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


def change(list,before,after):
    for j in range(n):
        if list[j] == before:
            list[j] = after
    print(sum(list))
    return list


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

Submission status: Time Limit Exceeded

Solution code:
import sys

if __name__ == '__main__':
    n = int(sys.stdin.readline().rstrip())
    A = list(map(lambda x: int(x), sys.stdin.readline().split()))
    q = int(sys.stdin.readline().rstrip())
    dic = {}
    for a in A:
        dic[a] = dic.get(a, 0) + 1

    for i in range(q):
        b, c = list(map(lambda x: int(x), sys.stdin.readline().split()))

        dic[c] = dic.get(c, 0) + dic.get(b, 0)
        dic[b] = 0

        summ = 0
        for k, v in dic.items():
            summ += k*v
        print(summ)

Submission status: Time Limit Exceeded

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

aDict = dict()
for aElement in A:
    if aElement not in aDict:
        aDict[aElement] = 0
    aDict[aElement] += 1

for b, c in zip(B, C):
    if c not in aDict:
        aDict[c] = 0
    aDict[c] += aDict.pop(b, 0)
    print(sum([k * v for k, v in aDict.items()]))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
li1 = list(map(int, input().split()))
q = int(input())
li2 = list(input().split() for x in range(q))
for i in li2:
    if int(i[0]) in li1:
        li3 = [index for index, value in enumerate(li1) if value == int(i[0])]
        for j in li3:
            li1[j] = int(i[1])
        print(sum(li1))
    else:
        print(sum(li1))

Submission status: Time Limit Exceeded

Solution code:
N = int(input().strip())

A = {}
for a in [int(x) for x in input().strip().split(" ")]:
    A.setdefault(a, 0)
    A[a] += 1

Q = int(input().strip())

for q in range(Q):
    B, C = [int(x) for x in input().strip().split(" ")]
    if B in A:
        A.setdefault(C, 0)
        A[C] += A[B]
        del A[B]
    print(sum([k * v for k, v in A.items()]))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
n_data = {i: A.count(i) for i in range(1, 10 ** 5 + 1)}
ans = sum(A)
for _ in range(Q):
  b, c = map(int, input().split())
  ans += (c - b) * n_data[b]
  n_data[c] += n_data[b]
  n_data[b] = 0
  print(ans)

Submission status: Time Limit Exceeded

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

for i in range(Q):
  A_string = ",".join(A)
  A_new = A_string.replace(B[i],C[i])
  A =  A_new.split(",")
  int_A =map((lambda x: int(x)), A)
  print(sum(int_A))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
B=[]
for _ in range(Q):
    B.append(list(map(int,input().split())))
for i in range(Q):
    for j in range(N):
        if A[j]==B[i][0]:
            A[j]=B[i][1]
    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):
    b, c = map(int, input().split())
    B.append(b)
    C.append(c)

# print(N)
# print(A)
# print(Q)
# print(B)
# print(C)

ans = []

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

    ans_item = sum(A)
    ans.append(ans_item)


# print(ans)
for item in ans:
    print(item)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = input()
a = np.array(list(map(int, input().split())))
q =int(input())
for i in range(q):
  b,c = map(int, input().split())
  a = np.where(a==b,c,a)
  s = sum(a)
  print(s)

Submission status: Time Limit Exceeded

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

replace = []
for _ in range(q):
  m, n = [int(i) for i in input().split()]
  replace.append((m,n))

l = len(a)
res = sum(a)
for r in replace:
  for i in range(l): 
    if a[i] == r[0]:
      res += r[1] - r[0]
      a[i] = r[1]
  print(res)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
from sys import setrecursionlimit
from collections import Counter
setrecursionlimit(10 ** 7)

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

q = int(input())
cc = Counter(a)

for _ in range(q):
    b,c = map(int,input().split())
    cc[c] += cc[b]
    cc[b] -= cc[b]
    su = 0
    for i,j in cc.items():
        su += i*j
    print(su)

Submission status: Time Limit Exceeded

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

for _ in range(q):
    b, c = map(int, input().split())
    if b not in a:
        l.append(a)
        continue
    a = [c if e == b else e for e in a]
    l.append(a)

for a in l:
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

target_list = map(int, input().split())
result = []

for i in range(int(input())):
    B, C = map(int, input().split())
    target_list = [C if i == B else i for i in target_list]
    result.append(sum(target_list))

for i in result:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]
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 i in BC:
    indexlist=my_index_multi(A,i[0])
    for j in indexlist:
        A[j]=i[1]
    print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
def sums(l):
	s = 0
	for v in l:
		s += v
	return s

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

for i in range(n):
	if a[i] in ind:
		ind[a[i]].append(i)
	else:
		ind[a[i]] = [i]

q = int(input())
for _ in range(q):
	b, c = map(int, input().split())
	if b not in ind:
		print(sums(a))
		continue
	for i in ind[b]:
		a[i] = c
		if c in ind:
			ind[c].append(i)

		else:
			ind[c] = [i]
			
	print(sums(a))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input())
a=list(map(int,input().split()))
a=np.array(a)
q=int(input())
for i in range(q):
    b,c=map(int,input().split())
    a[a==b]=c
    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())
  if b in A:
    A = [c if i  == b else i for i in A]
    print(sum(A))
  else:
    print(sum(A))

Submission status: Time Limit Exceeded

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

for i in range(q):
    a,b = map(int, input().split())
    al = [b if j == a else j for j in al ]
    print(sum(al))

Submission status: Time Limit Exceeded

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

dictA = {}

for z in A:
    dictA.setdefault(z, 0)
    dictA[z] += 1


BC = [list(map(int, input().split())) for i in range(Q)]
for x in BC:
    if x[0] in dictA:
        if x[1] in dictA:
            dictA[x[1]] += dictA[x[0]]
        else:
            dictA[x[1]] = dictA[x[0]]
        del dictA[x[0]]

    sumA = 0
    for k, v in dictA.items():
        sumA += k * v
    print(sumA)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
def my_index(l, x):
    if x in l:
        return l.index(x)
    else:
        return -1


N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = [0 for _ in range(Q)]
C = [0 for _ in range(Q)]
for i in range(Q):
  B[i], C[i] = map(int, input().split())
  
for i in range(Q):
  index = my_index(A, B[i])
  while(index != -1):
    A[index] = C[i]
    index = my_index(A, B[i])

  print(np.sum(A))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
q = int(input())
xy = [map(int, input().split()) for _ in range(q)]
x, y = [list(i) for i in zip(*xy)]
for j in range(q):
  a = [y[j] if i==x[j] 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())

baseline = sum(A)
seq = ''.join(str(n) for n in A)

for i in range(Q):
    b, c = input().split()
    n = seq.count(b)
    diff = int(c) - int(b)
    baseline += n * diff
    seq = seq.replace(b, c)

    print(baseline)

Submission status: Time Limit Exceeded

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

li = list(map(int,input().split()))
for i in range(n):
  member[li[i]] +=1

q = int(input())
for j in range(q):
  x,y = input().split()
  x=int(x)
  y=int(y)
  member[y]+=member[x]
  member[x]=0
  total = 0
  for k in range(10**5+1):
    total += k*member[k]
  print(total)

Submission status: Time Limit Exceeded

Solution code:
import sys

if __name__ == '__main__':
    n = int(sys.stdin.readline().rstrip())
    A = list(map(lambda x: int(x), sys.stdin.readline().split()))
    q = int(sys.stdin.readline().rstrip())
    B, C = [], []
    for i in range(q):
        b, c = list(map(lambda x: int(x), sys.stdin.readline().split()))
        B.append(b)
        C.append(c)

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

    for i in range(q):
        dic[C[i]] = dic.get(C[i], 0) + dic.get(B[i], 0)
        dic[B[i]] = 0

        summ = 0
        for k, v in dic.items():
            summ += k*v
        print(summ)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
seq = list(map(int,input().split()))
Q = int(input())
for i in range(Q):
    bi, ci = map(int,input().split())
    seq = [ci if i == bi else i for i in seq]
    print(sum(seq))

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())
for _ in range(Q):
    b, c = list(map(int, input().split()))
    A[np.where(A==b)] = c
    A = np.sort(A)
    print(np.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())
	a=[c if i==b 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 = []
for i in range(Q):
    B, C = map(int,input().split())
    while B in A:
        # A = A.replace(B, C)
        A[A.index(B)] = C

    S.append(sum(A))


for s in S:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

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 not in d.keys():
        print(s)
        continue
    cc = 0
    if c in d.keys():
        cc = d[c]
    d[c] = cc + d[b]
    del d[b]
    s = sum([k * v for k, v in d.items()])
    print(s)

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())
bc = [list(map(int, input().split())) for _ in range(Q)]

for i in range(Q):
    b, c = bc[i]
    a[a == b] = c
    print(np.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 i in range(Q):
  A = [BC[i][1] if j == BC[i][0] else j for j in A]
  print(sum(A))

Submission status: Time Limit Exceeded

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

    for _ in range(Q):
        B, C = map(int, input().split())
        for j in range(N) :
            if A[j] == B : 
                A[j] = C
        print(sum(A))

if __name__ == "__main__":
    main()

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=(int(x) for x in 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)]
for j in range(q):
    for i in range(n):
        if a[i]==bc[j][0]:
            a[i]=bc[j][1]
    print(sum(a))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

Q = int(input())

sumlist = []

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

for k in range(len(sumlist)):
    print(sumlist[k])