Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

N=int(input())
A=list(map(int,input().split()))

import math
import sys

def get_sieve_of_eratosthenes(n):
    if not isinstance(n, int):
        raise TypeError('n is int type.')
    if n < 2:
        raise ValueError('n is more than 2')
    prime = []
    limit = math.sqrt(n)
    data = [i + 1 for i in range(1, n)]
    D=list(range(2,n+1))
    while True:
        l=len(data)
        p = data[0]
        if limit <= p:
            return D
        prime.append(p)
        temp=[]
        for i in range(l):
          if data[i]%p!=0:
            temp.append(data[i])
          else:
            D[data[i]-2]=p
        data=temp

B=get_sieve_of_eratosthenes(10**6)
        
def fac(n):
  s=set()
  k=n
  while k!=B[k-2]:
    s.add(B[k-2])
    k=k//B[k-2]
  s.add(k)
  return s

com=set()
g=0
for i in range(N):
  g=math.gcd(g,A[i])
  if com=={0}:
    continue
  if fac(A[i])&com==set():
    com=com.union(fac(A[i]))
  else:
    com={0}

if com!={0}:
  print("pairwise coprime")
  sys.exit
elif g==1:
  print("setwise coprime")
else:
  print("not coprime")

### FIXED CODE ###

import math

N=int(input())
A=list(map(int,input().split()))

a=max(A)
def era(n):
  prime=[]
  furui=list(range(2,n+1))
  while furui[0]<math.sqrt(n):
    prime.append(furui[0])
    furui=[i for i in furui if i%furui[0]!=0]
  return prime+furui

b=A[0]
for i in range(1,N):
  b=math.gcd(b,A[i])

if b!=1:
  print("not coprime")
  exit()

