Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
input_data = list(map(int,input().split()))
nums = [input_data[0],input_data[1]]
x = input_data[2]
cnt = 0
for i in range(nums[0],nums[1]+1):
    if i % x == 0:
        cnt += 1

print(cnt)

Fixed submission:
a,b,x = map(int,input().split())

print(b//x-(a-1)//x)
Buggy python submission (verdict: Runtime Error):
# union find, bfs

import sys
from collections import deque
si = sys.stdin.readline


def main():
    [n, m] = [int(e) for e in si().split()]
    if not m:
        print(1)
        return

    visited = [False]*(n+1)
    fm = [set() for _ in range(n+1)]
    mx, comp = 0, 0

    while m:
        m -= 1
        [a, b] = [int(e) for e in si().split()]
        fm[a].add(b)
        fm[b].add(a)

    for i in range(1, n+1):
        if visited[i]:
            continue
        visited[i] = True
        comp += 1
        q = deque()
        q.append(i)
        cnt = 1
        while q:
            curr = q.popleft()
            for e in fm[curr]:
                if not visited[e]:
                    visited[e] = True
                    q.append(e)
                    cnt += 1
        mx = max(mx, cnt)
    print(mx)

if __name__ == '__main__':
    main()


Fixed submission:
import sys
si = sys.stdin.readline


def main():
    [d, t, s] = [int(e) for e in si().split()]
    if d <= t*s:
        print('Yes')
    else:
        print('No')


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import sys
si = sys.stdin.readline


def main():
    n = int(si())
    l = [int(e) for e in si().split()]
    mod, ans = 10**9+7, 0
    for i in range(n):
        for j in range(i+1, n):
            ans = (ans+(l[i]*l[j]) % mod) % mod
    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
import sys
si = sys.stdin.readline


def main():
    n = int(si())
    l = [int(e) for e in si().split()]
    tot = sum(l)
    mod, ans = 10**9+7, 0
    for i in range(n-1):
        h = l[i]
        tot -= h
        ans = (ans+(tot*h) % mod) % mod
    print(ans)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import sys
si = sys.stdin.readline


def main():
    [x, k, d] = [int(e) for e in si().split()]
    if x == 0:
        if k % 2 == 0:
            print(0)
        else:
            print(abs(d))
    elif x < 0:
        while x < 0 and k:
            x += d
            k -= 1
        if k > 0:
            if k % 2:
                print(abs(x-d))
            else:
                print(x)
        else:
            print(abs(x))
    else:
        while x > 0 and k:
            x -= d
            k -= 1
        if k > 0:
            if k % 2:
                print(x+d)
            else:
                print(abs(x))
        else:
            print(abs(x))


if __name__ == '__main__':
    main()


Fixed submission:
import sys
si = sys.stdin.readline


def main():
    [x, k, d] = [int(e) for e in si().split()]
    if x == 0:
        if k % 2 == 0:
            print(0)
        else:
            print(abs(d))
    elif x < 0:
        mul = (0-x)//d
        if k <= mul:
            x += d*k
            print(abs(x))
            return
        else:
            x += d*mul
            k -= mul
        while x < 0 and k:
            x += d
            k -= 1
        if k > 0:
            if k % 2:
                print(abs(x-d))
            else:
                print(x)
        else:
            print(abs(x))
    else:
        mul = (x-0)//d
        if k <= mul:
            x -= d*k
            print(x)
            return
        else:
            x -= d*mul
            k -= mul
        while x > 0 and k:
            x -= d
            k -= 1
        if k > 0:
            if k % 2:
                print(x+d)
            else:
                print(abs(x))
        else:
            print(abs(x))


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
import sys
si = sys.stdin.readline


def main():
    n = int(si())
    ans = [0 for _ in range(n+1)]
    for x in range(1, 100):
        for y in range(1, 100):
            for z in range(1, 100):
                v = ((x+y+z)**2+x**2+y**2+z**2)//2
                if v > 10000:
                    break
                ans[v] += 1

    for i in range(n):
        print(ans[i+1])


if __name__ == '__main__':
    main()


Fixed submission:
import sys
from itertools import permutations as pm
from math import factorial as fact
si = sys.stdin.readline


def main():
    ans = dict()
    n = int(si())
    for x in range(1, 100):
        for y in range(x, 100):
            for z in range(y, 100):
                value = ((x+y+z)**2+x**2+y**2+z**2)//2
                if value > 10000:
                    break
                if x == y and y == z:
                    cases = 1
                elif x != y and y != z and z != x:
                    cases = 6
                else:
                    cases = 3
                if value in ans:
                    ans[value] += cases
                else:
                    ans[value] = cases

    for i in range(1, n+1):
        if i in ans:
            print(ans[i])
        else:
            print(0)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
import sys
si = sys.stdin.readline


def main():
    [n, k] = [int(e) for e in si().split()]
    trace = dict()
    for i in range(1, n+1):
        trace[i] = 0

    l = [int(e) for e in si().split()]
    i, cnt = 1, 0
    while k > 0:
        k -= 1
        cnt += 1
        i = l[i-1]
        if i == 1 or trace[i] != 0:
            cy = cnt-trace[i]
            break
        trace[i] = cnt
    k %= cy
    while k > 0:
        i = l[i-1]
        k -= 1
    print(i)


if __name__ == '__main__':
    main()


Fixed submission:
import sys
si = sys.stdin.readline


def main():
    [n, k] = [int(e) for e in si().split()]
    trace = dict()
    for i in range(1, n+1):
        trace[i] = 0

    l = [int(e) for e in si().split()]
    i, cnt = 1, 0
    while k > 0:
        k -= 1
        cnt += 1
        i = l[i-1]
        if i == 1 or trace[i] != 0:
            cy = cnt-trace[i]
            k %= cy
            break
        trace[i] = cnt
    while k > 0:
        i = l[i-1]
        k -= 1
    print(i)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
import sys
si = sys.stdin.readline


def main():
    [n, k] = [int(e) for e in si().split()]
    trace = dict()
    for i in range(n):
        trace[i] = 0

    l = [int(e) for e in si().split()]
    i, cnt = 0, 0
    while True and k > 0:
        k -= 1
        cnt += 1
        i = l[i]-1
        if i == 0 or trace[i] != 0:
            cy = cnt-trace[i]
            break
        trace[i] = cnt
    k %= cy
    while k > 0:
        i = l[i]-1
        k -= 1
    print(i+1)


if __name__ == '__main__':
    main()


Fixed submission:
import sys
si = sys.stdin.readline


def main():
    [n, k] = [int(e) for e in si().split()]
    trace = dict()
    for i in range(1, n+1):
        trace[i] = 0

    l = [int(e) for e in si().split()]
    i, cnt = 1, 0
    while k > 0:
        k -= 1
        cnt += 1
        i = l[i-1]
        if i == 1 or trace[i] != 0:
            cy = cnt-trace[i]
            k %= cy
            break
        trace[i] = cnt
    while k > 0:
        i = l[i-1]
        k -= 1
    print(i)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
from math import gcd
import sys
si = sys.stdin.readline
d = {}


def main():
    k = int(si())
    ans = 0
    for p in range(1, k+1):
        for q in range(1, k+1):
            for r in range(1, k+1):
                if (q, r) in d:
                    t1 = d[(q, r)]
                elif (r, q) in d:
                    t1 = d[(r, q)]
                else:
                    t1 = gcd(q, r)
                    d[(q, r)] = t1
                    d[(r, q)] = t1

                if (p, t1) in d:
                    t2 = d[(p, t1)]
                elif (t1, p) in d:
                    t2 = d[(t1, p)]
                else:
                    t2 = gcd(p, t1)
                    d[(p, t1)] = t2
                    d[(t1, p)] = t2
                ans += t2
    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
from math import gcd
import sys
si = sys.stdin.readline

# def gcd(u, v):
#     if v:
#         return gcd(v, u % v)
#     else:
#         return u


def main():
    k = int(si())
    cnt = 0
    for p in range(1, k+1):
        for q in range(1, k+1):
            for r in range(1, k+1):
                cnt += gcd(p, gcd(q, r))
    print(cnt)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
import sys

def main():
    l=sys.stdin.readline().split()
    print l[1]+l[0]

main()

Fixed submission:
import sys

def main():
    l=sys.stdin.readline().split()
    print(l[1]+l[0])

main()
Buggy python submission (verdict: Time Limit Exceeded):
# need to solve this by C++ to avoid TLE

import sys
si = sys.stdin.readline
so = sys.stdout.write


def main():
    n = int(si())
    l = [int(e) for e in si().split()]
    i = 1
    if i not in l:
        print(-1)
        return
    count = 0
    while i in l:
        idx_i = l.index(i)
        count += idx_i
        l = l[idx_i+1:]
        i += 1

    print(count+len(l))


main()


Fixed submission:
import sys
si = sys.stdin.readline
so = sys.stdout.write


def main():
    n = int(si())
    l = [int(e) for e in si().split()]

    bn = 1
    count = 0
    for i in range(n):
        if l[i] == bn:
            bn += 1
        else:
            count += 1

    print(count if count != n else -1)


if __name__ == '__main__':
    main()

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

def main():
    n=int(input())
    l=[int(e) for e in list(sys.stdin.readline().split())]
    i=1
    if i not in l:
        print(-1)
        return
    count=0
    while i in l:
        idx_i=l.index(i)
        count+=idx_i
        l=l[idx_i+1:]
        i+=1

    print(count+len(l))

main()

Fixed submission:
import sys
si = sys.stdin.readline
so = sys.stdout.write


def main():
    n = int(si())
    l = [int(e) for e in si().split()]

    bn = 1
    count = 0
    for i in range(n):
        if l[i] == bn:
            bn += 1
        else:
            count += 1

    print(count if count != n else -1)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
# atCoder Beginner Contest 147 a

import sys


def main():
    l = list(sys.stdin.readline().split())
    [int(e) for e in l]
    if sum(l) <= 21:
        print('win')
    else:
        print('bust')


main()


Fixed submission:
# atCoder Beginner Contest 147 a

import sys


def main():
    l = list(sys.stdin.readline().split())
    l = [int(e) for e in l]
    if sum(l) <= 21:
        print('win')
    else:
        print('bust')


main()

Buggy python submission (verdict: Runtime Error):
# atCoder Beginner Contest 147 a

import sys


def main():
    l = list(sys.stdin.readline().split())
    if sum(l) <= 21:
        print('win')
    else:
        print('bust')


main()


Fixed submission:
# atCoder Beginner Contest 147 a

import sys


def main():
    l = list(sys.stdin.readline().split())
    l = [int(e) for e in l]
    if sum(l) <= 21:
        print('win')
    else:
        print('bust')


main()
Buggy python submission (verdict: Runtime Error):
import sys


def main():
    n = input()
    n = int(n)
    l = {}
    lR = {}
    l = list(sys.stdin.readline().split())
    lR = l[::-1]
    givenLen = len(l)
    if l == lR:
        return 0

    temp = {}
    temp[0] = 0
    for i, e in enumerate(l):
        temp[i+1] = e
    l = dict(temp)
    temp = {}
    temp[0] = 0
    for i, e in enumerate(lR):
        temp[i+1] = e
    lR = dict(temp)

    LCStable = [[0 for _ in range(givenLen+1)]]*(givenLen+1)

    for i in l:
        for j in lR:
            if i==0 or j==0:
                LCStable[i][j]=0
            elif l[i]==lR[j]:
                LCStable[i][j]=LCStable[i-1][j-1]+1
            else:
                LCStable[i][j]=max(LCStable[i][j-1],LCStable[i-1][j])
    # print(LCStable)
    return givenLen-LCStable[-1][-1]


# if __name__ == '__main__':
#     print(main())

print(main())


Fixed submission:
def main():
    l = input()
    givenLen = len(l)
    if l==l[::-1]:
        return 0

    count=0
    for i in range(len(l)//2):
        if l[i]!=l[len(l)-1-i]:
            count+=1
    return count

print(main())
Buggy python submission (verdict: Runtime Error):
n = int(input())
s = input()
l = []
for i in range(len(s)):
  if s[i] == "R":
    l.append(i)
if l[0] < len(l):
  ans = 0
  for i in l:
    if i >len(l)-1:
      ans += 1
  print(ans)
else:
  print(len(l))

Fixed submission:
n = int(input())
s = input()
l = []
for i in range(len(s)):
  if s[i] == "R":
    l.append(i)
if l ==[]:
  print(0)
elif l[0] < len(l):
  ans = 0
  for i in l:
    if i >len(l)-1:
      ans += 1
  print(ans)
else:
  print(len(l))

Buggy python submission (verdict: Runtime Error):
n = int(input())
s = input()
l = []
for i in range(len(s)):
  if s[i] == "R":
    l.append(i)
if l[0] < len(l):
  ans = 0
  for i in l:
    if i >len(l):
      ans += 1
  print(ans)
	
else:
  print(len(l))

Fixed submission:
n = int(input())
s = input()
l = []
for i in range(len(s)):
  if s[i] == "R":
    l.append(i)
if l ==[]:
  print(0)
elif l[0] < len(l):
  ans = 0
  for i in l:
    if i >len(l)-1:
      ans += 1
  print(ans)
else:
  print(len(l))
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int,input().split()))
for i in range(1,n):
    print(a.count(i))
