Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    A = list(map(int,input().split()))
    Q = int(input())
    B = [0 for i in range(Q)]
    C = [0 for i in range(Q)]
    
    A_kind = []
    A_num = []
    ans = 0
    for a in A:
        ans += a
        if not a in A_kind:
            A_kind.append(a)
            A_num.append(1)
        else:
            A_num[A_kind.index(a)] += 1
    
    for q in range(Q):
        b,c = map(int,input().split())
        if b in A_kind:
            n = A_kind.index(b)
            ans += (c - A_kind[n])*A_num[n]
            if c in A_kind:
                A_num[A_kind.index(c)] += A_num[n]
                del A_kind[n]
                del A_num[n]
            else:
                A_kind[n] = c
        print(ans)
    
if __name__ == '__main__':
    main()

Fixed submission:
def main():
    N = int(input())
    A = list(map(int,input().split()))
    Q = int(input())
    B = [0 for i in range(Q)]
    C = [0 for i in range(Q)]
    
    A_num = [0 for i in range(10**5+1)]
    ans = 0
    for a in A:
        ans += a
        A_num[a] += 1
    
    for q in range(Q):
        b,c = map(int,input().split())
        if A_num[b] != 0:
            ans += (c-b)*A_num[b]
            A_num[c] += A_num[b]
            A_num[b] = 0
            
        print(ans)
    
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    A = list(map(int,input().split()))
    Q = int(input())
    B = [0 for i in range(Q)]
    C = [0 for i in range(Q)]
    
    A_kind = []
    A_num = []
    ans = 0
    for a in A:
        ans += a
        if not a in A_kind:
            A_kind.append(a)
            A_num.append(1)
        else:
            A_num[A_kind.index(a)] += 1
    
    for q in range(Q):
        B[q],C[q] = map(int,input().split())
        
    for q in range(Q):
        b = B[q]
        c = C[q]
        if b in A_kind:
            n = A_kind.index(b)
            ans += (c - A_kind[n])*A_num[n]
            if c in A_kind:
                A_num[A_kind.index(c)] += A_num[n]
                del A_kind[n]
                del A_num[n]
            else:
                A_kind[n] = c
        print(ans)
    
if __name__ == '__main__':
    main()

Fixed submission:
def main():
    N = int(input())
    A = list(map(int,input().split()))
    Q = int(input())
    B = [0 for i in range(Q)]
    C = [0 for i in range(Q)]
    
    A_num = [0 for i in range(10**5+1)]
    ans = 0
    for a in A:
        ans += a
        A_num[a] += 1
    
    for q in range(Q):
        b,c = map(int,input().split())
        if A_num[b] != 0:
            ans += (c-b)*A_num[b]
            A_num[c] += A_num[b]
            A_num[b] = 0
            
        print(ans)
    
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    A = list(map(int,input().split()))
    Q = int(input())
    B = [0 for i in range(Q)]
    C = [0 for i in range(Q)]
    
    for q in range(Q):
        b,c = map(int,input().split())
        A = [c if a == b else a for a in A]
        print(sum(A))
    
if __name__ == '__main__':
    main()

Fixed submission:
def main():
    N = int(input())
    A = list(map(int,input().split()))
    Q = int(input())
    B = [0 for i in range(Q)]
    C = [0 for i in range(Q)]
    
    A_num = [0 for i in range(10**5+1)]
    ans = 0
    for a in A:
        ans += a
        A_num[a] += 1
    
    for q in range(Q):
        b,c = map(int,input().split())
        if A_num[b] != 0:
            ans += (c-b)*A_num[b]
            A_num[c] += A_num[b]
            A_num[b] = 0
            
        print(ans)
    
if __name__ == '__main__':
    main()