Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N=int(input())
    l=[0]*10**5
    A=map(int, input().split())
    for a in A:
        l[a-1]+=a
    Q=int(input())
    BC=[list(map(int,input().split())) for _ in range(Q)]
    for bc in BC:
        cnt=l[bc[0]-1]//bc[0]
        l[bc[0]-1]=0
        l[bc[1]-1]+=bc[1]*cnt
        print(sum(l))
    return

if __name__=='__main__':
    main()


Fixed submission:
def main():
    N=int(input())
    l=[0]*10**5
    sm=0
    A=map(int, input().split())
    for a in A:
        l[a-1]+=1
        sm+=a
    Q=int(input())
    BC=[list(map(int,input().split())) for _ in range(Q)]
    for bc in BC:
        cnt=l[bc[0]-1]
        l[bc[0]-1]=0
        l[bc[1]-1]+=cnt
        sm=sm-bc[0]*cnt+bc[1]*cnt
        print(sm)
    return

if __name__=='__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N=int(input())
    l=[0]*10**5
    A=map(int, input().split())
    for a in A:
        l[a-1]+=a
    Q=int(input())
    ans=[]
    for _ in range(Q):
        B,C=map(int, input().split())
        cnt=l[B-1]//B
        l[B-1]=0
        l[C-1]+=C*cnt
        ans.append(str(sum(l)))
    print('\n'.join(ans))
    return

if __name__=='__main__':
    main()


Fixed submission:
def main():
    N=int(input())
    l=[0]*10**5
    sm=0
    A=map(int, input().split())
    for a in A:
        l[a-1]+=1
        sm+=a
    Q=int(input())
    BC=[list(map(int,input().split())) for _ in range(Q)]
    for bc in BC:
        cnt=l[bc[0]-1]
        l[bc[0]-1]=0
        l[bc[1]-1]+=cnt
        sm=sm-bc[0]*cnt+bc[1]*cnt
        print(sm)
    return

if __name__=='__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N=int(input())
    A=[int(x) for x in input().split()]
    Q=int(input())
    ans=[]
    for _ in range(Q):
        B,C=map(int, input().split())
        A=[C if x==B else x for x in A]
        ans.append(str(sum(A)))
    print('\n'.join(ans))
    return

if __name__=='__main__':
    main()


Fixed submission:
def main():
    N=int(input())
    l=[0]*10**5
    sm=0
    A=map(int, input().split())
    for a in A:
        l[a-1]+=1
        sm+=a
    Q=int(input())
    BC=[list(map(int,input().split())) for _ in range(Q)]
    for bc in BC:
        cnt=l[bc[0]-1]
        l[bc[0]-1]=0
        l[bc[1]-1]+=cnt
        sm=sm-bc[0]*cnt+bc[1]*cnt
        print(sm)
    return

if __name__=='__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N=int(input())
    A=[int(x) for x in input().split()]
    Q=int(input())
    BC=[list(map(int, input().split())) for _ in range(Q)]
    for bc in BC:
        A=[bc[1] if x==bc[0] else x for x in A]
        print(sum(A))
    return

if __name__=='__main__':
    main()


Fixed submission:
def main():
    N=int(input())
    l=[0]*10**5
    sm=0
    A=map(int, input().split())
    for a in A:
        l[a-1]+=1
        sm+=a
    Q=int(input())
    BC=[list(map(int,input().split())) for _ in range(Q)]
    for bc in BC:
        cnt=l[bc[0]-1]
        l[bc[0]-1]=0
        l[bc[1]-1]+=cnt
        sm=sm-bc[0]*cnt+bc[1]*cnt
        print(sm)
    return

if __name__=='__main__':
    main()