print(0)

Fixed submission:
import math
r = int(input())
p = math.pi
print(2*r*p)

Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int,input().split()))
for i in range(1,n):
    print(a.count(i))
print(0)

Fixed submission:
import math
r = int(input())
p = math.pi
print(2*r*p)
Buggy python submission (verdict: Runtime Error):
a = input()
lists = [i+1 for i in range(a)]
for t in lists:
  if t % 5 == 0:
    lists.remove(t)
  elif t % 3 ==0:
    lists.remove(t)
    
print(sum(lists))

Fixed submission:
a = int(input())
lists = [i+1 for i in range(a) if (i+1)%5!=0 and (i+1)%3!=0]

print(sum(lists))
Buggy python submission (verdict: Runtime Error):
import math
a,b,c = map(int,input().split())
if math.squrt(a)+math.squrt(b) < math.squrt(c):
  print('Yes')
else:
  print('No')

Fixed submission:
a,b,c=map(int,input().split())
if c-a-b>0 and 4*a*b<(c-a-b)**2:
    print('Yes')
else:
    print("No")

Buggy python submission (verdict: Runtime Error):
import math
a,b,c = map(int,input().split())
if a + b + 2*math.squrt(a*b) < c:
  print('Yes')
else:
  print('No')

Fixed submission:
a,b,c=map(int,input().split())
if c-a-b>0 and 4*a*b<(c-a-b)**2:
    print('Yes')
