Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
class UnionFind:
    __slots__ = ["_data_size", "_roots"]
 
    def __init__(self, data_size: int) -> None:
        self._data_size = data_size
        self._roots = [-1] * data_size
 
    def __getitem__(self, x: int) -> int:
        while self._roots[x] >= 0:
            x = self._roots[x]
        return x
 
    def is_connected(self, x: int, y: int) -> bool:
        return self[x] == self[y]
 
    def unite(self, x: int, y: int) -> None:
        x, y = self[x], self[y]
        if x == y:
            return
        if self._roots[x] > self._roots[y]:
            x, y = y, x
        self._roots[x] += self._roots[y]
        self._roots[y] = x
 
 
import sys
 
readline = sys.stdin.readline
 
N, Q = map(int, readline().split())
tree = UnionFind(N)
res = []
ans=0
for _ in range(Q):
  u, v = map(int, readline().split())
  if not tree.unite(u, v):
    res.append(int(tree.is_connected(u, v)))
    ans+=1
print(ans)


Fixed submission:
class UnionFind:
    __slots__ = ["_data_size", "_roots"]
 
    def __init__(self, data_size: int) -> None:
        self._data_size = data_size
        self._roots = [-1] * data_size
 
    def __getitem__(self, x: int) -> int:
        while self._roots[x] >= 0:
            x = self._roots[x]
        return x
 
    def is_connected(self, x: int, y: int) -> bool:
        return self[x] == self[y]
 
    def unite(self, x: int, y: int) -> None:
        x, y = self[x], self[y]
        if x == y:
            return
        if self._roots[x] > self._roots[y]:
            x, y = y, x
        self._roots[x] += self._roots[y]
        self._roots[y] = x
 
 
import sys
 
readline = sys.stdin.readline
 
N, Q = map(int, readline().split())
tree = UnionFind(N)
res = []
ans=0
for _ in range(Q):
  u, v = map(int, readline().split())
  tree.unite(u-1, v-1)
for u in range(1,N):
  if not tree.is_connected(u, 0):
      ans+=1
      tree.unite(u, 0)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
xy=[]
for i in range(n):
  tmp=list(map(int,input().split()))
  xy.append(tmp)
ans=0
for i in range(n):
  for j in range(i,n):
    tmp=abs(xy[i][0]-xy[j][0])+abs(xy[i][1]-xy[j][1])
    ans=max(ans,tmp)
print(ans)

Fixed submission:
n=int(input())
z=[]
w=[]
for i in range(n):
  x,y=map(int,input().split())
  z.append(x+y)
  w.append(x-y)
ans=max(max(z)-min(z), max(w)-min(w))
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import collections
N = int(input())
A = [int(x) for x in input().split()]
a = collections.Counter(A)
Q = int(input())
for i in range(Q):
  B, C = map(int, input().split())
  if B in a:
    a[C] += a[B]
    a[B] = 0
  tmp = 0
  for i in a:
    tmp += a[i] * i
  print(tmp)

Fixed submission:
N = int(input())
A = [int(x) for x in input().split()]
S =sum(A)
D = [0]*(10**5+1)
for a in A:
  D[a] += 1
Q = int(input())
for i in range(Q):
  b, c = map(int, input().split())
  S -= D[b]*b
  S += D[b]*c
  D[c] += D[b]
  D[b] = 0
  print(S)
Buggy python submission (verdict: Time Limit Exceeded):
N, K = list(map(int, input().split()))
if N >= K:
  ans = N
  for i in range(int(N/K)):
    ans = ans - K
  print(min(ans, max(ans-K, K-ans)))
else:
  print(min(N, max(N-K, K-N)))

Fixed submission:
N, K = list(map(int, input().split()))
if N >= K:
  N = N % K
print(min(N, max(N-K, K-N)))
Buggy python submission (verdict: Runtime Error):
A = [list(map(int, input().split())) for i in range(3)]
N = int(input())
b = [int(input()) for i in range(N)]
hole = [[0]*3 for i in range(3)]
ans = "No" #初期値

print(A)
print(N)
print(b)
print(hole)
print(ans)


