Buggy python submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum=sum(a)
a2,a3=[],[]
import bisect as bs

for i in set(a):
    a2.append(i)
    a3.append(a.count(i))

#print(sum)
#print(a2,a3)

from functools import lru_cache
@lru_cache(maxsize=None)
def idx(x):
    return bs.bisect_left(a2,x)

for i in range(q):
    b,c=map(int,input().split())
    if b in a2:
        b_idx=idx(b)
        if a3[b_idx]!=0:
            if c in a2:
                c_idx=idx(c)
            else:
                a2.append(c)
                a3.append(0)
                c_idx=-1
            sum+=(c-b)*a3[b_idx]
            a3[c_idx]+=a3[b_idx]
            a3[b_idx]=0
#        del a2[b_idx]
#        del a3[b_idx]
#    print(a2,a3)
    print(sum)

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum=sum(a)

count=[0]*(10**5+1)

for i in a:
    count[i]+=1

for i in range(q):
    b,c=map(int,input().split())
    if count[b]!=0:
        sum+=(c-b)*count[b]
        count[c]+=count[b]
        count[b]=0
    print(sum)

Buggy python submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum=sum(a)
a2,a3=[],[]
import bisect as bs

for i in set(a):
    a2.append(i)
    a3.append(a.count(i))

#print(sum)
#print(a2,a3)

from functools import lru_cache
@lru_cache(maxsize=None)
def idx(x):
    return bs.bisect_left(a2,x)

for i in range(q):
    b,c=map(int,input().split())
    if b in a2:
        b_idx=idx(b)
        if c in a2:
            c_idx=idx(c)
        else:
            a2.append(c)
            a3.append(0)
            c_idx=-1
        sum+=(c-b)*a3[b_idx]
        a3[c_idx]+=a3[b_idx]
        a3[b_idx]=0
#        del a2[b_idx]
#        del a3[b_idx]
#    print(a2,a3)
    print(sum)

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum=sum(a)

count=[0]*(10**5+1)

for i in a:
    count[i]+=1

for i in range(q):
    b,c=map(int,input().split())
    if count[b]!=0:
        sum+=(c-b)*count[b]
        count[c]+=count[b]
        count[b]=0
    print(sum)

Buggy python submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum=sum(a)
a2,a3=[],[]
import bisect as bs

for i in set(a):
    a2.append(i)
    a3.append(a.count(i))

#print(sum)
#print(a2,a3)

def idx(x):
    return bs.bisect_left(a2,x)

for i in range(q):
    b,c=map(int,input().split())
    if b in a2:
        b_idx=idx(b)
        if c in a2:
            c_idx=idx(c)
        else:
            a2.append(c)
            a3.append(0)
            c_idx=-1
        sum+=(c-b)*a3[b_idx]
        a3[c_idx]+=a3[b_idx]
        del a2[b_idx]
        del a3[b_idx]
#    print(a2,a3)
    print(sum)

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum=sum(a)

count=[0]*(10**5+1)

for i in a:
    count[i]+=1

for i in range(q):
    b,c=map(int,input().split())
    if count[b]!=0:
        sum+=(c-b)*count[b]
        count[c]+=count[b]
        count[b]=0
    print(sum)

Buggy python submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum=sum(a)
a2,a3=[],[]
import bisect as bs

for i in set(a):
    a2.append(i)
    a3.append(a.count(i))

#print(sum)
#print(a2,a3)

def idx(l,x):
    return bs.bisect_left(l,x)

for i in range(q):
    b,c=map(int,input().split())
    if b in a2:
        b_idx=idx(a2,b)
        if c in a2:
            c_idx=idx(a2,c)
        else:
            a2.append(c)
            a3.append(0)
            c_idx=-1
        sum+=(c-b)*a3[b_idx]
        a3[c_idx]+=a3[b_idx]
        del a2[b_idx]
        del a3[b_idx]
#        a3[b_idx]=0
#    print(a2,a3)
    print(sum)

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum=sum(a)

count=[0]*(10**5+1)

for i in a:
    count[i]+=1

for i in range(q):
    b,c=map(int,input().split())
    if count[b]!=0:
        sum+=(c-b)*count[b]
        count[c]+=count[b]
        count[b]=0
    print(sum)

Buggy python submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum=sum(a)
a2,a3=[],[]
import bisect as bs

for i in set(a):
    a2.append(i)
    a3.append(a.count(i))

#print(sum)
#print(a2,a3)

def idx(l,x):
    return bs.bisect_left(l,x)

for i in range(q):
    b,c=map(int,input().split())
    if b in a2:
        b_idx=idx(a2,b)
        if c in a2:
            c_idx=idx(a2,c)
        else:
            a2.append(c)
            a3.append(0)
            c_idx=-1
        sum+=(c-b)*a3[b_idx]
        a3[c_idx]+=a3[b_idx]
        a3[b_idx]=0