furui=era(10**6+7)
minfac=list(range(10**6+7))
for i in furui:
  for j in range(i,(10**6+7)//i):
    if minfac[i*j]==i*j:
      minfac[i*j]=i
      
dummy=[False]*(10**6+7)
for i in range(N):
  k=A[i]
  while k!=1:
    if dummy[minfac[k]]==True:
      print("setwise coprime")
      exit()
    else:
      dummy[minfac[k]]=True
      f=minfac[k]
      while k%f==0:
        k//=f
print("pairwise coprime")
### BUGGY CODE ###

time = list(map(int, input().split()))
h1,m1 = time[0],time[1]
h2,m2 = time[2],time[3]
k = time[4]
ans = 0
full = (h2*60+m2) - (h1*60+m1)
while full - k !=0:
    full-=k
    ans+=k
print(ans)


### FIXED CODE ###

h1,m1,h2,m2,k = list(map(int, input().split()))
h = h2 -h1
m = m2 - m1
print(h*60+m-k)
### BUGGY CODE ###

from collections import Counter
from scipy.misc import comb
mod=10**9+7
h,w,a,b=map(int,input().split())
ans=0
for i in range(b,w):
    ans=(ans+comb(h-a-1+i,i,exact=1)*comb(a-1+w-i-1,w-i-1,exact=1))%mod
print(ans)


### FIXED CODE ###

class BigCombination(object):
    __slots__ = ["mod", "factorial", "inverse"]
 
    def __init__(self, mod  = 10**9+7, max_n = 10**6):
        fac, inv = [1], []
        fac_append, inv_append = fac.append, inv.append
 
        for i in range(1, max_n+1):
            fac_append(fac[-1] * i % mod)
 
        inv_append(pow(fac[-1], mod-2, mod))
 
        for i in range(max_n, 0, -1):
            inv_append(inv[-1] * i % mod)
 
        self.mod, self.factorial, self.inverse = mod, fac, inv[::-1]
 
    def get_combination(self, n, r):
        return self.factorial[n] * self.inverse[r] * self.inverse[n-r] % self.mod
 
    def get_permutation(self, n, r):
        return self.factorial[n] * self.inverse[n-r] % self.mod
 
 
import math
Big = BigCombination()
mod=10**9+7
h,w,a,b=map(int,input().split())
ans=0
for i in range(b,w):
    ans=(ans+ Big.get_combination(h-a-1+i,i)* Big.get_combination(a-1+w-i-1,w-i-1))%mod
print(ans)
### BUGGY CODE ###

#!python3

# input
N, K = list(map(int, input().split()))


def main():
    x = N
    while True:
        y = max(x - K, K - x)
        if y < x:
            x = y
        else:
            break
    print(x)


if __name__ == "__main__":
    main()


### FIXED CODE ###

#!python3

# input
N, K = list(map(int, input().split()))


def main():
    s = N % K
    ans = min(s, K - s)
    print(ans)    


if __name__ == "__main__":
    main()
### BUGGY CODE ###

N = int(input())
# S = list(input())
S = input()

left = []
right = []
ans = 3*10**5

for i in range(N):
    left = S[0:i]
    right = S[i+1:]
#     print(list(left))
#     print(list(right))
    left_count = list(left).count("W")
#     print(left_count)
    right_count = list(right).count("E")
#     print(right_count)
    count = left_count + right_count
    ans = min(count, ans)
print(ans)


### FIXED CODE ###

N = int(input())
S = list(input())

# 一番左がリーダーの時
l = 0
r = S[1:].count('W')
ans = l + r
for i in range(1, N):
    if S[i-1] == 'E':  # 元リーダーの人が東向きの時
        l += 1
    if S[i] == 'W':  # 元リーダーが西向きの時
        r -= 1
    cnt = l+r
    ans = max(cnt, ans)  # 向きを変える必要のない人数
print(N - ans -1)  # 向きを変える必要のある人数
### BUGGY CODE ###

N,M,K=map(int,input().split())
*A,=map(int,input().split())
*B,=map(int,input().split())

AX=[0]*(N+1)
for i in range(1,N+1):AX[i]=AX[i-1]+A[i-1]

BX=[0]*(M+1)
for i in range(1,M+1):BX[i]=BX[i-1]+B[i-1]

import bisect
ni=bisect.bisect_right(AX,K)
mi=bisect.bisect_right(BX,K)
    
ans=0
for i in range(ni):
 for j in range(mi):
   if K < AX[i]+BX[j]:break
   ans=max(ans,i+j)
            
print(ans)

### FIXED CODE ###

N,M,K=map(int,input().split())
*A,=map(int,input().split())
*B,=map(int,input().split())

AX=[0]*(N+1)
na=N+1
for i in range(1,N+1):
    AX[i]=AX[i-1]+A[i-1]
    if K<AX[i]:na=min(na,i)

BX=[0]*(M+1)
nb=M+1
for i in range(1,M+1):
    BX[i]=BX[i-1]+B[i-1]
    if K<BX[i]:nb=min(nb,i)

import bisect        
ans=0
for i in range(na):
 j=bisect.bisect_right(BX,K-AX[i])-1
 # print(na,t,i,j)
 ans=max(ans,i+j)
            
print(ans)
### BUGGY CODE ###

N, W = map(int, input().split())
items = [list(map(int, input().split())) for _ in range(N)]

dp = [[0 for _ in range(W+1)] for _ in range(N+1)]

for i in range(N):
    for j in range(W+1):
        if j - items[i][0] >= 0:
            dp[i+1][j] = max(dp[i][j], dp[i][j - items[i][0]] + items[i][1])
        else:
            dp[i+1][j] = dp[i][j]
print(dp[N][W])


### FIXED CODE ###

n, w = map(int, input().split())
items = []
 
for i in range(n):
    items.append(list(map(int, input().split())))
 
 
def knapsack(n, w, items):
    c = [[0 for _ in range(w + 1)] for _ in range(n + 1)]
    g = [[0 for _ in range(w + 1)] for _ in range(n + 1)]
 
    for i in range(w + 1):
        c[0][i] = 0
        g[0][i] = 1
 
    for i in range(n + 1):
        c[i][0] = 0
 
    for i in range(1, n + 1):
        for j in range(1, w + 1):
            c[i][j] = c[i - 1][j]
            g[i][j] = 0
            item = items[i - 1]
            if item[0] > j:
                continue
            if item[1] + c[i - 1][j - item[0]] > c[i - 1][j]:
                c[i][j] = item[1] + c[i - 1][j - item[0]]
                g[i][j] = 1
 
    max_value = c[n][w]
    routes = []
    for i in range(n, 0, -1):
        if g[i][w] == 1:
            routes.append(i)
            w -= items[i - 1][0]
    return max_value, routes
 
 
result, _ = knapsack(n, w, items)
print(result)
### BUGGY CODE ###

import numpy as np

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

trains = np.zeros((M, N))
# 電車の都市区間が1始まりなことに注意
for i in range(M):
    l, r = list(map(int, input().split()))
    trains[i, l-1] = 1
    trains[i, r-1] = trains[i, r-1]+1


for j in range(Q):
    p, q = list(map(int, input().split()))
    t = trains[:, p-1:q]
    column_sum = np.sum(t, axis=1)
    a = column_sum[np.where(column_sum == 2)]
    print(np.size(a))

### FIXED CODE ###

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

trains = [[0]*N for j in range(N)]

for i in range(M):
    l, r = list(map(int, input().split()))
    trains[r-1][l-1] += 1

ans = [[0]*N for j in range(N)]
ans[0][0] = trains[0][0]

for e in range(1, N):  # endのe
    for s in reversed(range(N)):  # startのs
        if s == e:
            ans[e][s] = trains[e][s]
        else:
            ans[e][s] = sum(trains[e][s:]) + ans[e-1][s]


for i in range(Q):
    p, q = list(map(int, input().split()))
    print(ans[q-1][p-1])
### BUGGY CODE ###

import bisect
N = int(input())

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

List.sort()

a = []

for i in range(N):
  for j in range(i+1,N):
    a.append(List[i] + List[j])
    
a.sort()
ans = N * (N-1) * (N-2) / 6

for i in range(len(a)):
  b = a[i]
  c = bisect.bisect_left(List, b)
  ans -= (N-c)

print(int(ans))

### FIXED CODE ###

import bisect
N = int(input())

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

List.sort()

a = []

for i in range(N):
  for j in range(i+1,N):
    a.append(List[i] + List[j])
    
a.sort()
ans = N * (N-1) * (N-2) / 6

for i in range(len(a)):
  b = a[i]
  c = bisect.bisect_left(List, b)
  ans -= (N-c)

print(int(ans))
### BUGGY CODE ###

import numpy as np

n, k, q = map(int, input().split())
a = list(int(input()) for i in range(q))

p = np.zeros(n, dtype=int)

for i in range(q):
  ind = np.ones(n, dtype=bool)
  ind[a[i]-1] = False
  p[ind] -=1

for i in range(n):
  if k + p[i] > 0:
    print('Yes')
  else:
    print('No')

### FIXED CODE ###

import numpy as np

n, k, q = map(int, input().split())
a = [int(input()) for i in range(q)]

score = [k - q]*(n+1)

for i in range(q):
  score[a[i]] += 1

for i in range(1,n+1):
  if score[i] > 0:
    print('Yes')
  else:
    print('No')
### BUGGY CODE ###

import numpy as np
import statistics
H,W,M = map(int, input().split())
S = np.array([list([int(s) for s in input().split()]) for i in range(M)]) - 1
Hlist = S[:,0]
Wlist = S[:,1]
Hunique,Hcount = np.unique(Hlist, return_counts=True)
Wunique,Wcount = np.unique(Wlist, return_counts=True)
Hmode = np.array(list(Hunique[Hcount == max(Hcount)]))
Wmode = np.array(list(Wunique[Wcount == max(Wcount)]))
judge = -1
slist = []
for m in range(M):
  slist.append(list(S[m,:]))
for h in Hmode:
  for w in Wmode:
    if ([h,w] in slist) == False:
      judge = 0
      break
print(max(Hcount) + max(Wcount) + judge)

### FIXED CODE ###

def calc():
  H,W,M = map(int, input().split())
  s = []
  hcount = [0]*H
  wcount = [0]*W
  for m in range(M):
    h,w = map(int, input().split())
    h = h - 1
    w = w - 1
    hcount[h] = hcount[h] + 1
    wcount[w] = wcount[w] + 1
    s.append((h,w))
  hmax = max(hcount)
  wmax = max(wcount)
  hmode = [h for h, x in enumerate(hcount) if x == hmax]
  wmode = [w for w, y in enumerate(wcount) if y == wmax]
  s = tuple(s)
  S = set(s)
  for h in hmode:
    for w in wmode:
      if ((h,w) not in S):
        return hmax + wmax
  return hmax + wmax - 1
  
if __name__ == '__main__':
    print(calc())
### BUGGY CODE ###

from collections import defaultdict
d, g = list(map(int, input().split()))
pc = [list(map(int, input().split())) for _ in range(d)]

dp = defaultdict(int)
dp[0] = 0
for i, (p, c) in enumerate(pc):
    dp_cur = list(dp.items())
    for p_cur, n_cur in dp_cur:
        for n in range(1, p+1):
            if n < p:
                if dp[p_cur+n*100*(i+1)] == 0:
                    dp[p_cur+n*100*(i+1)] = n_cur+n
                else:
                    dp[p_cur+n*100*(i+1)] = min(dp[p_cur+n*100*(i+1)], n_cur+n)
                if p_cur+n*100*(i+1) > g:
                    break
            else:
                if dp[p_cur+n*100*(i+1)+c] == 0:
                    dp[p_cur+n*100*(i+1)+c] = n_cur+n
                else:
                    dp[p_cur+n*100*(i+1)+c] = min(dp[p_cur+n*100*(i+1)+c], n_cur+n)

# print(dp)
ans = 10**9
v = 0
for p, n in list(dp.items()):
    if p < g:
        continue
    else:
        ans = min(ans, n)
print(ans)


### FIXED CODE ###

d, g = list(map(int, input().split()))
pc = [list(map(int, input().split())) for _ in range(d)]

ans = 10**9
for bit in range(1<<d):
    score = 0
    n = 0
    
    for i in range(d): 
        if bit & 1<<i: # Problems completely answered
            score += pc[i][1] + pc[i][0]*100*(i+1)
            n += pc[i][0]

    if score >= g:
        ans = min(ans, n)
    else: # additional answering
        for i in range(d-1, -1, -1): # answer from high-score problem
            if bit & 1<<i: # already answered completely
                continue
            for j in range(pc[i][0]): # answer to remaining problems
                if score >= g:
                    break
                score += 100*(i+1)
                n += 1
        ans = min(ans, n)

print(ans)
### BUGGY CODE ###

import math

k = int( input() )
out_sum = 0

for a in range(1, k+1):
    for b in range(1, k+1):
        for c in range(1, k+1):
            out_sum += math.gcd( a, math.gcd(b,c) )

print( out_sum )

### FIXED CODE ###

import math

k = int( input() )
out_sum = 0

# a==b and b==c
for a in range(1, k+1):
    out_sum += a

# a==b and b!=c
for a in range(2, k+1):
    for b in range(1, a):
        out_sum += 6 * math.gcd(a,b)

# a!=b and b!=c
for a in range(3, k+1):
    for b in range(2, a):
        for c in range(1, b):
            out_sum += 6 * math.gcd( a, math.gcd(b,c) )

print( out_sum )
### BUGGY CODE ###

import sys
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import dijkstra

input = sys.stdin.readline
N = int(input())

P=set()
for _ in range(N - 1):
  a, b, c = map(int, input().split())
  P.add((c, a - 1, b - 1))

d, i, j = zip(*P)
G = csr_matrix((d, (i, j)),(N, N))

Q, K = map(int, input().split())
P = dijkstra(G, indices=K - 1, directed=False)
for _ in range(Q):
  x, y = map(int, input().split())
  print(int(P[x - 1] + P[y - 1]))

### FIXED CODE ###

from collections import deque

N = int(input())
ABC = [list(map(int, input().split())) for _ in range(N - 1)]
Q, K = map(int, input().split())
XY = [list(map(int, input().split())) for _ in range(Q)]

G = [[] * N for _ in range(N)]
for a, b, c in ABC:
  G[a - 1].append((b - 1, c))
  G[b - 1].append((a - 1, c))

D = [-1] * N
D[K - 1] = 0

q = deque()
q.append(K - 1)
while q:
  n = q.popleft()
  for i, c in G[n]:
    if D[i] < 0:
      q.append(i)
      D[i] = D[n] + c

for x, y in XY:
  print(D[x - 1] + D[y - 1])
### BUGGY CODE ###

n,m=map(int,input().split())
h=[int(input()) for i in range(n)]
h.sort()
v=[]
for i in range(n-m+1):
    a=min(h[i:m+i])
    b=max(h[i:m+i])
    v.append(b-a)
print(min(v))

### FIXED CODE ###

n,m=map(int,input().split())
h=[int(input()) for i in range(n)]
h.sort()
v=[]
for i in range(n-m+1):
    a=h[i]
    b=h[i+m-1]
    v.append(b-a)
    
print(min(v))
### BUGGY CODE ###

n = int(input())
s = input()
ans = {}

for i in range(n - 2):
    for j in range(i + 1, n - 1):
        for k in range(j + 1, n):
            ref = s[i] + s[j] + s[k]
            ans[ref] = 1

print(len(ans))

### FIXED CODE ###

from itertools import product

n = int(input())
s = input()
ans = 0

for i in product(range(10), repeat = 3):
    a = s.find(str(i[0]))
    if a != -1 and a != n - 1:
        b = s[a + 1:].find(str(i[1]))
        if b != -1:
            b += a + 1
            if b != n - 1:
                c = s[b + 1:].find(str(i[2]))
                if c != -1:
                    ans += 1
print(ans)
### BUGGY CODE ###

def prime_eratosthenes(n):
    prime_list = []
    num_list=[]
    for i in range(2, n+1):
        if i not in num_list:
            prime_list.append(i)
            for j in range(i*i, n+1, i):
                num_list.append(j)
    return prime_list

ls = prime_eratosthenes(100000)
ls2 = [i+1 for i in range(100000)]
print(ls2)
Q = int(input())
for i in range(Q):
    l, r = map(int, input().split())
    ans = 0
    for j in range(l-1, r):
        if (ls2[j] in ls) and (ls2[j+1]//2 in ls):
            ans += 1
    print(ans)

### FIXED CODE ###

q = int(input())
    
def is_prime(n):
    if n == 1:
        return False
    for i in range(2,int(n**0.5)+1):
        if n % i == 0:
            return False
    return True

ls = [0]

for i in range(1,100000):
    if is_prime(i) and is_prime((i+1)//2):
        ls.append(ls[i-1]+1)
    else:
        ls.append(ls[i-1])
        
for i in range(q):
    l,r = map(int,input().split())
    if l == r:
        if is_prime(l) and is_prime((l+1)//2):
            print(1)
        else:
          print(ls[r]-ls[l-1])  
    else:
        print(ls[r]-ls[l-1])
### BUGGY CODE ###

import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
from collections import deque
from bisect import bisect_left
from itertools import product
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def LSI(): return list(map(int, list(sys.stdin.readline().rstrip())))
def LSI2(N): return [list(map(int, list(sys.stdin.readline().rstrip()))) for i in range(N)]
def S(): return sys.stdin.readline().rstrip()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h): return [i for j in range(h)]
def FILL2(i,h,w): return [FILL(i,w) for j in range(h)]
def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)]
def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 998244353
INF = float("inf")
sys.setrecursionlimit(10**6+10)
input = sys.stdin.readline

N,S = MI()
A = LI()
dp = FILL2(0,N+1,S+1)
for i in range(N):
    for j in range(S+1):
        if j>0 and j+A[i]<=S:
            dp[i+1][j+A[i]] += dp[i][j]
        elif j==0 and A[i]<=S:
            dp[i+1][A[i]] += i+1
        dp[i+1][j] += dp[i][j]
#        dp[i+1][j] %= MOD
ans = 0
for i in range(N+1):
    ans += dp[i][S]
    ans %= MOD
print(ans)


### FIXED CODE ###

import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
from collections import deque
from bisect import bisect_left
from itertools import product
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def LSI(): return list(map(int, list(sys.stdin.readline().rstrip())))
def LSI2(N): return [list(map(int, list(sys.stdin.readline().rstrip()))) for i in range(N)]
def S(): return sys.stdin.readline().rstrip()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h): return [i for j in range(h)]
def FILL2(i,h,w): return [FILL(i,w) for j in range(h)]
def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)]
def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 998244353
INF = float("inf")
sys.setrecursionlimit(10**6+10)
input = sys.stdin.readline

