Buggy python submission (verdict: Runtime Error):
n=int(input())
elements= input().split()
q=int(input())
actions=[]
for i in range(q):
    actions.append(input().split())
 
dict={}
for e in elements:
    count=dict.get(e,0)
    count+=1
    dict[e]=count
 
sum=0
for key in dict.keys():
    sum+=int(key)* dict[key]

for act in actions:
    count=dict.get(act[0],0)
    sum+=(int(act[1])-int(act[0]))*count
    count+=dict.get(act[1],0)
    dict[act[1]]=count
    del dict[act[0]]
 
    print(str(sum))

Fixed submission:
n=int(input())
elements= input().split()
q=int(input())
actions=[]
for i in range(q):
    actions.append(input().split())
 
dict={}
for e in elements:
    count=dict.get(e,0)
    count+=1
    dict[e]=count
 
sum=0
for key in dict.keys():
    sum+=int(key)* dict[key]

for act in actions:
    count=dict.get(act[0],0)
    sum+=(int(act[1])-int(act[0]))*count
    count+=dict.get(act[1],0)
    dict[act[1]]=count
    dict[act[0]]=0
 
    print(str(sum))

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
elements= input().split()
q=int(input())
actions=[]
for i in range(q):
    actions.append(input().split())
 
number_list=[]
count_list=[]
for e in elements:
    if int(e) in number_list:
        count_list[number_list.index(int(e))]+=1
    else:
        number_list.append(int(e))
        count_list.append(1)
 
sum=0
for i in range(len(count_list)):
    sum+=number_list[i]* count_list[i]

for act in actions:
    if int(act[0]) in number_list:
        index0=number_list.index(int(act[0]))
        
        if int(act[1]) in number_list:
            index1=number_list.index(int(act[1]))
            sum+=(number_list[index1]-number_list[index0])* count_list[index0]
            count_list[index1]+=count_list[index0]
        else:
            sum+=(int(act[1])-number_list[index0])* count_list[index0]
            number_list.append(int(act[1]))
            count_list.append(count_list[index0])

        count_list[index0]=0
 
 
    print(str(sum))

Fixed submission:
n=int(input())
elements= input().split()
q=int(input())
actions=[]
for i in range(q):
    actions.append(input().split())
 
dict={}
for e in elements:
    count=dict.get(e,0)
    count+=1
    dict[e]=count
 
sum=0
for key in dict.keys():
    sum+=int(key)* dict[key]

for act in actions:
    count=dict.get(act[0],0)
    sum+=(int(act[1])-int(act[0]))*count
    count+=dict.get(act[1],0)
    dict[act[1]]=count
    dict[act[0]]=0
 
    print(str(sum))

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
elements= input().split()
q=int(input())
actions=[]
for i in range(q):
    actions.append(input().split())
 
number_list=[]
count_list=[]
for e in elements:
    if int(e) in number_list:
        count_list[number_list.index(int(e))]+=1
    else:
        number_list.append(int(e))
        count_list.append(1)
 
sum=0
for i in range(len(count_list)):
    sum+=number_list[i]* count_list[i]

for act in actions:
    if int(act[0]) in number_list:
        index0=number_list.index(int(act[0]))
        
        if int(act[1]) in number_list:
            index1=number_list.index(int(act[1]))
            sum+=(number_list[index1]-number_list[index0])* count_list[index0]
            count_list[index1]+=count_list[index0]
        else:
            sum+=(int(act[1])-number_list[index0])* count_list[index0]
            number_list.append(int(act[1]))
            count_list.append(count_list[index0])

        number_list.pop(index0)
        count_list.pop(index0)
 
 
    print(str(sum))

Fixed submission:
n=int(input())
elements= input().split()
q=int(input())
actions=[]
for i in range(q):
    actions.append(input().split())
 
dict={}
for e in elements:
    count=dict.get(e,0)
    count+=1
    dict[e]=count
 
sum=0
for key in dict.keys():
    sum+=int(key)* dict[key]

for act in actions:
    count=dict.get(act[0],0)
    sum+=(int(act[1])-int(act[0]))*count
    count+=dict.get(act[1],0)
    dict[act[1]]=count
    dict[act[0]]=0
 
    print(str(sum))

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
elements= input().split()
q=int(input())
actions=[]
for i in range(q):
    actions.append(input().split())

number_list=[]
count_list=[]
for e in elements:
    if int(e) in number_list:
        count_list[number_list.index(int(e))]+=1
    else:
        number_list.append(int(e))
        count_list.append(1)

for act in actions:
    if int(act[0]) in number_list:
        index0=number_list.index(int(act[0]))
        
        if int(act[1]) in number_list:
            index1=number_list.index(int(act[1]))
            count_list[index1]+=count_list[index0]
            number_list.pop(index0)
            count_list.pop(index0)
        else:
            number_list.append(int(act[1]))
            count_list.append(count_list[index0])
            number_list.pop(index0)
            count_list.pop(index0)

    sum=0
    for i in range(len(count_list)):
        sum+=number_list[i]* count_list[i]

    print(str(sum))

Fixed submission:
n=int(input())
elements= input().split()
q=int(input())
actions=[]
for i in range(q):
    actions.append(input().split())
 
dict={}
for e in elements:
    count=dict.get(e,0)
    count+=1
    dict[e]=count
 
sum=0
for key in dict.keys():
    sum+=int(key)* dict[key]

for act in actions:
    count=dict.get(act[0],0)
    sum+=(int(act[1])-int(act[0]))*count
    count+=dict.get(act[1],0)
    dict[act[1]]=count
    dict[act[0]]=0
 
    print(str(sum))