Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin

N = int(stdin.readline())
A = list(map(int,stdin.readline().split()))
A.sort()
Q = int(stdin.readline())
D = dict()
S = set(A)
sum=0
count=0

for y in range(0,N):
    sum+=A[y]
    if y>0 and A[y]!=A[y-1]:
        D[A[y-1]]=count
        count=1
    elif y==0:
        count=1
    else:
        count+=1
    if y==N-1:
        D[A[y]]=count

for u in range(0,Q):
    U= list(map(int,stdin.readline().split()))
    SS=S.copy()
    SS.add(U[0])
    if len(SS)==len(S):

        SS.add(U[1])
        if len(SS)==len(S):
            sum+=(D[U[0]])*(U[1]-U[0])

            D[U[1]]+=D[U[0]]
            D[U[0]]=0
        else:
            sum += (D[U[0]]) * (U[1] - U[0])
            D[U[1]]=D[U[0]]
            D[U[0]]=0
        S=SS

    print(sum)

Fixed submission:
from sys import stdin

N = int(stdin.readline())
A = list(map(int,stdin.readline().split()))
A.sort()
Q = int(stdin.readline())
D = dict()
S = set(A)
sum=0
count=0

for y in range(0,N):
    sum+=A[y]
    if y>0 and A[y]!=A[y-1]:
        D[A[y-1]]=count
        count=1
    elif y==0:
        count=1
    else:
        count+=1
    if y==N-1:
        D[A[y]]=count

for u in range(0,Q):
    U= list(map(int,stdin.readline().split()))
    if U[0] in D:
        sum+=(D[U[0]])*(U[1]-U[0])
        if U[1] in D:
            D[U[1]]+=D[U[0]]
        else:
            D[U[1]]=D[U[0]]
        D[U[0]]=0


    print(sum)

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin

N = int(stdin.readline())
A = list(map(int,stdin.readline().split()))
Q = int(stdin.readline())
D = dict()
S = set()
sum=0

for y in A:
    sum+=y
    s = len(S)
    S.add(y)
    if len(S)==s:
        D[y]+=1
    else:
        D[y]=1



for u in range(0,Q):
    U= list(map(int,stdin.readline().split()))
    SS=S.copy()
    SS.add(U[0])
    if len(SS)==len(S):

        SS.add(U[1])
        if len(SS)==len(S):
            sum+=(D[U[0]])*(U[1]-U[0])

            D[U[1]]+=D[U[0]]
            D[U[0]]=0
        else:
            sum += (D[U[0]]) * (U[1] - U[0])
            D[U[1]]=D[U[0]]
            D[U[0]]=0
        S=SS

    print(sum)

Fixed submission:
from sys import stdin

N = int(stdin.readline())
A = list(map(int,stdin.readline().split()))
A.sort()
Q = int(stdin.readline())
D = dict()
S = set(A)
sum=0
count=0

for y in range(0,N):
    sum+=A[y]
    if y>0 and A[y]!=A[y-1]:
        D[A[y-1]]=count
        count=1
    elif y==0:
        count=1
    else:
        count+=1
    if y==N-1:
        D[A[y]]=count

for u in range(0,Q):
    U= list(map(int,stdin.readline().split()))
    if U[0] in D:
        sum+=(D[U[0]])*(U[1]-U[0])
        if U[1] in D:
            D[U[1]]+=D[U[0]]
        else:
            D[U[1]]=D[U[0]]
        D[U[0]]=0


    print(sum)

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin

N = int(stdin.readline())
A = list(map(int,stdin.readline().split()))
Q = int(stdin.readline())
D = dict()
S = set(A)
sum=0

for y in A:
    sum+=y

for t in S:
    D[t]=A.count(t)

for u in range(0,Q):
    U= list(map(int,stdin.readline().split()))
    SS=S.copy()
    SS.add(U[0])
    if len(SS)==len(S):

        SS.add(U[1])
        if len(SS)==len(S):
            sum+=(D[U[0]])*(U[1]-U[0])

            D[U[1]]+=D[U[0]]
            D[U[0]]=0
        else:
            sum += (D[U[0]]) * (U[1] - U[0])
            D[U[1]]=D[U[0]]
            D[U[0]]=0
        S=SS

    print(sum)

Fixed submission:
from sys import stdin

N = int(stdin.readline())
A = list(map(int,stdin.readline().split()))
A.sort()
Q = int(stdin.readline())
D = dict()
S = set(A)
sum=0
count=0

for y in range(0,N):
    sum+=A[y]
    if y>0 and A[y]!=A[y-1]:
        D[A[y-1]]=count
        count=1
    elif y==0:
        count=1
    else:
        count+=1
    if y==N-1:
        D[A[y]]=count

for u in range(0,Q):
    U= list(map(int,stdin.readline().split()))
    if U[0] in D:
        sum+=(D[U[0]])*(U[1]-U[0])
        if U[1] in D:
            D[U[1]]+=D[U[0]]
        else:
            D[U[1]]=D[U[0]]
        D[U[0]]=0


    print(sum)