Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = [int(an) for an in input().split()]
q = int(input())

a_sum = sum(a)
for i in range(q):
    stdins = input().split()
    b = int(stdins[0])
    c = int(stdins[1])
    for j in range(n):
        if a[j] == b:
            a[j] = c
            a_sum = a_sum - b + c
    print(a_sum)


Fixed submission:
n = int(input())
a = input().split()

a_cnts = {}
a_sum = 0
for an in a:
    if an in a_cnts.keys():
        a_cnts[an] += 1
    else:
        a_cnts[an] = 1
    a_sum += int(an)

q = int(input())

for i in range(q):
    (b, c) = input().split()

    if b in a_cnts.keys():
        hit_cnt = a_cnts[b]
        del a_cnts[b]
        if c in a_cnts.keys():
            a_cnts[c] += hit_cnt
        else:
            a_cnts[c] = hit_cnt
        a_sum += (int(c) - int(b))*hit_cnt

    print(a_sum)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = [int(an) for an in input().split()]
q = int(input())

for i in range(q):
    stdins = input().split()
    for j in range(n):
        if a[j] == int(stdins[0]):
            a[j] = int(stdins[1])
    ans = sum([int(an) for an in a])
    print(ans)


Fixed submission:
n = int(input())
a = input().split()

a_cnts = {}
a_sum = 0
for an in a:
    if an in a_cnts.keys():
        a_cnts[an] += 1
    else:
        a_cnts[an] = 1
    a_sum += int(an)

q = int(input())

for i in range(q):
    (b, c) = input().split()

    if b in a_cnts.keys():
        hit_cnt = a_cnts[b]
        del a_cnts[b]
        if c in a_cnts.keys():
            a_cnts[c] += hit_cnt
        else:
            a_cnts[c] = hit_cnt
        a_sum += (int(c) - int(b))*hit_cnt

    print(a_sum)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = input().split()
q = int(input())

for i in range(q):
    stdins = input().split()
    for j in range(n):
        a[j] = a[j].replace(stdins[0], stdins[1])
    ans = sum([int(an) for an in a])
    print(ans)


Fixed submission:
n = int(input())
a = input().split()

a_cnts = {}
a_sum = 0
for an in a:
    if an in a_cnts.keys():
        a_cnts[an] += 1
    else:
        a_cnts[an] = 1
    a_sum += int(an)

q = int(input())

for i in range(q):
    (b, c) = input().split()

    if b in a_cnts.keys():
        hit_cnt = a_cnts[b]
        del a_cnts[b]
        if c in a_cnts.keys():
            a_cnts[c] += hit_cnt
        else:
            a_cnts[c] = hit_cnt
        a_sum += (int(c) - int(b))*hit_cnt

    print(a_sum)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = input().split()
q = int(input())
b = []
c = []
for i in range(q):
    stdins = input().split()
    b.append(stdins[0])
    c.append(stdins[1])

for i in range(q):
    for j in range(n):
        a[j] = a[j].replace(b[i], c[i])
    ans = sum([int(an) for an in a])
    print(ans)


Fixed submission:
n = int(input())
a = input().split()

a_cnts = {}
a_sum = 0
for an in a:
    if an in a_cnts.keys():
        a_cnts[an] += 1
    else:
        a_cnts[an] = 1
    a_sum += int(an)

q = int(input())

for i in range(q):
    (b, c) = input().split()

    if b in a_cnts.keys():
        hit_cnt = a_cnts[b]
        del a_cnts[b]
        if c in a_cnts.keys():
            a_cnts[c] += hit_cnt
        else:
            a_cnts[c] = hit_cnt
        a_sum += (int(c) - int(b))*hit_cnt

    print(a_sum)