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

s = sum(a)
q = int(input())

for i in range(q):
  b, c = map(int, input().split())
  if b in key:
    x = key.index(b)
    if c in key:
      y = key.index(c)
      s += (c - b) * value[x]
      value[y] += value[x]
      del key[x]
      del value[x]
    else:
      key[x] = c
      s += (c - b) * value[x]
  else:
    pass
  print(s)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
l = [i for i in range(10 ** 5 + 2)]
m = [0] * (10 ** 5 + 2)
for i in a:
  m[i] += 1
s = sum(a)
q = int(input())
for i in range(q):
  b, c = map(int, input().split())
  if m[b] != 0:
    s += (c - b) * m[b]
    m[c] += m[b]
    m[b] = 0
  print(s)

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

s = sum(a)
q = int(input())

for i in range(q):
  b, c = map(int, input().split())
  if b in key:
    x = key.index(b)
    if c in key:
      y = key.index(c)
      s += (c - b) * value[x]
      value[y] += value[x]
      del key[x]
      del value[x]
    else:
      key[x] = c
      s += (c - b) * value[x]
  else:
    pass
  print(s)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
l = [i for i in range(10 ** 5 + 2)]
m = [0] * (10 ** 5 + 2)
for i in a:
  m[i] += 1
s = sum(a)
q = int(input())
for i in range(q):
  b, c = map(int, input().split())
  if m[b] != 0:
    s += (c - b) * m[b]
    m[c] += m[b]
    m[b] = 0
  print(s)

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

s = sum(a)
q = int(input())

for i in range(q):
  b, c = map(int, input().split())
  if b in key:
    x = key.index(b)
    if c in key:
      y = key.index(c)
      s += (c - b) * value[x]
      value[y] += value[x]
      del key[x]
      del value[x]
    else:
      key[x] = c
      s += (c - b) * value[x]
  else:
    pass
  print(s)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
l = [i for i in range(10 ** 5 + 2)]
m = [0] * (10 ** 5 + 2)
for i in a:
  m[i] += 1
s = sum(a)
q = int(input())
for i in range(q):
  b, c = map(int, input().split())
  if m[b] != 0:
    s += (c - b) * m[b]
    m[c] += m[b]
    m[b] = 0
  print(s)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
x = sum(a)
q = int(input())

for i in range(q):
  b, c = map(int, input().split())
  cnt = 0
  for j in range(len(a)):
    if a[j] == b:
      a[j] = c
      cnt += 1
  x += (c - b) * cnt
  print(x)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
l = [i for i in range(10 ** 5 + 2)]
m = [0] * (10 ** 5 + 2)
for i in a:
  m[i] += 1
s = sum(a)
q = int(input())
for i in range(q):
  b, c = map(int, input().split())
  if m[b] != 0:
    s += (c - b) * m[b]
    m[c] += m[b]
    m[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):
  b, c = map(int, input().split())
  for j in range(len(a)):
    if a[j] == b:
      a[j] = c
  print(sum(a))

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
l = [i for i in range(10 ** 5 + 2)]
m = [0] * (10 ** 5 + 2)
for i in a:
  m[i] += 1
s = sum(a)
q = int(input())
for i in range(q):
  b, c = map(int, input().split())
  if m[b] != 0:
    s += (c - b) * m[b]
    m[c] += m[b]
    m[b] = 0
  print(s)