else:
    print("No")
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
import math
count = 0
for C in range(1,N):
    NC = N-C
    NCP = math.sqrt(NC)
    NN = math.floor(NCP)
    for i in range(1,NN+1):
        
        if NC%i == 0:
            count += 2
            if i == NCP:
                count -=1
print(count)
    

Fixed submission:
N = int(input())
count=0
for i in range(1,N+1):
    for j in range(1,N+1):
            
        if i*j >=N:
            count += j-1
            break
print(count)
Buggy python submission (verdict: Runtime Error):
N = int(input())
mod = 10**9+7
import math
NN = math.factorial(N+7)
U = (NN*(N+8)*(N+9))/362880
A = (NN*(N+8))/40320
C = NN/5040
S = int((U-2*A+C)%mod)
print(S)


Fixed submission:
N = int(input())
mod = 10**9+7
S = 10**N-2*(9**N)+8**N
print(S%mod)
Buggy python submission (verdict: Runtime Error):
S = input()
T = input()
lS = len(S)
lT = len(T)
C = []
for i in range(lS-lT):
    count = 0
    for j in range(lT):
        if S[i+j]!=T[j]:
            count += 1
    C.append(count)
print(min(C))           

Fixed submission:
S = input()
T = input()
lS = len(S)
lT = len(T)
C = []
for i in range(lS-lT+1):
    count = 0
    for j in range(lT):
        if S[i+j]!=T[j]:
            count += 1
    C.append(count)
