Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
#a = list(map(int,input().split()))
a = input()
q = int(input())
for i in range(q):
    b,c = input().split()
    a = a.replace(b,c)
    al = list(map(int,a.split()))
    #print(al,type(al))
    su = sum(al)
    print(su)
    

Fixed submission:
from collections import Counter

n = int(input())
a = list(map(int,input().split()))
q = int(input())
freq = Counter(a)
sm = sum(a)
for i in range(q):
    b,c = map(int,input().split())
    bn = freq[b]
    freq[b] = 0
    if c in freq:
        freq[c] += bn
    else:
        freq[c] = bn
    sm = sm + (c-b)*bn
    print(sm)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int,input().split()))
q = int(input())
bc = []
ans = []
for i in range(q):
    bc = list(map(int,input().split()))
    #bc.append(list(map(int,input().split())))
    #while bc[i][0] in a:
        #a[a.index(bc[i][0])] = bc[i][1]
    aa = [bc[1] if bc[0] == a[j] else a[j] for j in range(len(a))]
    a = aa
    ans.append(sum(aa))
for i in range(len(ans)):
    print(ans[i])

Fixed submission:
from collections import Counter

n = int(input())
a = list(map(int,input().split()))
q = int(input())
freq = Counter(a)
sm = sum(a)
for i in range(q):
    b,c = map(int,input().split())
    bn = freq[b]
    freq[b] = 0
    if c in freq:
        freq[c] += bn
    else:
        freq[c] = bn
    sm = sm + (c-b)*bn
    print(sm)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int,input().split()))
q = int(input())
bc = []
ans = []
for i in range(q):
    bc = list(map(int,input().split()))
    #bc.append(list(map(int,input().split())))
    #while bc[i][0] in a:
        #a[a.index(bc[i][0])] = bc[i][1]
    aa = [bc[1] if bc[0] == a[j] else a[j] for j in range(len(a))]
    a = aa
    ans.append(sum(aa))
for i in range(len(ans)):
    print(ans[i])

Fixed submission:
from collections import Counter

n = int(input())
a = list(map(int,input().split()))
q = int(input())
freq = Counter(a)
sm = sum(a)
for i in range(q):
    b,c = map(int,input().split())
    bn = freq[b]
    freq[b] = 0
    if c in freq:
        freq[c] += bn
    else:
        freq[c] = bn
    sm = sm + (c-b)*bn
    print(sm)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int,input().split()))
q = int(input())
bc = []
ans = []
for i in range(q):
    bc.append(list(map(int,input().split())))
    while bc[i][0] in a:
        a[a.index(bc[i][0])] = bc[i][1]
    ans.append(sum(a))
for i in range(len(ans)):
    print(ans[i])

Fixed submission:
from collections import Counter

n = int(input())
a = list(map(int,input().split()))
q = int(input())
freq = Counter(a)
sm = sum(a)
for i in range(q):
    b,c = map(int,input().split())
    bn = freq[b]
    freq[b] = 0
    if c in freq:
        freq[c] += bn
    else:
        freq[c] = bn
    sm = sm + (c-b)*bn
    print(sm)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int,input().split()))
q = int(input())
bc = []
for i in range(q):
    bc.append(list(map(int,input().split())))
   
for i in range(q):

    while bc[i][0] in a:
        a[a.index(bc[i][0])] = bc[i][1]
    print(sum(a))

Fixed submission:
from collections import Counter

n = int(input())
a = list(map(int,input().split()))
q = int(input())
freq = Counter(a)
sm = sum(a)
for i in range(q):
    b,c = map(int,input().split())
    bn = freq[b]
    freq[b] = 0
    if c in freq:
        freq[c] += bn
    else:
        freq[c] = bn
    sm = sm + (c-b)*bn
    print(sm)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int,input().split()))
q = int(input())
bc = []
for i in range(q):
    bc.append(list(map(int,input().split())))
   
for i in range(q):

    while bc[i][0] in a:
        a[a.index(bc[i][0])] = bc[i][1]
    print(sum(a))

Fixed submission:
from collections import Counter

n = int(input())
a = list(map(int,input().split()))
q = int(input())
freq = Counter(a)
sm = sum(a)
for i in range(q):
    b,c = map(int,input().split())
    bn = freq[b]
    freq[b] = 0
    if c in freq:
        freq[c] += bn
    else:
        freq[c] = bn
    sm = sm + (c-b)*bn
    print(sm)