Buggy python submission (verdict: Runtime Error):
from collections import Counter

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

for i in range(q):
	B, C = map(int, input().split())
	b.append(B)
	c.append(C)

cnt = Counter(a)

s = sum(cnt)

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

Fixed submission:
from collections import Counter

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

cnt = Counter(a)

s = sum(a)

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

Buggy python submission (verdict: Runtime Error):
from collections import Counter

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)

cnt = Counter(a)

s = sum(arr)

for i in range(q):
	s -= b[i]*cnt[b[i]]
	s += c[i]*cnt[b[i]]
	cnt[c[i]] += cnt[b[i]]
	cnt[b[i]] = 0
	print(s)

Fixed submission:
from collections import Counter

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

cnt = Counter(a)

s = sum(a)

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

Buggy python submission (verdict: Runtime Error):
from collections import Counter

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)

arr = Counter(a)

s = sum(arr)

for i in range(q):
	s -= b[i]*arr[b[i]]
	s += c[i]*arr[c[i]]
	ans[c[i]] += ans[b[i]]
	ans[b[i]] = 0
	print(s)

Fixed submission:
from collections import Counter

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

cnt = Counter(a)

s = sum(a)

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

Buggy python submission (verdict: Time Limit Exceeded):
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)

a1 = []
a2 = []

for i in a:
	if i in a1:
		a2[a1.index(i)]+=1
	else:
		if i in b:
			a1.insert(0,i)
			a2.insert(0,1)
		else:
			a1.append(i)
			a2.append(1)

ans = []
s = 0

for i in range(len(a1)):
	s += a1[i]*a2[i]

ans.append(s)

for i in range(q):
	if b[i] in a1:
		bi = a1.index(b[i])
		if c[i] in a1:
			a2[a1.index(c[i])]+=a2[bi]
			ans.append(ans[-1]+a2[bi]*(c[i]-b[i]))
			a2[bi] = 0
		else:
			a1.append(c[i])
			a2.append(a2[bi])
			ans.append(ans[-1]+a2[bi]*(c[i]-b[i]))
			a2[bi] = 0
	else:
		ans.append(ans[-1])

for i in range(len(ans)-1):
	print(ans[i+1])

Fixed submission:
from collections import Counter

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

cnt = Counter(a)

s = sum(a)

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

Buggy python submission (verdict: Time Limit Exceeded):
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)

a1 = []
a2 = []

for i in a:
	if i in a1:
		a2[a1.index(i)]+=1
	else:
		a1.append(i)
		a2.append(1)

ans = []
s = 0

for i in range(len(a1)):
	s += a1[i]*a2[i]

ans.append(s)

for i in range(q):
	if b[i] in a1:
		bi = a1.index(b[i])
		if c[i] in a1:
			a2[a1.index(c[i])]+=a2[bi]
			ans.append(ans[-1]+a2[bi]*(c[i]-b[i]))
			a2[bi] = 0
		else:
			a1.append(c[i])
			a2.append(a2[bi])
			ans.append(ans[-1]+a2[bi]*(c[i]-b[i]))
			a2[bi] = 0
	else:
		ans.append(ans[-1])

for i in range(len(ans)-1):
	print(ans[i+1])

Fixed submission:
from collections import Counter

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

cnt = Counter(a)

s = sum(a)

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