Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
A.sort()
Q = int(input())

for i in range(Q):
    b, c = map(int, input().split())
    s = 0
    e = N
    h = int((s+e)/2)
    while A[h] != b and s != h :
        if A[h] < b:
            s = h
            h = int((s+e)/2)
        elif A[h] > b:
            e = h
            h = int((s+e)/2)
    X = []
    for j in range(s, e):
        if A[j] == b:
            X.append(j)
            A[j] = c
    print(sum(A))
    if len(X) > 0:
        A = A[:X[0]] + A[X[-1]:]
        s = 0
        e = len(A)
        h = int((s+e)/2)
        while A[h] != c and s != h :
            if A[h] < c:
                s = h
                h = int((s+e)/2)
            elif A[h] > c:
                e = h
                h = int((s+e)/2)
        A = A[:h] + [c]*len(X) + A[h+1:]

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
X = [0] * (10**5)

for i in range(N):
    X[A[i]-1] += 1

ans = sum(A)

for j in range(Q):
    b, c = map(int, input().split())
    a = X[b-1]
    X[b-1] = 0
    X[c-1] += a
    ans = ans - a*b + c*a
    print(ans)

Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
A.sort()
Q = int(input())

for i in range(Q):
    b, c = map(int, input().split())
    s = 0
    e = N
    h = int((s+e)/2)
    while A[h] != b and s != h :
        if A[h] < b:
            s = h
            h = int((s+e)/2)
        elif A[h] > b:
            e = h
            h = int((s+e)/2)
    X = []
    for j in range(s, e):
        if A[j] == b:
            X.append(j)
            A[j] = c
    print(sum(A))
    if len(X) > 0:
        A = A[:X[0]] + A[X[-1]:]
        s = 0
        e = len(A)
        h = int((s+e)/2)
        while A[h] != c and s != h :
            if A[h] < c:
                s = h
                h = int((s+e)/2)
            elif A[h] > c:
                e = h
                h = int((s+e)/2)
        A = A[:h] + [c]*len(X) + A[h+1:]

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
X = [0] * (10**5)

for i in range(N):
    X[A[i]-1] += 1

ans = sum(A)

for j in range(Q):
    b, c = map(int, input().split())
    a = X[b-1]
    X[b-1] = 0
    X[c-1] += a
    ans = ans - a*b + c*a
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
A.sort()
Q = int(input())

for i in range(Q):
    b, c = map(int, input().split())
    s = 0
    e = N
    h = int((s+e)/2)
    while A[h] != b and s != h :
        if A[h] < b:
            s = h
            h = int((s+e)/2)
        elif A[h] > b:
            e = h
            h = int((s+e)/2)
    for j in range(s, e):
        if A[j] == b:
            A[j] = c
    A.sort()
    print(sum(A))

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
X = [0] * (10**5)

for i in range(N):
    X[A[i]-1] += 1

ans = sum(A)

for j in range(Q):
    b, c = map(int, input().split())
    a = X[b-1]
    X[b-1] = 0
    X[c-1] += a
    ans = ans - a*b + c*a
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = []
C = []

X = np.array([0]*(10**5))
Y = np.array([i  for i in range(1, (10**5)+1)])

for j in range(N):
    X[A[j]-1] += 1

for i in range(Q):
    b, c = map(int, input().split())
    a = X[b-1]
    X[b-1] = 0
    X[c-1] += a
    print(X@Y)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
X = [0] * (10**5)

for i in range(N):
    X[A[i]-1] += 1

ans = sum(A)

for j in range(Q):
    b, c = map(int, input().split())
    a = X[b-1]
    X[b-1] = 0
    X[c-1] += a
    ans = ans - a*b + c*a
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
A.sort()
Q = int(input())
B = []
C = []

for i in range(Q):
    b, c = map(int, input().split())
    B.append(b)
    C.append(c)

for i in range(Q):
    s = 0
    e = N
    h = int((s+e)/2)
    while A[h] != B[i] and s != h :
        if A[h] < B[i]:
            s = h
            h = int((s+e)/2)
        elif A[h] > B[i]:
            e = h
            h = int((s+e)/2)
    for j in range(s, e):
        if A[j] == B[i]:
            A[j] = C[i]
    A.sort()
    print(sum(A))

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
X = [0] * (10**5)

for i in range(N):
    X[A[i]-1] += 1

ans = sum(A)

for j in range(Q):
    b, c = map(int, input().split())
    a = X[b-1]
    X[b-1] = 0
    X[c-1] += a
    ans = ans - a*b + c*a
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = []
C = []

X = np.array([0]*(10**5))
Y = np.array([i  for i in range(1, (10**5)+1)])

for j in range(N):
    X[A[j]-1] += 1

for i in range(Q):
    b, c = map(int, input().split())
    a = X[b-1]
    X[b-1] = 0
    X[c-1] += a
    print(X@Y)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
X = [0] * (10**5)

for i in range(N):
    X[A[i]-1] += 1

ans = sum(A)

for j in range(Q):
    b, c = map(int, input().split())
    a = X[b-1]
    X[b-1] = 0
    X[c-1] += a
    ans = ans - a*b + c*a
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = []
C = []

for i in range(Q):
    b, c = map(int, input().split())
    B.append(b)
    C.append(c)

X = np.array([0]*(10**5))
Y = np.array([i  for i in range(1, (10**5)+1)])

for j in range(N):
    X[A[j]-1] += 1

for i in range(Q):
    a = X[B[i]-1]
    X[B[i]-1] = 0
    X[C[i]-1] += a
    print(X@Y)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
X = [0] * (10**5)

for i in range(N):
    X[A[i]-1] += 1

ans = sum(A)

for j in range(Q):
    b, c = map(int, input().split())
    a = X[b-1]
    X[b-1] = 0
    X[c-1] += a
    ans = ans - a*b + c*a
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = []
C = []

for i in range(Q):
    b, c = map(int, input().split())
    B.append(b)
    C.append(c)

for i in range(Q):
    for j in range(N):
        if A[j] == B[i]:
            A[j] = C[i]
    print(sum(A))

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
X = [0] * (10**5)

for i in range(N):
    X[A[i]-1] += 1

ans = sum(A)

for j in range(Q):
    b, c = map(int, input().split())
    a = X[b-1]
    X[b-1] = 0
    X[c-1] += a
    ans = ans - a*b + c*a
    print(ans)