Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
numlist = list(map(int,input().split()))
numcount = [0]*10**6

numset = set()
for num in numlist:
    numcount[num] += 1
    numset.add(num)
n = int(input())
anslist = []

for i in range(n):
    b,c = map(int,input().split())
    numcount[c] += numcount[b]

    numcount[b] = 0
    numset.add(c)
    numset.discard(b)
    result = 0
    for num in list(numset):
        result += numcount[num]*num
    anslist.append(result)

for ans in anslist:
    print(ans)


Fixed submission:
n = int(input())
numlist = list(map(int,input().split()))
numcount = [0]*10**6

numset = set()
for num in numlist:
    numcount[num] += 1
    numset.add(num)
n = int(input())
anslist = []
result = 0
for num in list(numset):
    result += numcount[num]*num
    
for i in range(n):
    b,c = map(int,input().split())
    numcount[c] += numcount[b]
    result += (c-b)*numcount[b]
    anslist.append(result)

    numcount[b] = 0
    numset.add(c)
    numset.discard(b)

for ans in anslist:
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
numlist = list(map(int,input().split()))
numcount = [0]*10**6

for num in numlist:
    numcount[num] += 1
n = int(input())
anslist = []
for i in range(n):
    b,c = map(int,input().split())
    numcount[c] += numcount[b]
    numcount[b] = 0
    result = 0
    for i,num in enumerate(numcount):
        result += i*num
    anslist.append(result)

for ans in anslist:
    print(ans)


Fixed submission:
n = int(input())
numlist = list(map(int,input().split()))
numcount = [0]*10**6

numset = set()
for num in numlist:
    numcount[num] += 1
    numset.add(num)
n = int(input())
anslist = []
result = 0
for num in list(numset):
    result += numcount[num]*num
    
for i in range(n):
    b,c = map(int,input().split())
    numcount[c] += numcount[b]
    result += (c-b)*numcount[b]
    anslist.append(result)

    numcount[b] = 0
    numset.add(c)
    numset.discard(b)

for ans in anslist:
    print(ans)

Buggy python submission (verdict: Runtime Error):
n = int(input())
numlist = list(map(int,input().split()))
numcount = [0]*10**5
for num in numlist:
    numcount[num] += 1
n = int(input())
anslist = []
for i in range(n):
    b,c = map(int,input().split())
    numcount[c] += numcount[b]
    numcount[b] = 0
    result = 0
    for i,num in enumerate(numcount):
        result += i*num
    anslist.append(result)

for ans in anslist:
    print(ans)


Fixed submission:
n = int(input())
numlist = list(map(int,input().split()))
numcount = [0]*10**6

numset = set()
for num in numlist:
    numcount[num] += 1
    numset.add(num)
n = int(input())
anslist = []
result = 0
for num in list(numset):
    result += numcount[num]*num
    
for i in range(n):
    b,c = map(int,input().split())
    numcount[c] += numcount[b]
    result += (c-b)*numcount[b]
    anslist.append(result)

    numcount[b] = 0
    numset.add(c)
    numset.discard(b)

for ans in anslist:
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
numlist = list(map(int,input().split()))
numcount = [0]*10**6
for num in numlist:
    numcount[num] += 1
n = int(input())
anslist = []
for i in range(n):
    b,c = map(int,input().split())
    numcount[c] += numcount[b]
    numcount[b] = 0
    result = 0
    for i,num in enumerate(numcount):
        result += i*num
    anslist.append(result)

for ans in anslist:
    print(ans)


Fixed submission:
n = int(input())
numlist = list(map(int,input().split()))
numcount = [0]*10**6

numset = set()
for num in numlist:
    numcount[num] += 1
    numset.add(num)
n = int(input())
anslist = []
result = 0
for num in list(numset):
    result += numcount[num]*num
    
for i in range(n):
    b,c = map(int,input().split())
    numcount[c] += numcount[b]
    result += (c-b)*numcount[b]
    anslist.append(result)

    numcount[b] = 0
    numset.add(c)
    numset.discard(b)

for ans in anslist:
    print(ans)