#    print(a2,a3)
    print(sum)

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum=sum(a)

count=[0]*(10**5+1)

for i in a:
    count[i]+=1

for i in range(q):
    b,c=map(int,input().split())
    if count[b]!=0:
        sum+=(c-b)*count[b]
        count[c]+=count[b]
        count[b]=0
    print(sum)

Buggy python submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum=sum(a)
a2,a3=[],[]
import bisect as bs

for i in set(a):
    a2.append(i)
    a3.append(a.count(i))

#print(sum)
#print(a2,a3)

def idx(l,x):
    return bs.bisect_left(l,x)

for i in range(q):
    b,c=map(int,input().split())
    if b in a2 and c in a2:
        b_idx=idx(a2,b)
        c_idx=idx(a2,c)
        sum+=(c-b)*a3[b_idx]
        a3[c_idx]+=a3[b_idx]
        a3[b_idx]=0
    elif b in a2:
        a2.append(c)
        b_idx=idx(a2,b)
        sum+=(c-b)*a3[b_idx]
        a3.append(a3[b_idx])
        a3[b_idx]=0
#    print(a2,a3)
    print(sum)

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum=sum(a)

count=[0]*(10**5+1)

for i in a:
    count[i]+=1

for i in range(q):
    b,c=map(int,input().split())
    if count[b]!=0:
        sum+=(c-b)*count[b]
        count[c]+=count[b]
        count[b]=0
    print(sum)

Buggy python submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum=sum(a)
a2,a3=[],[]
import bisect as bs

for i in set(a):
    a2.append(i)
    a3.append(a.count(i))

for i in range(q):
    b,c=map(int,input().split())
    if b in a2 and c in a2:
        b_idx=bs.bisect_left(a2,b)
        c_idx=bs.bisect_left(a2,c)
        sum+=(c-b)*a3[b_idx]
        a3[c_idx]+=a3[b_idx]
        a3[b_idx]=0
    elif b in a2:
        a2.append(c)
        a3.append(0)
        b_idx=bs.bisect_left(a2,b)
        sum+=(c-b)*a3[b_idx]
        a3[-1]+=a3[b_idx]
        a3[b_idx]=0
    print(sum)

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum=sum(a)

count=[0]*(10**5+1)

for i in a:
    count[i]+=1

for i in range(q):
    b,c=map(int,input().split())
    if count[b]!=0:
        sum+=(c-b)*count[b]
        count[c]+=count[b]
        count[b]=0
    print(sum)

Buggy python submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum=sum(a)
a2,a3=[],[]
for i in set(a):
    a2.append(i)
    a3.append(a.count(i))

for i in range(q):
    b,c=map(int,input().split())
    if b in a2 and c in a3:
        sum+=(c-b)*a3[a2.index(b)]
        a3[a2.index(c)]+=a3[a2.index(b)]
        a3[a2.index(b)]=0
    elif b in a2:
        a2.append(c)
        a3.append(0)
        sum+=(c-b)*a3[a2.index(b)]
        a3[a2.index(c)]+=a3[a2.index(b)]
        a3[a2.index(b)]=0
    print(sum)

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum=sum(a)

count=[0]*(10**5+1)

for i in a:
    count[i]+=1

for i in range(q):
    b,c=map(int,input().split())
    if count[b]!=0:
        sum+=(c-b)*count[b]
        count[c]+=count[b]
        count[b]=0
    print(sum)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
for i in range(q):
    b,c=map(int,input().split())
    for i in range(n):
        if a[i]==b:
            a[i]=c
    print(sum(a))

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum=sum(a)

count=[0]*(10**5+1)

for i in a:
    count[i]+=1

for i in range(q):
    b,c=map(int,input().split())
    if count[b]!=0:
        sum+=(c-b)*count[b]
        count[c]+=count[b]
        count[b]=0
    print(sum)

Buggy python submission (verdict: Runtime Error):
a=list(map(int,input().split()))
q=int(input())
for i in range(q):
    b,c=map(int,input().split())
    for i in range(n):
        if a[i]==b:
            a[i]=c
    print(sum(a))

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum=sum(a)

count=[0]*(10**5+1)

for i in a:
    count[i]+=1

for i in range(q):
    b,c=map(int,input().split())
    if count[b]!=0:
        sum+=(c-b)*count[b]
        count[c]+=count[b]
        count[b]=0
    print(sum)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
for i in range(q):
    b,c=map(int,input().split())
    for i in range(n):
        if a[i]==b:
            a[i]=c
    print(sum(a))

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum=sum(a)

count=[0]*(10**5+1)

for i in a:
    count[i]+=1

for i in range(q):
    b,c=map(int,input().split())
    if count[b]!=0:
        sum+=(c-b)*count[b]
        count[c]+=count[b]
        count[b]=0
    print(sum)