Buggy python submission (verdict: Runtime Error):

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

a.sort()
A = [0] * 100000
for i in a:
  A[i] += 1

ans  = 0
for i in range(len(A)):
  ans += i * A[i]  

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


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

A = [0] * 100001
for i in a:
  A[i] += 1

ans  = 0
for i in range(len(A)):
  ans += i * A[i]  

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

Buggy python submission (verdict: Runtime Error):

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

a.sort()
A = [0] * 100000
for i in a:
  A[i] += 1

ans  = 0
for i in range(len(A)):
  ans += i * A[i]  

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

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

A = [0] * 100001
for i in a:
  A[i] += 1

ans  = 0
for i in range(len(A)):
  ans += i * A[i]  

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

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import numpy as np
n = int(input())
a = list(map(int, input().split()))
q = int(input())
a.sort()
A = list(set(a))
CA = [0]*len(A)
for i,x in enumerate(A):
  l = bisect.bisect_left(a,x)
  r = bisect.bisect_right(a,x)
  CA[i] = r-l

def trans(A,b,c):
  for i,x in enumerate(A):
    if x == b:
      A[i] = c
  return A

A = np.array(A)
CA = np.array(CA) 
for i in range(q):
  b,c = list(map(int, input().split()))
  A = trans(A,b,c)
  ans = A * CA
  print(sum(ans))

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

A = [0] * 100001
for i in a:
  A[i] += 1

ans  = 0
for i in range(len(A)):
  ans += i * A[i]  

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

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
n = int(input())
a = list(map(int, input().split()))

A = {}
for i in range(n):
  A = dict(Counter(A) + Counter({a[i]:1}))

def sumA(A):
  ans = 0
  for x in A.keys():
    ans += x * A[x]
  return ans

q = int(input())
for i in range(q):
  b,c = list(map(int, input().split()))
  if b in A.keys():
    A = dict(Counter(A) + Counter({c:A[b]}))
    A = dict(Counter(A) - Counter({b:A[b]}))
  print(sumA(A))


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

A = [0] * 100001
for i in a:
  A[i] += 1

ans  = 0
for i in range(len(A)):
  ans += i * A[i]  

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

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
def sumA(A):
  ans = 0
  for x in A.keys():
    ans += x * A[x]
  return ans

A = {}
for i in range(n):
  if a[i] in A.keys():
    A[a[i]] += 1
  else:
    A[a[i]] = 1

q = int(input())
for i in range(q):
  b,c = list(map(int, input().split()))
  if b in A.keys():
    if c in A.keys():
      A[c] += A[b]
      A[b] = 0
    else:
      A[c] = A[b]
      A[b] = 0
  print(sumA(A))



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

A = [0] * 100001
for i in a:
  A[i] += 1

ans  = 0
for i in range(len(A)):
  ans += i * A[i]  

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