N,S = MI()
A = LI()
dp = FILL2(0,N+1,S+1)
for i in range(N):
    for j in range(S+1):
        if j>0 and j+A[i]<=S:
            dp[i+1][j+A[i]] += dp[i][j]
            dp[i+1][j+A[i]] %= MOD
        elif j==0 and A[i]<=S:
            dp[i+1][A[i]] += i+1
            dp[i+1][A[i]] %= MOD
        dp[i+1][j] += dp[i][j]
        dp[i+1][j] %= MOD
ans = 0
for i in range(N+1):
    ans += dp[i][S]
    ans %= MOD
print(ans)
### BUGGY CODE ###

# -*- coding: utf-8 -*-
n = int(input())
l = list(map(int, input().split()))
l.sort()
total = 0
result = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        for k in range(j+1, n):
            if l[k] < l[i] + l[j]:
                result +=1
            total += 1
print(result)


### FIXED CODE ###

# -*- coding: utf-8 -*-
import bisect
n = int(input())
l = list(map(int, input().split()))
l.sort()
result = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        tmp = l[i] + l[j]
        ind = bisect.bisect_left(l, tmp)
        result += (ind-j-1)
print(result)
### BUGGY CODE ###

N = int(input())
s = [sorted(input()) for i in range(N)]

s_int = []
a_ord = ord('a')
for _s in s:
    v = list(map(lambda x: ord(x)-a_ord, _s))
    v = sum([v[i]*26**i for i in range(len(v))])
    s_int.append(v)

