Buggy python submission (verdict: Time Limit Exceeded):
from operator import mul
import collections
N = int(input())
A = list(map(int, input().split()))
cnt = collections.Counter(A)
Q = int(input())
ans = []
for _ in range(Q):
    b, c = map(int, input().split())
    if b in cnt:
        cnt[c] += cnt[b]
        del cnt[b]
    k = list(cnt.keys())
    v = list(cnt.values())
    print(sum(map(mul, k, v)))

Fixed submission:
import collections
N = int(input())
A = list(map(int, input().split()))
cnt = collections.Counter(A)
Q = int(input())
ans = []
s = sum(A)
for _ in range(Q):
    b, c = map(int, input().split())
    n = cnt[b]
    if b in cnt:
        cnt[c] += cnt[b]
        del cnt[b]
    s += (c-b)*n
    print(s)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
ans = []
for _ in range(Q):
    b, c = map(int, input().split())
    if b in A:
        for n in range(N):
            if A[n] == b:
                A[n] = c
    print(sum(A))

Fixed submission:
import collections
N = int(input())
A = list(map(int, input().split()))
cnt = collections.Counter(A)
Q = int(input())
ans = []
s = sum(A)
for _ in range(Q):
    b, c = map(int, input().split())
    n = cnt[b]
    if b in cnt:
        cnt[c] += cnt[b]
        del cnt[b]
    s += (c-b)*n
    print(s)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
ans = []
for _ in range(Q):
    b, c = map(int, input().split())
    if b in A:
        for n in range(N):
            if A[n] == b:
                A[n] = c
    ans.append(sum(A))
    
for q in range(Q):
    print(ans[q])

Fixed submission:
import collections
N = int(input())
A = list(map(int, input().split()))
cnt = collections.Counter(A)
Q = int(input())
ans = []
s = sum(A)
for _ in range(Q):
    b, c = map(int, input().split())
    n = cnt[b]
    if b in cnt:
        cnt[c] += cnt[b]
        del cnt[b]
    s += (c-b)*n
    print(s)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
bc = []
for q in range(Q):
    b, c = map(int, input().split())
    A = [c if b == a else a for a in A]
    print(sum(A))

Fixed submission:
import collections
N = int(input())
A = list(map(int, input().split()))
cnt = collections.Counter(A)
Q = int(input())
ans = []
s = sum(A)
for _ in range(Q):
    b, c = map(int, input().split())
    n = cnt[b]
    if b in cnt:
        cnt[c] += cnt[b]
        del cnt[b]
    s += (c-b)*n
    print(s)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
bc = []
for q in range(Q):
    b, c = map(int, input().split())
    A = [c if b == a else a for a in A]
    print(sum(A))

Fixed submission:
import collections
N = int(input())
A = list(map(int, input().split()))
cnt = collections.Counter(A)
Q = int(input())
ans = []
s = sum(A)
for _ in range(Q):
    b, c = map(int, input().split())
    n = cnt[b]
    if b in cnt:
        cnt[c] += cnt[b]
        del cnt[b]
    s += (c-b)*n
    print(s)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())


for q in range(Q):
    b, c = map(int, input().split())
    for n in range(N):
        if b == A[n]:
            A[n] = c
    print(sum(A))

Fixed submission:
import collections
N = int(input())
A = list(map(int, input().split()))
cnt = collections.Counter(A)
Q = int(input())
ans = []
s = sum(A)
for _ in range(Q):
    b, c = map(int, input().split())
    n = cnt[b]
    if b in cnt:
        cnt[c] += cnt[b]
        del cnt[b]
    s += (c-b)*n
    print(s)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
N = int(input())
A = list(map(int, input().split()))
Q = int(input())


for q in range(Q):
    b, c = map(int, input().split())
    for n in range(N):
        if b == A[n]:
            A[n] = c
    print(sum(A))

Fixed submission:
import collections
N = int(input())
A = list(map(int, input().split()))
cnt = collections.Counter(A)
Q = int(input())
ans = []
s = sum(A)
for _ in range(Q):
    b, c = map(int, input().split())
    n = cnt[b]
    if b in cnt:
        cnt[c] += cnt[b]
        del cnt[b]
    s += (c-b)*n
    print(s)

Buggy python submission (verdict: Runtime Error):
mport sys
N = int(input())
A = list(map(int, input().split()))
Q = int(input())


for q in range(Q):
    b, c = map(int, input().split())
    for n in range(N):
        if b == A[n]:
            A[n] = c
    print(sum(A))

Fixed submission:
import collections
N = int(input())
A = list(map(int, input().split()))
cnt = collections.Counter(A)
Q = int(input())
ans = []
s = sum(A)
for _ in range(Q):
    b, c = map(int, input().split())
    n = cnt[b]
    if b in cnt:
        cnt[c] += cnt[b]
        del cnt[b]
    s += (c-b)*n
    print(s)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
N = int(input())
A = list(map(int, input().split()))
Q = int(input())


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

Fixed submission:
import collections
N = int(input())
A = list(map(int, input().split()))
cnt = collections.Counter(A)
Q = int(input())
ans = []
s = sum(A)
for _ in range(Q):
    b, c = map(int, input().split())
    n = cnt[b]
    if b in cnt:
        cnt[c] += cnt[b]
        del cnt[b]
    s += (c-b)*n
    print(s)

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

Fixed submission:
import collections
N = int(input())
A = list(map(int, input().split()))
cnt = collections.Counter(A)
Q = int(input())
ans = []
s = sum(A)
for _ in range(Q):
    b, c = map(int, input().split())
    n = cnt[b]
    if b in cnt:
        cnt[c] += cnt[b]
        del cnt[b]
    s += (c-b)*n
    print(s)