print(min(C))

Buggy python submission (verdict: Runtime Error):
N=int(input())
A=list(map(int, input().split()))
p,S = 0,0
for i in range(N-1):
    ap = A.pop(0)
    AA = sum(A)
    aa = ap*AA
    a = aa%1000000007
    S += a
p = S%1000000007
print(p)

Fixed submission:
S = input()
T = input()
lS = len(S)
lT = len(T)
C = []
for i in range(lS-lT+1):
    count = 0
    for j in range(lT):
        if S[i+j]!=T[j]:
            count += 1
    C.append(count)
print(min(C))
Buggy python submission (verdict: Runtime Error):
H, W = map(int, input().split())
Ch, Cw = map(int, input().split())
Dh, Dw = map(int, input().split())
S=[list(map(str, list(input()))) for i in range(H)]

ah = Ch
aw = Cw
count = 0

while (ah != Dh) or (aw != Dw):
    h = Dh - ah
    w = Dw - aw
    if h > 0:
        if S[ah+1][aw] == '.':
            ah = ah + 1
    if h < 0:
        if S[ah-1][aw] == '.':
            ah = ah - 1
    if w > 0:
        if S[ah][aw+1] == '.':
            aw = aw + 1
    if w < 0:
        if S[ah][aw-1] == '.':
            aw = aw - 1
