Buggy python submission (verdict: Runtime Error):
import sys
from collections import Counter
input = sys.stdin.readline


N = int(input())
alist = list(map(int, input().split()))
Q = int(input())


sumA = sum(alist)
adict = Counter(alist)

for _ in range(Q):
    b, c = map(int, input().split())

    bnum = adict.pop(b)
    if c in adict:
        adict[c] += bnum
    else:
        adict[c] = bnum

    sumA -= bnum * (b - c)
    print(sumA)


Fixed submission:
import sys
from collections import Counter
input = sys.stdin.readline


N = int(input())
alist = list(map(int, input().split()))
Q = int(input())


sumA = sum(alist)
adict = Counter(alist)

for _ in range(Q):
    b, c = map(int, input().split())

    if b not in adict:
        print(sumA)
        continue

    bnum = adict.pop(b)
    if c in adict:
        adict[c] += bnum
    else:
        adict[c] = bnum

    sumA -= bnum * (b - c)
    print(sumA)

Buggy python submission (verdict: Runtime Error):
import sys
from collections import Counter
input = sys.stdin.readline


N = int(input())
alist = list(map(int, input().split()))
Q = int(input())


sumA = sum(alist)
adict = Counter(alist)

for _ in range(Q):
    b, c = map(int, input().split())

    bnum = adict.pop(b)
    if c in adict:
        adict[c] += bnum
    else:
        adict[c] = bnum

    sumA -= bnum * (b - c)
    print(sumA)


Fixed submission:
import sys
from collections import Counter
input = sys.stdin.readline


N = int(input())
alist = list(map(int, input().split()))
Q = int(input())


sumA = sum(alist)
adict = Counter(alist)

for _ in range(Q):
    b, c = map(int, input().split())

    if b not in adict:
        print(sumA)
        continue

    bnum = adict.pop(b)
    if c in adict:
        adict[c] += bnum
    else:
        adict[c] = bnum

    sumA -= bnum * (b - c)
    print(sumA)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import sys
input = sys.stdin.readline


N = int(input())
alist = list(map(int, input().split()))
Q = int(input())


alist.sort()
sumA = sum(alist)
for _ in range(Q):
    b, c = map(int, input().split())
    idx_bl = bisect.bisect_left(alist, b)
    idx_br = bisect.bisect_right(alist, b)

    alist = alist[:idx_bl] + alist[idx_br:]
    idx_cl = bisect.bisect_left(alist, c)
    alist = alist[:idx_cl] + [c] * (idx_br - idx_bl) + alist[idx_cl:]

    sumA -= (idx_br - idx_bl) * (b - c)
    print(sumA)


Fixed submission:
import sys
from collections import Counter
input = sys.stdin.readline


N = int(input())
alist = list(map(int, input().split()))
Q = int(input())


sumA = sum(alist)
adict = Counter(alist)

for _ in range(Q):
    b, c = map(int, input().split())

    if b not in adict:
        print(sumA)
        continue

    bnum = adict.pop(b)
    if c in adict:
        adict[c] += bnum
    else:
        adict[c] = bnum

    sumA -= bnum * (b - c)
    print(sumA)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import sys
input = sys.stdin.readline


N = int(input())
alist = list(map(int, input().split()))
Q = int(input())


alist.sort()
sumA = sum(alist)
for _ in range(Q):
    b, c = map(int, input().split())
    idx_bl = bisect.bisect_left(alist, b)
    idx_br = bisect.bisect_right(alist, b)

    alist = alist[:idx_bl] + alist[idx_br:]
    idx_cl = bisect.bisect_left(alist, c)
    alist = alist[:idx_cl] + [c] * (idx_br - idx_bl) + alist[idx_cl:]

    sumA -= (idx_br - idx_bl) * (b - c)
    print(sumA)


Fixed submission:
import sys
from collections import Counter
input = sys.stdin.readline


N = int(input())
alist = list(map(int, input().split()))
Q = int(input())


sumA = sum(alist)
adict = Counter(alist)

for _ in range(Q):
    b, c = map(int, input().split())

    if b not in adict:
        print(sumA)
        continue

    bnum = adict.pop(b)
    if c in adict:
        adict[c] += bnum
    else:
        adict[c] = bnum

    sumA -= bnum * (b - c)
    print(sumA)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import sys
input = sys.stdin.readline


N = int(input())
alist = list(map(int, input().split()))
Q = int(input())
BC = [list(map(int, input().split())) for _ in range(Q)]


alist.sort()
sumA = sum(alist)
for b, c in BC:
    idx_bl = bisect.bisect_left(alist, b)
    idx_br = bisect.bisect_right(alist, b)

    alist = alist[:idx_bl] + alist[idx_br:]
    idx_cl = bisect.bisect_left(alist, c)
    alist = alist[:idx_cl] + [c] * (idx_br - idx_bl) + alist[idx_cl:]

    sumA -= (idx_br - idx_bl) * (b - c)
    print(sumA)


Fixed submission:
import sys
from collections import Counter
input = sys.stdin.readline


N = int(input())
alist = list(map(int, input().split()))
Q = int(input())


sumA = sum(alist)
adict = Counter(alist)

for _ in range(Q):
    b, c = map(int, input().split())

    if b not in adict:
        print(sumA)
        continue

    bnum = adict.pop(b)
    if c in adict:
        adict[c] += bnum
    else:
        adict[c] = bnum

    sumA -= bnum * (b - c)
    print(sumA)