Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
dict={}
a=list(map(int,input().split()))
for i in a:
  if i not in dict:
    dict[i]=1
  else:
    dict[i]+=1
q=int(input())
for i in range(q):
  b,c=map(int,input().split())
  ans=0
  if c in dict and b in dict:
    dict[c]+=dict[b]
  elif c not in dict and b in dict:
    dict[c]=dict[b]
  dict[b]=0
  ans=0
  for j in dict:
    ans+=dict[j]*j
  print(ans)


Fixed submission:
import collections
n = int(input())
a = list(map(int, input().split()))
count = collections.Counter(a)
q = int(input())
s = sum(a)

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

Buggy python submission (verdict: Runtime Error):
n=int(input())
dict={}
a=list(map(int,input().split()))
for i in a:
  if i not in dict:
    dict[i]=1
  else:
    dict[i]+=1
q=int(input())
for i in range(q):
  b,c=map(int,input().split())
  ans=0
  if c in dict:
    dict[c]+=dict[b]
  else:
    dict[c]=dict[b]
  dict[b]=0
  ans=0
  for j in dict:
    ans+=dict[j]*j
  print(ans)

Fixed submission:
import collections
n = int(input())
a = list(map(int, input().split()))
count = collections.Counter(a)
q = int(input())
s = sum(a)

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

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
dict={}
a=list(map(int,input().split()))
for i in a:
  if i not in dict:
    dict[i]=1
  else:
    dict[i]+=1
q=int(input())
for i in range(q):
  b,c=map(int,input().split())
  ans=0
  for j in dict:
    if j==b:
      ans+=c*dict[b]
    else:
      ans+=j*dict[j]
  if b in dict and c not in dict:
    dict[c]=dict[b]
    dict[b]=0
  elif b in dict and c in dict:
    dict[c]+=dict[b]
    dict[b]=0
  print(ans)

Fixed submission:
import collections
n = int(input())
a = list(map(int, input().split()))
count = collections.Counter(a)
q = int(input())
s = sum(a)

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

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
q = int(input())
for i in range(q):
  bc = list(map(int, input().split()))
  res = bc[0]
  for bit in range(n):
    if a[bit] == res:
      a[bit] = bc[1]
  print(sum(a))


Fixed submission:
import collections
n = int(input())
a = list(map(int, input().split()))
count = collections.Counter(a)
q = int(input())
s = sum(a)

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

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
q = int(input())
for i in range(q):
  bc = list(map(int, input().split()))
  res = bc[0]
  for bit in range(n):
    if a[bit] == res:
      a[bit] = bc[1]
  print(sum(a))

Fixed submission:
import collections
n = int(input())
a = list(map(int, input().split()))
count = collections.Counter(a)
q = int(input())
s = sum(a)

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

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
q = int(input())
for j in range(q):
  bc = list(map(int, input().split()))
  res = bc[0]
  for j in range(n):
    if a[j] == res:
      a[j] = bc[1]
    else:
      a[j] = a[j]
  print(sum(a))

Fixed submission:
import collections
n = int(input())
a = list(map(int, input().split()))
count = collections.Counter(a)
q = int(input())
s = sum(a)

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