Buggy python submission (verdict: Runtime Error):
N = int(input())
numbers = list(map(int,input().split()))
numOfOperation = int(input())
dic = {}

for num in numbers:
    if num in dic : dic[num] += 1
    else: dic[num] = 1
    

sums = sum(numbers)
for i in range(numOfOperation):
    B,C = map(int,input().split())
    cnt = dic[B]
    dic[B] = 0
    if (C not in dic) : dic[C] = 1
    else : dic[C] += cnt
    sums += cnt * (C - B)
    print(sums)


Fixed submission:
N = int(input())
numbers = list(map(int,input().split()))
numOfOperation = int(input())
dic = {}

for num in numbers:
    if num in dic : dic[num] += 1
    else: dic[num] = 1
    

sums = sum(numbers)
for i in range(numOfOperation):
    B,C = map(int,input().split())
    if (B in dic) : 
        cnt = dic[B]
        dic[B] = 0
        if (C not in dic) : dic[C] = cnt
        else : dic[C] += cnt
        sums += cnt * (C - B)
    print(sums)

Buggy python submission (verdict: Runtime Error):
N = int(input())
numbers = list(map(int,input().split()))
numOfOperation = int(input())
dic = {}

for num in numbers:
    if num in dic : dic[num] += 1
    else: dic[num] = 1
    

sums = sum(numbers)
for i in range(numOfOperation):
    B,C = map(int,input().split())
    cnt = dic[B]
    dic[B] = 0
    dic[C] += cnt
    sums += cnt * (C - B)
    print(sums)


Fixed submission:
N = int(input())
numbers = list(map(int,input().split()))
numOfOperation = int(input())
dic = {}

for num in numbers:
    if num in dic : dic[num] += 1
    else: dic[num] = 1
    

sums = sum(numbers)
for i in range(numOfOperation):
    B,C = map(int,input().split())
    if (B in dic) : 
        cnt = dic[B]
        dic[B] = 0
        if (C not in dic) : dic[C] = cnt
        else : dic[C] += cnt
        sums += cnt * (C - B)
    print(sums)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
numbers = list(map(int,input().split()))
numOfOperation = int(input())
sums = sum(numbers)
for i in range(numOfOperation):
    B,C = map(int,input().split())
    cnt = 0
    for j in range(len(numbers)):
        if numbers[j] == B : 
            cnt += 1
            numbers[j] = C
    sums += cnt * (C - B)
    print(sums)


Fixed submission:
N = int(input())
numbers = list(map(int,input().split()))
numOfOperation = int(input())
dic = {}

for num in numbers:
    if num in dic : dic[num] += 1
    else: dic[num] = 1
    

sums = sum(numbers)
for i in range(numOfOperation):
    B,C = map(int,input().split())
    if (B in dic) : 
        cnt = dic[B]
        dic[B] = 0
        if (C not in dic) : dic[C] = cnt
        else : dic[C] += cnt
        sums += cnt * (C - B)
    print(sums)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
numbers = list(map(int,input().split()))
numOfOperation = int(input())
for i in range(numOfOperation):
    B,C = map(int,input().split())
    for j in range(len(numbers)):
        if numbers[j] == B : numbers[j] = C
    print(sum(numbers))


Fixed submission:
N = int(input())
numbers = list(map(int,input().split()))
numOfOperation = int(input())
dic = {}

for num in numbers:
    if num in dic : dic[num] += 1
    else: dic[num] = 1
    

sums = sum(numbers)
for i in range(numOfOperation):
    B,C = map(int,input().split())
    if (B in dic) : 
        cnt = dic[B]
        dic[B] = 0
        if (C not in dic) : dic[C] = cnt
        else : dic[C] += cnt
        sums += cnt * (C - B)
    print(sums)