Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
q = int(input())
xy = [map(int, input().split()) for _ in range(q)]
b, c = [list(i) for i in zip(*xy)]

stuck_b = []
stuck_c = []
stuck_sum = []

sum = 0

for cnt_q in range(q):
    sum = 0
    if len(stuck_b) > 0:
        if stuck_b[cnt_q] == b[cnt_q] and stuck_c[cnt_q] == c[cnt_q]:
            print(stuck_sum[cnt_q])
        else:
            for cnt_n in range(n):
                if a[cnt_n] == b[cnt_q]:
                    a[cnt_n] = c[cnt_q]
            for num in range(n):
                sum += a[num]
            print(sum)
    else:
        for cnt_n in range(n):
            if a[cnt_n] == b[cnt_q]:
                a[cnt_n] = c[cnt_q]
        for num in range(n):
            sum += a[num]
        print(sum)


Fixed submission:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
xy = [map(int, input().split()) for _ in range(q)]
b, c = [list(i) for i in zip(*xy)]

ans = 0

sum_all = 0
for cnt in range(n):
    sum_all += a[cnt]

a = sorted(a)
list_a_num = [0]*(10**5 + 1)
for cnt in range(n):
    list_a_num[a[cnt]] += 1

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

Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
q = int(input())
xy = [map(int, input().split()) for _ in range(q)]
b, c = [list(i) for i in zip(*xy)]

stuck_b = []
stuck_c = []
stuck_sum = []

sum = 0

for cnt_q in range(q):
    sum = 0
    if stuck_b[cnt_q] == b[cnt_q] and stuck_c[cnt_q] == c[cnt_q]:
        print(stuck_sum[cnt_q])
    else:
        for cnt_n in range(n):
            if a[cnt_n] == b[cnt_q]:
                a[cnt_n] = c[cnt_q]
        for num in range(n):
            sum += a[num]
        print(sum)


Fixed submission:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
xy = [map(int, input().split()) for _ in range(q)]
b, c = [list(i) for i in zip(*xy)]

ans = 0

sum_all = 0
for cnt in range(n):
    sum_all += a[cnt]

a = sorted(a)
list_a_num = [0]*(10**5 + 1)
for cnt in range(n):
    list_a_num[a[cnt]] += 1

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

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
q = int(input())
xy = [map(int, input().split()) for _ in range(q)]
b, c = [list(i) for i in zip(*xy)]

sum = 0

for cnt_q in range(q):
    sum = 0
    for cnt_n in range(n):
        if a[cnt_n] == b[cnt_q]:
            a[cnt_n] = c[cnt_q]
    for num in range(n):
        sum += a[num]
    print(sum)


Fixed submission:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
xy = [map(int, input().split()) for _ in range(q)]
b, c = [list(i) for i in zip(*xy)]

ans = 0

sum_all = 0
for cnt in range(n):
    sum_all += a[cnt]

a = sorted(a)
list_a_num = [0]*(10**5 + 1)
for cnt in range(n):
    list_a_num[a[cnt]] += 1

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