print(count)    

Fixed submission:
N, X, T = map(int, input().split())
import math
a = math.ceil(N/X)
b = a*T
print(b)
Buggy python submission (verdict: Runtime Error):
N = input()
L = list(input())
count = 0

for i in range(N-2):
    Li = L[i]
    for j in range(i+1, N-1):
        Lj = L[j]
        for k in range(j+1, N):
            Lk = L[k]
            if (Li == Lj) or (Lj == Lk) or (Lk == Li):
                continue
            else:
                if ((Li + Lj) >= Lk) and ((Li + Lk) >= Lj) and ((Lk + Lj) >= Li):
                    count = count + 1
print(count)

Fixed submission:
N =int(input())
L=list(map(int, input().split()))
LL = sorted(L)
count=0
for i in range(N-2):
    for j in range(i+1,N-1):
        for k in range(j+1,N):
            if LL[i]+LL[j]>LL[k]:
                if (LL[i]!=LL[j])and(LL[j]!=LL[k]):
                    count += 1
print(count)
Buggy python submission (verdict: Runtime Error):
# 088 B
N = int(input())
a = list(map(int, input().split()))
A = 0
B = 0
aa = sorted(a)
for i in range(0, N, 2):
    A += aa[-i-1]
    B += aa[-i-2]
print(A-B)


Fixed submission:
# 088 B
N = int(input())
a = list(map(int, input().split()))
A = 0
B = 0
aa = sorted(a)
for i in range(0, N, 2):
    A += aa[-i-1]
    if (i+2)<=N:
        B += aa[-i-2]
print(A-B)
Buggy python submission (verdict: Runtime Error):
# 085 C
N,Y = map(int, input().split())
for x in range(N):
    for y in range(N):
        for z inrange(N):
            if ((10000*x + 5000*y + 1000*z) == Y) and ((x + y + z) == N):
                print(x' 'y' 'z' ')
                exit()
print('-1 -1 -1')

Fixed submission:
# 085 C
N,Y = map(int, input().split())
x,y,z = -1,-1,-1
for i in range(N+1):
    for j in range(N+1):
        if ((10000*i + 5000*j + 1000*(N-i-j)) == Y)and(N-i-j>=0):
            x,y = i,j
            z = N - i - j
            break
    else:
        continue
    break
print(x,y,z)
Buggy python submission (verdict: Runtime Error):
import numpy as np

S=input()
T=input()

max_score = 0
for i in range(len(list(S))-len(list(T))):
    tmp_T = np.array(list(T))
    tmp_S = np.array(list(S))[i:i+len(tmp_T)]
    score = np.zeros(len(tmp_T))
    score[tmp_T==tmp_S]=1
    score = np.sum(score)
    if max_score < score:
        max_score=score
print(int(len(tmp_T)-max_score))


Fixed submission:
import numpy as np

S=input()
T=input()

length = len(list(T))
S_length = len(list(S))

