Buggy python submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
num=[0]*(10**5+1)
ans=sum(a)
for i in a:
    num[i]+=1
for i in range(q):
    b,c=map(int,input().split())
    ans=ans+(c-b)*num[b-1][0]
    num[c-1][0]+=num[b-1][0]
    num[b-1][0]=0
    print(ans)
    

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
num=[0]*(10**5+1)
ans=sum(a)
for i in a:
    num[i]+=1
for i in range(q):
    b,c=map(int,input().split())
    ans=ans+(c-b)*num[b]
    num[c]+=num[b]
    num[b]=0
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
num=[[a.count(i+1)] for i in range(10**5)]
ans=sum(a)
for i in range(q):
    b,c=map(int,input().split())
    ans=ans+(c-b)*num[b-1][0]
    num[c-1][0]+=num[b-1][0]
    num[b-1][0]=0
    print(ans)
    

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
num=[0]*(10**5+1)
ans=sum(a)
for i in a:
    num[i]+=1
for i in range(q):
    b,c=map(int,input().split())
    ans=ans+(c-b)*num[b]
    num[c]+=num[b]
    num[b]=0
    print(ans)

Buggy python submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
num=[[a.count(i+1)] for i in range(n)]
ans=sum(a)
for i in range(q):
    b,c=map(int,input().split())
    ans=ans+(c-b)*num[b-1][0]
    num[c-1][0]+=num[b-1][0]
    num[b-1][0]=0
    print(ans)

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
num=[0]*(10**5+1)
ans=sum(a)
for i in a:
    num[i]+=1
for i in range(q):
    b,c=map(int,input().split())
    ans=ans+(c-b)*num[b]
    num[c]+=num[b]
    num[b]=0
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=input()
q=int(input())
BC=[[] for _ in range(q)]
for i in range(q):
    b,c=input().split()
    BC[i].append(b)
    BC[i].append(c)
for i in range(q):
    if BC[i][0] in a:
        
        a=a.replace(BC[i][0],BC[i][1])
    e=list(map(int,a.split()))
    print(sum(e))

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
num=[0]*(10**5+1)
ans=sum(a)
for i in a:
    num[i]+=1
for i in range(q):
    b,c=map(int,input().split())
    ans=ans+(c-b)*num[b]
    num[c]+=num[b]
    num[b]=0
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=input()
q=int(input())
BC=[[] for _ in range(q)]
for i in range(q):
    b,c=input().split()
    BC[i].append(b)
    BC[i].append(c)
for i in range(q):
    a=a.replace(BC[i][0],BC[i][1])
    e=list(map(int,a.split()))
    print(sum(e))
        

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
num=[0]*(10**5+1)
ans=sum(a)
for i in a:
    num[i]+=1
for i in range(q):
    b,c=map(int,input().split())
    ans=ans+(c-b)*num[b]
    num[c]+=num[b]
    num[b]=0
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=input()
q=int(input())
BC=[[] for _ in range(q)]
for i in range(q):
    b,c=input().split()
    BC[i].append(b)
    BC[i].append(c)
for i in range(q):
    a=a.replace(BC[i][0],BC[i][1])
    e=list(map(int,a.split()))
    print(sum(e))
        

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
num=[0]*(10**5+1)
ans=sum(a)
for i in a:
    num[i]+=1
for i in range(q):
    b,c=map(int,input().split())
    ans=ans+(c-b)*num[b]
    num[c]+=num[b]
    num[b]=0
    print(ans)