for i in range(3):
  for j in range(3):
    for k in range(N):
      if A[i][j] == b[k]:
        hole[i][j] = 1 #穴あけ
  if A[i] == [1, 1, 1]:
    ans = "Yes" #横ビンゴ、i個目の要素（配列）はそろうので判別できる

flag_diag1 = 1
flag_diag2 = 1
for j in range(3):
  flag_vert = 1 #列ごとに初期化
  for i in range(3):
    flag_vert *= hole[i][j] #3つとも1のときだけ1
  if flag_vert ==1:
    ans = "Yes" #縦ビンゴ
  flag_diag1 *= hole[j][j] #＼
  flag_diag2 *= hole[j][3-j] #／
if flag_diag1 == 1 or flag_diag2 == 1:
  ans = "Yes" #斜めビンゴ
  
print(ans)


Fixed submission:
A = [list(map(int, input().split())) for i in range(3)]
N = int(input())
b = [int(input()) for k in range(N)]
hole = [[0,0,0] for i in range(3)]
ans = "No"

for i in range(3):
  for j in range(3):
    for k in range(N):
      if A[i][j] == b[k]:
        hole[i][j] = 1
  flag_hol = 1
  for j in range(3):
    flag_hol *= hole[i][j]
  if flag_hol == 1:
  	ans = "Yes"

flag_diag1 = 1
flag_diag2 = 1
for j in range(3):
  flag_vert = 1
  for i in range(3):
    flag_vert *= hole[i][j]
  if flag_vert ==1:
    ans = "Yes"
  flag_diag1 *= hole[j][j]
  flag_diag2 *= hole[j][2-j]
if (flag_diag1 == 1) or (flag_diag2 == 1):
  ans = "Yes"
  
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
B = [0] * N
for i in range(N):
  for j in range(N):
    if A[j] == i+1:
      B[i] = j+1
print(*B)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
B = [[] for i in range(N)]
for i in range(N):
  B[i] = [A[i], i+1]
B.sort()
ans = [0 for i in range(N)]
for i in range(N):
  ans[i] = B[i][1]
print(*ans)
Buggy python submission (verdict: Time Limit Exceeded):
import math
n, m = map(int, input().split())
a = list(map(int, input().split()))
for i in range(m):
  a.sort()
  a[-1] /= 2
  #print(a)
for i in range(n):
  a[i] = math.floor(a[i])
#print(a)
print(sum(a))


Fixed submission:
from collections import deque
n,m = list(map(int,input().split()))
a = list(map(int,input().split()))

A = deque(sorted(a,reverse=True))
B = deque([])