max_score = 0
for i in range(len(list(S))-len(list(T))):
    tmp_T = np.array(list(T))
    tmp_S = np.array(list(S))[i:i+len(tmp_T)]
    score = np.zeros(len(tmp_T))
    score[tmp_T==tmp_S]=1
    score = np.sum(score)
    if max_score < score:
        max_score=score

if S_length==length:
    tmp_T = np.array(list(T))
    tmp_S = np.array(list(S))
    score = np.zeros(len(tmp_T))
    score[tmp_T==tmp_S]=1
    max_score = np.sum(score)
print(int(length-max_score))
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
As = list(map(int, input().split()))

result = 0
for i in range(int(N)-1):
    result+=As[i]*sum(As[i+1:])
print(result%(1*10**9+7))


Fixed submission:
N = int(input())
As = list(map(int, input().split()))

result = 0
before_sum = sum(As)
for i in range(N-1):
    before_sum-=As[i]
    result+=As[i]*(before_sum)
print(result%(1*10**9+7))

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

result = 0
for i in range(N-1):
    result+=As[i]*sum(As[i+1:])
print(result%(1*10**9+7))


Fixed submission:
N = int(input())
As = list(map(int, input().split()))

result = 0
before_sum = sum(As)
for i in range(N-1):
    before_sum-=As[i]
    result+=As[i]*(before_sum)
print(result%(1*10**9+7))

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

result = 0
for i in range(len(As)-1):
    result+=As[i]*sum(As[i+1:])
print(result%(1*10**9+7))


Fixed submission:
N = int(input())
As = list(map(int, input().split()))

result = 0
before_sum = sum(As)
for i in range(N-1):
    before_sum-=As[i]
    result+=As[i]*(before_sum)
print(result%(1*10**9+7))

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

result = 0
for i in range(len(As)-1):
    result+=As[i]*sum(As[i+1:])

print(result%(1*10**9+7))


Fixed submission:
N = int(input())
As = list(map(int, input().split()))

result = 0
before_sum = sum(As)
for i in range(N-1):
    before_sum-=As[i]
    result+=As[i]*(before_sum)
print(result%(1*10**9+7))

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

result = 0
for i in range(len(As)-1):
    for j in range(i+1, len(As)):
        result+=As[i]*As[j]

print(result%(1*10**9+7))


Fixed submission:
N = int(input())
As = list(map(int, input().split()))

result = 0
before_sum = sum(As)
for i in range(N-1):
    before_sum-=As[i]
    result+=As[i]*(before_sum)
print(result%(1*10**9+7))
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
A = list(map(int, input().split()))

diff = N-K
for i in range(N-diff, N):
    mul = 1
    if 0 in A[i-K:i]:
        print("No")
        continue
    for j in range(i-K, i):
        mul*=A[j]
    if mul<mul*A[i]/A[i-K]:
        print("Yes")
    else:
        print("No")


Fixed submission:
N, K = map(int, input().split())
As = list(map(int, input().split()))

for i in range(K, N):
    if As[i]/As[i-K]>1:
        print("Yes")
    else:
        print("No")
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))

dicts = {}
for tmp in a:
  if tmp in dicts.keys():
    dicts[tmp] += 1
  else:
    dicts[tmp] = 1
    
m = int(input())
result = 0
for key, value in dicts.items():
  result += key*value

for _ in range(m):
  b,c = list(map(int, input().split()))
  if b in dicts.keys():
    tmp = dicts[b]
    del dicts[b]
    if c in dicts.keys():
      dicts[c] += tmp
    else:
      dicts[c] = tmp
    result = 0
    for key, value in dicts.items():
      result += key*value
    print(result)
  else:
    print(result)
  

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

dicts = {}
for tmp in a:
  if tmp in dicts.keys():
    dicts[tmp] += 1
  else:
    dicts[tmp] = 1
    
m = int(input())
result = 0
for key, value in dicts.items():
  result += key*value

for _ in range(m):
  b,c = list(map(int, input().split()))
  if b in dicts.keys():
    tmp = dicts[b]
    diff = dicts[b]*b
    add = tmp*c
    
    del dicts[b]
    if c in dicts.keys():
      dicts[c] += tmp
      
    else:
      dicts[c] = tmp
      
    result -= diff
    result += add
    print(result)
  else:
    print(result)