check = [0 for _ in range(N)]
cnt = 0
for i in range(N-1):
    cnt_i = 0
    for j in range(i+1, N):
        if (check[j] != 0):
            continue
        if (s_int[i] == s_int[j]):
            cnt_i += 1
            cnt += cnt_i
            check[j] = 1

print(cnt)


### FIXED CODE ###

from collections import defaultdict
N = int(input())
s = [''.join(sorted(input())) for i in range(N)]
d = defaultdict(lambda: 0)

for _s in s:
    d[_s] += 1

print(sum(map(lambda x: x * (x-1) // 2, d.values())))
### BUGGY CODE ###

A, B = map(int, input().split())
a = min(A, B)
b = max(A, B)
for i in range(a, b):
    if abs(a-i) == abs(b-i):
        print(i)
        break
else:
    print("IMPOSSIBLE")

### FIXED CODE ###

A, B = map(int, input().split())
a = min(A, B)
b = max(A, B)
if (b-a)%2 == 0:
    print(a+(b-a)//2)
else:
    print("IMPOSSIBLE")
### BUGGY CODE ###

n = int(input())
a = list(map(int,input().split()))
q = int(input())
for i in range(q):
    b,c = map(int,input().split())
    a = [i if i != b else c for i in a]
    print(sum(a))

### FIXED CODE ###

n = int(input())
a = list(map(int,input().split()))
dist = [0]*100000
ans = sum(a)
for i in a:
    dist[i-1] += 1

q = int(input())
for i in range(q):
    b,c = map(int,input().split())
    if dist[b-1] != 0:
        dist[c-1] += dist[b-1]
        ans += dist[b-1] * (c-b)
        dist[b-1] = 0
    print(ans)
### BUGGY CODE ###

import sys
def input(): return sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)

H, W = map(int, input().split())
S = [input() for _ in range(H)]
# print(S)
dr = [ 1, 0]
dc = [ 0, 1]
ans = 1001001001

def dfs(r,c,cnt,flg):
    """
    r, c
    cnt: 連続黒の回数
    flg: 1: 現在白（黒に来たらcnt+1）, 0: 現在黒
    """
    global ans
    if S[r][c] == '#':
        if flg:
            cnt += 1
            black_flg = 0
    else:
        flg = 1

    if r==H-1 and c ==W-1:
        ans = min(ans, cnt)
        return

    for dir in range(2):
        nr = r + dr[dir]
        nc = c + dc[dir]
        if nr >= H or nc >= W: continue
        dfs(nr, nc, cnt, flg)
    return

dfs(0,0,0,1)
print(ans)


### FIXED CODE ###

import sys
def input(): return sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)

H, W = map(int, input().split())
S = [input() for _ in range(H)]
# print(S)
INF = 1001001001

dp = [[INF]*W for _ in range(H)]
if S[0][0] == '#':
    dp[0][0] = 1
else:
    dp[0][0] = 0

for r in range(H):
    for c in range(W):
        if r:
            if S[r][c] == '#' and S[r-1][c] == '.':
                dp[r][c] = min(dp[r][c], dp[r-1][c]+1)
            else:
                dp[r][c] = min(dp[r][c], dp[r-1][c])
        if c:
            if S[r][c] == '#' and S[r][c-1] == '.':
                dp[r][c] = min(dp[r][c], dp[r][c-1]+1)
            else:
                dp[r][c] = min(dp[r][c], dp[r][c-1])

print(dp[H-1][W-1])
### BUGGY CODE ###

n,k,q = map(int, input().split())
A = [int(input()) for i in range(q)]

ANS = [k - q] * n

for i in range(n):
  if ANS[i] + A.count(i+1) > 0:
    print("Yes")
  else:
    print("No")

### FIXED CODE ###

n,k,q = map(int, input().split())
A = [int(input()) for i in range(q)]
S = [k] * n
for i in range(q):
  S[A[i]-1] += 1
  
ANS = [S[i]- q for i in range(n)]
for i in range(n):
  if ANS[i] <= 0:
    print("No")
  else:
    print("Yes")
### BUGGY CODE ###

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

a_cnt = {}
for i in a:
    if i in a_cnt:
        a_cnt[i] += 1
    else:
        a_cnt[i] = 1

cnt_list = [0] * len(a_cnt)
for i, value in enumerate(a_cnt.values()):
    cnt_list[i] = value

cnt_list.sort()

ans = 0
while len(cnt_list) > K:
    ans += cnt_list[0]
    cnt_list.pop(0)
print(ans)

### FIXED CODE ###

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

a_cnt = {}
for i in a:
    if i in a_cnt:
        a_cnt[i] += 1
    else:
        a_cnt[i] = 1
from  operator import itemgetter

a_cnt = sorted(a_cnt.items(), key=itemgetter(1))

from collections import deque
cnt_list = deque([])
for i, j in a_cnt:
    cnt_list.append(j)

ans = 0
while len(cnt_list) > K:
    ans += cnt_list[0]
    cnt_list.popleft()
print(ans)