for _ in range(m):
    #if _ % (m//10) == 0: print(A, B)
    if A:
        if B:
            if (A[0] > B[0]):
                B.append(A.popleft()//2)
                continue
        else:
            B.append(A.popleft()//2)
            continue
    b = B.popleft()
    B.append(b//2)

print(sum(A)+sum(B))
Buggy python submission (verdict: Time Limit Exceeded):
import collections
n=int(input())
s=[input() for _ in range(n)]
dicti=[]
for i in range(n):
  tmp = collections.Counter(s[i])
  dicti.append(tmp)
ans=0
for i in range(n-1):
  for j in range(i+1,n):
    if dicti[i]==dicti[j]: ans+=1
print(ans)

Fixed submission:
import collections
N = int(input())
S = [sorted(input()) for _ in range(N)]
Ssrt = []
for s in S:
    s = ''.join(s)
    Ssrt.append(s)
cnt = collections.Counter(Ssrt)
ans = 0
for i in cnt.keys():
    if cnt[i] >= 2:
        ans += cnt[i]*(cnt[i]-1)//2
print(ans)
Buggy python submission (verdict: Runtime Error):
import collections
n = int(input())
v = [int(x) for x in input().split()]
v_odd, v_evn = [], []
for i in range(n):
  if i%2==0:
    v_odd.append(v[i])
  else:
    v_evn.append(v[i])
v_odd_cnt = collections.Counter(v_odd)
v_evn_cnt = collections.Counter(v_evn)
#print(v_odd_cnt, v_even_cnt)
max_kv_odd = max(v_odd_cnt.items(), key=lambda x: x[1])
max_kv_evn = max(v_evn_cnt.items(), key=lambda x: x[1])
if max_kv_odd[0] == max_kv_evn[0]:
  v_odd_cnt_tmp = v_odd_cnt
  v_evn_cnt_tmp = v_evn_cnt
  v_odd_cnt_tmp.pop(max_kv_odd[0])
  v_evn_cnt_tmp.pop(max_kv_evn[0])
  max_kv_odd_tmp = max(v_odd_cnt_tmp.items(), key=lambda x: x[1])
  max_kv_evn_tmp = max(v_evn_cnt_tmp.items(), key=lambda x: x[1])
  a = max_kv_odd[1] + max_kv_evn_tmp[1]
  b = max_kv_evn[1] + max_kv_odd_tmp[1]
  print(n-max(a, b))
  exit()
print(n-max_kv_odd[1]-max_kv_evn[1])
'''
v_odd = [0] * 10**5
veven = [0] * 10**5
for i in range(0, n//2+1, 2):
  v_odd[v[i]] += 1
  veven[v[i+1]] += 1
for i, x in enumerate(v_odd):

v_odd = {}
veven = {}
for i in range(0, n//2+1, 2):
  v_odd[v[i]] = v_odd.get(v[i], 0) + 1
  veven[v[i+1]] = veven.get(v[i+1], 0) + 1
'''

Fixed submission:
import collections
n = int(input())
v = [int(x) for x in input().split()]
v_odd, v_evn = [], []
for i in range(n):
  if i%2==0:
    v_odd.append(v[i])
  else:
    v_evn.append(v[i])
v_odd_cnt = collections.Counter(v_odd)
v_evn_cnt = collections.Counter(v_evn)
if len(v_odd_cnt) == len(v_evn_cnt) == 1:
  if v[0] == v[1]:
    print(n//2)
    exit()
  else:
    print(0)
    exit()
max_kv_odd = max(v_odd_cnt.items(), key=lambda x: x[1])
max_kv_evn = max(v_evn_cnt.items(), key=lambda x: x[1])
if max_kv_odd[0] == max_kv_evn[0]:
  if len(v_odd_cnt) == 1:
    v_evn_cnt.pop(max_kv_evn[0])
    max_kv_evn_tmp = max(v_evn_cnt.items(), key=lambda x: x[1])
    print(n-max_kv_odd[1]-max_kv_evn_tmp[1])
    exit()
  if len(v_evn_cnt) == 1:
    v_odd_cnt.pop(max_kv_odd[0])
    max_kv_odd_tmp = max(v_odd_cnt.items(), key=lambda x: x[1])
    print(n-max_kv_odd_tmp[1]-max_kv_evn[1])
    exit()
  v_odd_cnt_tmp = v_odd_cnt
  v_evn_cnt_tmp = v_evn_cnt
  v_odd_cnt_tmp.pop(max_kv_odd[0])
  v_evn_cnt_tmp.pop(max_kv_evn[0])
  max_kv_odd_tmp = max(v_odd_cnt_tmp.items(), key=lambda x: x[1])
  max_kv_evn_tmp = max(v_evn_cnt_tmp.items(), key=lambda x: x[1])
  a = max_kv_odd[1] + max_kv_evn_tmp[1]
  b = max_kv_evn[1] + max_kv_odd_tmp[1]
  print(n-max(a, b))
  exit()
print(n-max_kv_odd[1]-max_kv_evn[1])
Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
s='E'+input()+'W'
ans=n
for i in range(1,n+1):
  tmp=s[:i].count('W')+s[i+1:].count('E')
  ans=min(ans,tmp)
print(ans)

Fixed submission:
n=int(input())
s=input()
ans=s[1:].count('E') #soppo-count
tmp=ans
for i in range(1,n):
  if s[i-1]=='W': tmp+=1
  if s[i]=='E': tmp-=1
  ans=min(ans,tmp)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N = int(input())
D = deque(sorted(list(map(int, input().split()))))
M = int(input())
T = deque(sorted(list(map(int, input().split()))))
ans = 'YES'
for i in range(M):
  prb = T.pop()
  #print(D, T, prb)
  if prb in D:
    D.remove(prb)
  else:
    ans = 'NO'
    break
print(ans)


Fixed submission:
import collections
N = int(input())
D = collections.Counter(list(map(int, input().split())))
M = int(input())
T = collections.deque(sorted(list(map(int, input().split()))))
ans = 'YES'
for i in range(M):
  prb = T.pop()
  #print(prb, D)
  if prb in D:
    if D[prb]:
      D[prb] -= 1
      if not D[prb]:
        del D[prb]
    else:
      ans = 'NO'
      break
  else:
    ans = 'NO'
    break
print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())
K = int(input())
x = [int(input()) for i in range(N)]
ans = 0
for i in range(N):
    ans += min(2x[i], 2*(K-x[i]))
print(ans)

Fixed submission:
N = int(input())
K = int(input())
if N > 1:
  x = list(map(int, input().split()))
  ans = 0
  for i in range(N):
    ans += min(2*x[i], 2*(K-x[i]))
else: 
  x = int(input())
  ans = min(2*x, 2*(K-x))
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import copy
N = int(input())
a = [int(x) for x in input().split()]
a.sort()
ans = 1
for i in range(len(a)):
  cnt = 1
  b = copy.deepcopy(a)
  rm = a[i]
  b.remove(rm)
  cnt += b.count(rm-1)+b.count(rm+1)
  ans = max(ans, cnt)
print(ans)

Fixed submission:
N = int(input())
a = [int(x) for x in input().split()]
memo = [0]*(10**5+2)
ans = 1
for i in range(N):
  memo[a[i]] += 1
  memo[a[i]+1] += 1
  memo[a[i]-1] += 1
print(max(memo))
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
ans = 1; togU = 0; togD = 0
if a[1]>a[0]: togU = 1
elif a[1]<a[0]: togD = 1
for i in range(1,n):
  if not (togD or togU):
    if a[i]>a[i-1]: togU = 1; continue
    if a[i]<a[i-1]: togD = 1; continue
  if a[i]>a[i-1]:
    if togD:
      ans += 1
      togD = 0
      togU = 0
  elif a[i]<a[i-1]:
    if togU:
      ans += 1
      togU = 0
      togD = 0
print(ans)

Fixed submission:
n = int(input())
if n>1: a = list(map(int, input().split()))
else: a = int(input()); print(1); exit()
ans = 1; togU = 0; togD = 0
if a[1]>a[0]: togU = 1
elif a[1]<a[0]: togD = 1
for i in range(1,n):
  if not (togD or togU):
    if a[i]>a[i-1]: togU = 1; continue
    if a[i]<a[i-1]: togD = 1; continue
  if a[i]>a[i-1]:
    if togD:
      ans += 1
      togD = 0
      togU = 0
  elif a[i]<a[i-1]:
    if togU:
      ans += 1
      togU = 0
      togD = 0
print(ans)
Buggy python submission (verdict: Runtime Error):
class UnionFind():
    def __init__(self, n):
        self.parents = list(range(n))

    def find(self, x):
        if self.parents[x] == x:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        self.parents[y] = x
        
n,m = map(int,input().split())
uf = UnionFind(n)
for i in range(m):
    a,b=map(int,input().split())
    uf.union(a,b)
size = 0
for i in range(n):
    if uf.parents[i] == i:
        size += 1
print(size-1)

Fixed submission:
class UnionFind():
    def __init__(self, n):
        self.parents = list(range(n))

    def find(self, x):
        if self.parents[x] == x:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        self.parents[y] = x
        
n,m = map(int,input().split())
uf = UnionFind(n)
for i in range(m):
    a,b=map(int,input().split())
    uf.union(a-1,b-1)

size = 0
for i in range(n):
    if uf.parents[i] == i:
        size += 1
print(size-1)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
points=[]
summaxp=[]
summinp=[]
diffmaxp=[]
diffminp=[]

for i in range(n):
    x,y = map(int,input().split())
    if i == 0:
        summax = x+y
        summin = x+y
        diffmax = x-y
        diffmin = x-y
    if not [x,y] in points:
        points.append([x,y])
        if x+y > summax:
            summaxp=[[x,y]]
            summax = x+y
        elif x+y == summax:
            summaxp.append([x,y])
        if x+y < summin:
            summinp=[[x,y]]
            summin = x+y
        elif x+y == summin:
            summinp.append([x,y])
        if x-y > diffmax:
            diffmaxp=[[x,y]]
            diffmax = x-y
        elif x-y == diffmax:
            diffmaxp.append([x,y])
        if x-y < diffmin:
            diffminp=[[x,y]]
            diffmin = x-y
        elif x-y == diffmin:
            diffminp.append([x,y])

a = summax-summin
b = diffmax-diffmin
sol = max(a,b)
print(sol)

Fixed submission:
n = int(input())

for i in range(n):
    x,y = map(int,input().split())
    if i == 0:
        summax = x+y
        summin = x+y
        diffmax = x-y
        diffmin = x-y

    if x+y > summax:
        summax = x+y
    if x+y < summin:
        summin = x+y
    if x-y > diffmax:
        diffmax = x-y
    if x-y < diffmin:
        diffmin = x-y

a = summax-summin
b = diffmax-diffmin
sol = max(a,b)
print(sol)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
points=[]
summaxp=[]
summinp=[]
diffmaxp=[]
diffminp=[]

for i in range(n):
    x,y = map(int,input().split())
    if i == 0:
        summax = x+y
        summin = x+y
        diffmax = x-y
        diffmin = x-y
    if not [x,y] in points:
        points.append([x,y])
        if x+y > summax:
            summaxp=[[x,y]]
            summax = x+y
        elif x+y == summax:
            summaxp.append([x,y])
        if x+y < summin:
            summinp=[[x,y]]
            summin = x+y
        elif x+y == summin:
            summinp.append([x,y])
        if x-y > diffmax:
            diffmaxp=[[x,y]]
            diffmax = x-y
        elif x-y == diffmax:
            diffmaxp.append([x,y])
        if x-y < diffmin:
            diffminp=[[x,y]]
            diffmin = x-y
        elif x-y == diffmin:
            diffminp.append([x,y])

sol = 0
for p1 in summaxp:
    for p2 in summinp:
        kyori = abs(p1[0]-p2[0]) + abs(p1[1]-p2[1])
        if kyori > sol:
            sol = kyori

for p1 in diffmaxp:
    for p2 in diffminp:
        kyori = abs(p1[0]-p2[0]) + abs(p1[1]-p2[1])
        if kyori > sol:
            sol = kyori

print(sol)

Fixed submission:
n = int(input())

for i in range(n):
    x,y = map(int,input().split())
    if i == 0:
        summax = x+y
        summin = x+y
        diffmax = x-y
        diffmin = x-y

    if x+y > summax:
        summax = x+y
    if x+y < summin:
        summin = x+y
    if x-y > diffmax:
        diffmax = x-y
    if x-y < diffmin:
        diffmin = x-y

a = summax-summin
b = diffmax-diffmin
sol = max(a,b)
print(sol)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
A = list(map(int, input().split()))

mod = 10 ** 9 + 7
ans = 0

for k in range(n):
    if A[k] > 500000000 :
        A[k] = A[k] - mod

for j in range(n):
    for i in range(j):
        tmp = A[i]*A[j] % mod
        ans = ans+tmp
ans = ans % mod
print(ans)

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

mod = 10 ** 9 + 7

summ = 0
quadsum = 0

for k in range(n):
    summ = (summ + A[k]) % mod
    quadsum = (quadsum + A[k]*A[k])%mod

ans = (summ*summ)%mod - quadsum
if ans % 2 == 1:
    ans += mod
ans = (ans/2)%mod
print(int(ans))

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

mod = 10 ** 9 + 7
ans = 0

for k in range(n):
    A[k] = A[k] % mod

for j in range(n):
    for i in range(j):
        tmp = A[i]*A[j] % mod
        ans = (ans+tmp) % mod
print(ans)

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

mod = 10 ** 9 + 7

summ = 0
quadsum = 0

for k in range(n):
    summ = (summ + A[k]) % mod
    quadsum = (quadsum + A[k]*A[k])%mod

ans = (summ*summ)%mod - quadsum
if ans % 2 == 1:
    ans += mod
ans = (ans/2)%mod
print(int(ans))

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

mod = 10 ** 9 + 7
ans = 0
for j in range(n):
    for i in range(j):
        tmp = A[i]*A[j] % mod
        ans = (ans+tmp) % mod
print(ans)

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

mod = 10 ** 9 + 7

summ = 0
quadsum = 0

for k in range(n):
    summ = (summ + A[k]) % mod
    quadsum = (quadsum + A[k]*A[k])%mod

ans = (summ*summ)%mod - quadsum
if ans % 2 == 1:
    ans += mod
ans = (ans/2)%mod
print(int(ans))

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

sm=0

for i in range(n-1):
  for j in range(i+1,n):
    b=a[i]*a[j]
    sm+=b
    sm=sm%((10**9)+7)

print(sm)


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

sm=0
sumj=sum(a)

for i in range(n-1):
  sumj-=a[i]
  sm+=sumj*a[i]


print(sm%((10**9)+7))
Buggy python submission (verdict: Runtime Error):
x,k,d=map(int,input().split())

a=x//d

if a>=k:
  x=x-(d*k)
  print(abs(x))
else:
  x=x-(d*a)
  b=k-a
  if b%2==0:
    print(abs(x))
  else:


Fixed submission:
x,k,d=map(int,input().split())

a=abs(x)//d

if x<0:
  d=d*(-1)

if a>=k:
  x=x-(d*k)
  print(abs(x))
else:
  x=x-(d*a)
  b=k-a
  if abs(x)<abs(x-d):
    if b%2==0:
      print(abs(x))
    else:
      print(abs(x-d))
  else:
    x=x-d
    b-=1
    if b%2==0:
      print(abs(x))
    else:
      print(abs(x+d))
Buggy python submission (verdict: Time Limit Exceeded):
k=int(input())

s=7

cnt=1
m=0
while(1):
  t=s%k
  if t==0:
    print(cnt)
    break
  else:
    s=t*10 + 7

  if cnt==1:
    m=t

  elif m==t:
    print("-1")
    break
  cnt+=1


Fixed submission:
k=int(input())

s=7
cnt=1
m=0

while(1):

  t=s%k
  if m==0 and t!=s:
    m=t
  elif m==t:
    print("-1")
    break

  if t==0:
    print(cnt)
    break
  else:
    s=t*10 + 7

  cnt+=1
Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())

a=list(map(int,input().split()))

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

for i in range(n):
  z[a[i]]+=1

q=int(input())

y=[]


for i in range(q):
  b,c=map(int,input().split())
  z[c]+=z[b]
  z[b]=0
  #print(z)
  sam=0
  for j in range(len(z)):
    sam+=z[j]*j
  y.append(sam)

for i in range(len(y)):
  print(y[i])

Fixed submission:
n=int(input())

a=list(map(int,input().split()))

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

for i in range(n):
  z[a[i]]+=1

q=int(input())

y=[]

sam=sum(a)

for i in range(q):
  b,c=map(int,input().split())
  z[c]+=z[b]
  sam-=z[b]*b
  sam+=z[b]*c
  z[b]=0
  #print(z)
  
  y.append(sam)

for i in range(len(y)):
  print(y[i])
Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())

a=list(map(int,input().split()))

ans=1

for i in range(n):
  ans=ans*a[i]

if ans>(10**18):
  print("-1")
else:
  print(ans)

Fixed submission:
n=int(input())

a=list(map(int,input().split()))

ans=1

for i in range(n):
  if ans>0 or a[i]==0:
    ans=ans*a[i]
  if ans>10**18:
    ans=-1

print(ans)

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

n=int(input())

a=list(map(int,input().split()))

ans=1

for i in range(n):
  ans=ans*a[i]
  
if ans>10**18:
  print("-1")
else:
  print(ans)

Fixed submission:
n=int(input())

a=list(map(int,input().split()))

ans=1

for i in range(n):
  if ans>0 or a[i]==0:
    ans=ans*a[i]
  if ans>10**18:
    ans=-1

print(ans)
Buggy python submission (verdict: Runtime Error):
map(int,input().split())
h1,m1,h2,m2,k=map(int,input().split())

s=h1*60+m1
e=h2*60+m2

a=e-s

print(a-k)

Fixed submission:
h1,m1,h2,m2,k=map(int,input().split())

s=h1*60+m1
e=h2*60+m2

a=e-s

print(a-k)
Buggy python submission (verdict: Runtime Error):
import sys

k=int(input()
a,b=map(int,input().split())

for i in range(a,b+1):
  if i%k==0:
    print("OK")
    sys.exit()

print("NG")


Fixed submission:
import sys

k=int(input())
a,b=map(int,input().split())

for i in range(a,b+1):
  if i%k==0:
    print("OK")
    sys.exit()

print("NG")
Buggy python submission (verdict: Time Limit Exceeded):
a,b,n=map(int,input().split())

m=-1
x=0

for i in range(1,n+1):
  c=int(a*i/b)-a*int(i/b)

  if m<c or m==-1:
    m=c
    x=i

print(m)

Fixed submission:
a,b,n=map(int,input().split())
"""
m=-1

for i in range(b+1):
  w=int(a*i/b)-a*int(i/b)
  if m<w or m==-1:
    m=w
print(m)
"""
if b<=n:
  print(int(a*(b-1)/b)-a*int((b-1)/b))
else:
  print(int(a*n/b)-a*int(n/b))
Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())

s=[]
cnt=0

for i in range(n):
  a=str(input())
  if a in s:
    continue

  else:
    s.append(a)
    cnt+=1

print(cnt)

Fixed submission:
n=int(input())

s={}
cnt=0

for i in range(n):
  a=str(input())
  if a in s:
    continue

  else:
    s[a]=1
    cnt+=1

print(cnt)
Buggy python submission (verdict: Time Limit Exceeded):
n,k=map(int,input().split())

while(1):
  m=abs(n-k)

  if m>n:
    print(n)
    break
  else:
    n=m

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

x=n%k

y=abs(x-k)

if x<y:
  print(x)
else:
  print(y)

Buggy python submission (verdict: Runtime Error):
n,k=map(int,input().split())i

while(1):
  m=abs(n-k)

  if m>n:
    print(n)
    break
  else:
    n=m

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

x=n%k

y=abs(x-k)

if x<y:
  print(x)
else:
  print(y)
Buggy python submission (verdict: Time Limit Exceeded):
k,n=map(int,input().split())

a=list(map(int,input().split()))

mindis=0

for i in range(len(a)):
  cw=0
  ncw=a[i]
  nccw=a[i]
  ccw=0

  for j in range(1,len(a)):
    r=0
    q=0

    if ncw>a[(i+j)%len(a)]:
      cw+=((k-ncw)+a[(i+j)%len(a)])
      r=-1
    if nccw<a[i-j]:
      ccw+=(nccw+(k-a[i-j]))
      q=-1
    if r==0:
      cw+=abs(ncw-a[(i+j)%len(a)])
    if q==0:
      ccw+=abs(nccw-a[i-j])

    ncw=a[(i+j)%len(a)]
    nccw=a[i-j]
    #print(cw,ccw)

  if mindis==0:
    if cw<ccw:
      mindis=cw
    else:
      mindis=ccw

  if mindis>cw:
    mindis=cw

  if mindis>ccw:
    mindis=ccw
  #print()

print(mindis)

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

a=list(map(int,input().split()))

mindis=0

for i in range(len(a)):
  cw=0 #時計回り
  ccw=0#反時計

  if i==0:
    cw=a[len(a)-1]-a[0]
    ccw=k-(a[i+1]-a[i])

  elif i==len(a)-1:
    cw=k-(a[i]-a[i-1])
    ccw=a[i]-a[0]


  else:
    cw=k-(a[i+1]-a[i])
    ccw=k-(a[i]-a[i-1])

  #print(cw,ccw)

  if mindis==0:
    if ccw>cw:
      mindis=cw
    else:
      mindis=ccw

  if mindis>cw:
    mindis=cw
  if mindis>ccw:
    mindis=ccw

print(mindis)