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

n = int(input())
abc=[]
for i in range(n-1):
    tmp = list(map(int,input().split()))
    abc.append(tmp)
q,k = map(int, input().split())
que = []
for i in range(q):
    tmp = list(map(int,input().split()))
    que.append(tmp)

from scipy.sparse.csgraph import shortest_path, dijkstra, floyd_warshall, bellman_ford, johnson
from scipy.sparse import csr_matrix

arr = [[0 for i in range(n)] for j in range(n)]
for a in abc:
    arr[a[0]-1][a[1]-1] = a[2]
    arr[a[1]-1][a[0]-1] = a[2]

dist = dijkstra(csr_matrix(arr),indices=[k-1])

for tmp in que:
    print(int(dist[0][tmp[0]-1] + dist[0][tmp[1]-1]))

### FIXED CODE ###

n = int(input())
abc=[]
for i in range(n-1):
    tmp = list(map(int,input().split()))
    abc.append(tmp)
q,k = map(int, input().split())
que = []
for i in range(q):
    tmp = list(map(int,input().split()))
    que.append(tmp)

link = [[] for _ in range(n)]
for tmp in abc:
    link[tmp[0]-1].append([tmp[1]-1,tmp[2]])
    link[tmp[1]-1].append([tmp[0]-1,tmp[2]])

dist=[-1]*n

from collections import deque
Q = deque()
Q.append([k-1,0])

while Q:
    now,now_d=Q.popleft()

    for nxt in link[now]:
        if dist[nxt[0]]!=-1:
            continue
        nxt_d =nxt[1] + now_d
        dist[nxt[0]]=nxt_d
        Q.append([nxt[0],nxt_d])

for tmp in que:
    print(dist[tmp[0]-1] + dist[tmp[1]-1])
### BUGGY CODE ###

from itertools import accumulate
from operator import add

N,K = map(int,input().split())
A = map(int,input().split()) 
for k in range(K):
    B = [0] * N
    for i,x in enumerate(A):
        L = max(0, i-x)
        R = min(N-1, i+x)
        B[L] += 1
        if R+1 != N:
            B[R+1] -= 1
    T = accumulate(B,add) 
    if T == A:
        break
    A = T
print(*A)    

### FIXED CODE ###

from itertools import accumulate
#from operator import add

N,K = map(int,input().split())
A = map(int,input().split()) 
for k in range(K):
    B = [0] * N
    for i,x in enumerate(A):
        L = max(0, i-x)
        R = min(N-1, i+x)
        B[L] += 1
        if R+1 != N:
            B[R+1] -= 1
    #L = accumulate(B,add) 
    L = list(accumulate(B))[:]
    if L == A:
        break
    A = L
print(*A)
### BUGGY CODE ###

import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**18
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n,k=LI()
  l=LI()

  dp=[inf]*n
  dp[0]=0
  for i in range(1,n):
    for j in range(max(0,i-k),i):
      dp[i] = min(dp[i], dp[j] + abs(l[i]-l[j]))

  return dp[n-1]

print(main())


### FIXED CODE ###

import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n,k=LI()
  l=LI()

  dp=[inf]*100010
  dp[0]=0
  for i in range(n):
    for j in range(1,k+1):
      if i+j<n:
        dp[i+j]=min(dp[i+j],dp[i]+abs(l[i+j]-l[i]))

  return dp[n-1]

print(main())
### BUGGY CODE ###

def Dict_Choice(D):
    S=0
    for k in D:
        x=D[k]
        S+=(x*(x-1))//2
    return S

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

D={}
for i in range(N):
    if A[i] in D:
        D[A[i]]+=1
    else:
        D[A[i]]=1

for i in range(N):
    y=A[i]
    D[y]-=1
    print(Dict_Choice(D))
    D[y]+=1


### FIXED CODE ###

def Dict_Choice(D):
    S=0
    for k in D:
        x=D[k]
        S+=(x*(x-1))//2
    return S

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

D={}
for i in range(N):
    if A[i] in D:
        D[A[i]]+=1
    else:
        D[A[i]]=1

E={}
for k in D:
    x=D[k]
    E[k]=(x*(x-1))//2

S=0
for k in E:
    S+=E[k]

for i in range(N):
    print(S-(D[A[i]]-1))
### BUGGY CODE ###

n = int(input())
d = list(map(int,input().split()))
m = int(input())
t = list(map(int,input().split()))
s = set(t)
ans = "YES"
for i in s:
    if d.count(i) < t.count(i):
        ans = "NO"
print(ans)


### FIXED CODE ###

n = int(input())
d = list(map(int,input().split()))
m = int(input())
t = list(map(int,input().split()))

dd = {}
for i in range(n):
    if d[i] in dd:
        dd[d[i]] += 1
    else:
        dd[d[i]] = 1


ans = "YES"
for i in t:
    try:
        if dd[i] >= 1:
            dd[i] -= 1
        else:
            ans = "NO"
    except:
        ans="NO"
print(ans)
### BUGGY CODE ###

N=int(input())
A=sorted(list(map(int,input().split())))
X=[A[0]]
if N>1 and A[0]==A[1]:
  ans=0
else:
  ans=1
f=True
if A[0]==1:
  if N>1 and A[0]==A[1]:
    print(0)
  else:
    print(1)
  exit(0)
for i in range(1,N):
  if A[i]==A[(i+1)%N]:
    f=False
    continue
  
  F=True
  for x in X:
    if A[i]%x==0:
      F=False
      break
  if F:
    X.append(A[i])
    if f:
      ans+=1
  f=True
print(ans)

### FIXED CODE ###

N=int(input())
A=sorted(list(map(int,input().split())))
dp=[True for i in range(A[-1])]
ans=0
i=0
j=-1
while i<N:
  if i+1<N:
    while A[i]==A[i+1]:
      i+=1
      if i+1==N:
        break
  if dp[A[i]-1] and i-j==1:
    ans+=1
  for k in range(2*A[i],A[-1]+1,A[i]):
    dp[k-1]=False
  j=i
  i+=1
print(ans)
### BUGGY CODE ###

import collections
N=int(input())
A=list(map(int,input().split()))
sum=0
list=[]
list1={}

for i in range(N):
    if A[i] not in list:
        list.append(A[i])
        x=A[i]
        A[i]=0
        c=collections.Counter(A)
        for j in c.values():
            sum+=j*(j-1)/2
        print(int(sum))
        A[i]=x
        list1[A[i]]=sum
        sum=0
    else:
        print(int(list1[A[i]]))


### FIXED CODE ###

import collections
from operator import mul
from functools import reduce

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under


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

c=collections.Counter(A)
list=c.values()
result=0

for x in list:
    if x>1:
        result+=cmb(x,2)
for y in c.keys():
    c[y]=result-(c[y]-1)

for z in A:
    print(c[z])
### BUGGY CODE ###

from collections import Counter

mod = 10 ** 9 + 7

N, *A = map(int, open(0).read().split())

C = Counter()
for a in A:
    i = 2
    while i * i <= a:
        if a % i == 0:
            cnt = 0
            while a % i == 0:
                a //= i
                cnt += 1
            C[i] = max(C[i], cnt)
        i += 1
    if a > 1:
        C[a] = max(C[a], 1)

L = 1
for i, c in C.items():
    L *= pow(i, c, mod)
    L %= mod

ans = 0
for a in A:
    ans += pow(a, mod - 2, mod)
    ans %= mod

print(L * ans % mod)

### FIXED CODE ###

from fractions import gcd
from functools import reduce

def main():
    lcm = lambda a, b: a * b // gcd(a, b)
    mod = 10 ** 9 + 7

    N, *A = map(int, open(0).read().split())

    L = reduce(lcm, A)
    print(sum(L // a for a in A) % mod)

main()
### BUGGY CODE ###

import array
import itertools
from collections import defaultdict, deque


def log(s):
    # print("| " + str(s), file=sys.stderr)
    pass


def output(x):
    print(x, flush=True)


def input_ints():
    return map(int, input().split())


def solve():
    def fill(queue):
        footsteps = dict()

        s = None
        while queue:
            p, s = queue.popleft()
            footsteps[p] = s

            for pp in [(p[0] + 1, p[1]), (p[0] - 1, p[1]), (p[0], p[1] + 1), (p[0], p[1] - 1)]:
                if 0 <= pp[0] < width and 0 <= pp[1] < height and field[pp[1]][pp[0]] == '.' and pp not in footsteps:
                    queue.append((pp, s + 1))
                    footsteps[pp] = -1  # placeholder

        return s

    height, width = tuple(input_ints())
    field = [input() for _ in range(height)]
    queue = deque()

    for y, line in enumerate(field):
        for x, c in enumerate(line):
            if c == '#':
                queue.append(((x, y), 0))

    return fill(queue)


def main():
    print(solve())


main()


### FIXED CODE ###

import array
import itertools
from collections import defaultdict, deque


def log(s):
    # print("| " + str(s), file=sys.stderr)
    pass


def output(x):
    print(x, flush=True)


def input_ints():
    return map(int, input().split())


def solve():
    def fill(queue):
        footsteps = [[None] * width for _ in range(height)]

        for x, y in queue:
            footsteps[y][x] = 0

        s = 0
        while queue:
            queue2 = deque()
            s += 1
            while queue:
                x, y = queue.popleft()

                if x > 0 and footsteps[y][x - 1] is None:
                    queue2.append((x - 1, y))
                    footsteps[y][x - 1] = s
                if x < width - 1 and footsteps[y][x + 1] is None:
                    queue2.append((x + 1, y))
                    footsteps[y][x + 1] = s
                if y > 0 and footsteps[y - 1][x] is None:
                    queue2.append((x, y - 1))
                    footsteps[y - 1][x] = s
                if y < height - 1 and footsteps[y + 1][x] is None:
                    queue2.append((x, y + 1))
                    footsteps[y + 1][x] = s

            queue = queue2

        return s - 1

    height, width = tuple(input_ints())
    field = [input() for _ in range(height)]
    queue = deque()

    for y, line in enumerate(field):
        for x, c in enumerate(line):
            if c == '#':
                queue.append((x, y))
    # queue.append((10, 10))


    return fill(queue)


def main():
    print(solve())


main()
### BUGGY CODE ###

# coding: utf-8
# Your code here!

import copy
#import random

string = [x for x in input()]
#string = [random.randint(0, 1) for _ in range(int(1e5))]

concat_string = copy.copy(string)
count = 0
while True:
    num_delete = 0
    flags_delete = [False] * len(string)
    
    i = 0
    j = 1
    while True:
        if j >= len(concat_string) or i < 0:
            break
        if concat_string[i] != concat_string[j]:
            flags_delete[i] = True
            flags_delete[j] = True
            num_delete += 2
            
            if i == 0:
                i = j + 1
                j = i + 1
            else:
                if flags_delete[i] == False:
                    i = i - 1
                    j = j + 1
                else:
                    i = j + 1
                    j = i + 1
        else:
            i = j
            j = i + 1
            
    if num_delete == 0:
        break
    
    count += num_delete
    concat_string = [concat_string[i] for i in range(len(concat_string)) if flags_delete[i] == False]

print(count)

### FIXED CODE ###

# coding: utf-8
# Your code here!

import copy

string = [x for x in input()]

concat_string = copy.copy(string)
num_delete = 0

i = 0
j = 1
not_delete_list = []
while True:
    if j >= len(concat_string) or i < 0:
        break
    if concat_string[i] != concat_string[j]:
        num_delete += 2
        
        if i == 0:
            i = j + 1
            j = i + 1
        else:
            if len(not_delete_list) > 0:
                i = not_delete_list.pop(-1)
                j = j + 1
            else:
                i = j + 1
                j = i + 1
    else:
        not_delete_list.append(i)
        i = j
        j = i + 1


print(num_delete)
### BUGGY CODE ###

Q = int(input())
lr = []
for i in range(Q):
   lr.append(list(map(int, input().split())))

N = 100000
like_number = [3]
prime_number = [2]
for i in range(3, N, 2):
   flag = 0
   for j in range(3, int(i**0.5)+1, 2):
      if i%j == 0:
         flag = 1
         break
   if flag == 0:
      prime_number.append(i)
      if ((i+1)/2)%2 == 1:
         if (i+1)/2 in prime_number:
            like_number.append(i)
         

for i in range(Q):
   count = 0
   for j in like_number:
      if lr[i][1] >= j:
         if lr[i][0] <= j:
            count = count + 1
      else:
         break
   print(count)


### FIXED CODE ###

def main():
    Q = int(input())
    L = [0 for _ in range(Q)]
    R = [0 for _ in range(Q)]
    for i in range(Q):
        L[i], R[i] = map(int, input().split())

    N = 100000
    sosu = [1 for _ in range(N)]
    sosu[0] = 0
    for i in range(1,int(N**0.5)):
        k = i+1
        if sosu[i] == 1:
            j = k + k
            while j <= N:
                sosu[j-1] = 0
                j = j+k
    like = [0 for _ in range(N)]
    for i in range(2, N, 2):
        if sosu[i]==1 and sosu[(i+2)//2-1]==1:
            like[i] = 1
    like_wa = [0 for _ in range(N+1)]
    for i in range(N):
        like_wa[i+1] = like_wa[i] + like[i]

    for i in range(Q):
        print(like_wa[R[i]] - like_wa[L[i]-1])

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

def main():
    def solve(strings_max, rows_max, mapa):
        summa = maxs + maxr - 1
        if summa == h + w - 1 or summa == m:
            return summa
        for h_item in strings_max:
            for w_item in rows_max:
                if w_item not in mapa.get(h_item, []):
                    return summa + 1
        return summa


    h, w, m = map(int, input().split())
    mapa = {}
    strings = [0]*h
    rows = [0]*w
    for _ in range(m):
        h1, w1 = map(int, input().split())
        if not mapa.get(h1-1, 0):
            mapa[h1-1] = []
        mapa[h1-1].append(w1-1)
        strings[h1-1] += 1
        rows[w1-1] += 1
    
    maxs = max(strings)
    maxr = max(rows)
    strings_max = []
    rows_max = []
    for i in range(h):
        if strings[i] == maxs:
            strings_max.append(i)
    for i in range(w):
        if rows[i] == maxr:
            rows_max.append(i)
    
    ans = solve(strings_max, rows_max, mapa)
    print(ans)

main()


### FIXED CODE ###

def main():
    h, w, m = map(int, input().split())
    mapa = {}
    strings = [0]*h
    rows = [0]*w
    for _ in range(m):
        h1, w1 = map(int, input().split())
        if not mapa.get(h1-1, 0):
            mapa[h1-1] = []
        mapa[h1-1].append(w1-1)
        strings[h1-1] += 1
        rows[w1-1] += 1
    
    max_s = max(strings)
    max_r = max(rows)
    strings_max = []
    rows_max = []
    for i in range(h):
        if strings[i] == max_s:
            strings_max.append(i)
    for i in range(w):
        if rows[i] == max_r:
            rows_max.append(i)
    
    forbidden_positions = set(mapa[strings_max[0]])
    for item in strings_max[1:]:
        forbidden_positions.intersection_update(set(mapa[item]))

    answer_set = set(rows_max).difference(forbidden_positions)
    if answer_set:
        print(max_s + max_r)
    else:
        print(max_s + max_r - 1)

main()
### BUGGY CODE ###

n,k,q = map(int, input().split())
a = [input() for _ in range(q)]
for i in range(n):
    b = a.count(str(i+1))
    print("No" if q-b >= k else "Yes")

### FIXED CODE ###

n,k,q=map(int,input().split())
a=[0]*n
for _ in range(q):
    a[int(input())-1] += 1
c = sum(a)
for i in a:
    if (c-i)>=k:
        print("No")
    else:
        print("Yes")
### BUGGY CODE ###

N,M = map(int,input().split())
A = sorted(list(map(int,input().split())))
BC = []
for i in range(M):
    a = list(map(int,input().split()))
    BC.append(a)
BC = sorted(BC, key = lambda x:x[1],reverse = True)

for b,c in BC:
    count = b
    A = sorted(A)
    for i in range(len(A)):
        if A[i] < c and count != 0:
            A[i] = c
            count -= 1
        else:
            break

print(sum(A))

### FIXED CODE ###

def main():
    N,M = map(int,input().split())
    A = sorted(list(map(int,input().split())))
    BC = []
    for i in range(M):
        a = list(map(int,input().split()))
        BC.append(a)
    BC = sorted(BC, key = lambda x:x[1],reverse = True)
    sum = 0
    for b,c in BC:
        if sum + b <= N:
            sum += b
            for i in range(b):
                A.append(c)
        else:
            sub = N - sum
            for i in range(sub):
                A.append(c)
    A = sorted(A,reverse = True)
    suma = 0
    for i in range(N):
        suma += A[i]
    print(suma)    

main()
### BUGGY CODE ###

import sys
readline = sys.stdin.buffer.readline
read = sys.stdin.read
from collections import deque
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    if max(A) > 0:
        plus = True
    else:
        plus = False

    A.sort(reverse=True)

    if K%2==1 and plus==False:
        ans = 1
        for i in A[:K]:
            ans *= i % MOD
        ans %= MOD
    else:
        ans = 1
        left = 0
        right = N-1
        if K%2==1:
            ans *= A[left]
            left += 1
        for _ in range(K//2):
            if A[left]*A[left+1]>=A[right]*A[right-1]:
                ans *= A[left]*A[left+1]%MOD
                left += 2
            else:
                ans *= A[right]*A[right-1] % MOD
                right -= 2
        ans %= MOD

    print(ans)

if __name__ == '__main__':
    main()

### FIXED CODE ###

import sys
readline = sys.stdin.buffer.readline
read = sys.stdin.read
from collections import deque
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    n, k = map(int, readline().split())
    A = list(map(int, readline().split()))
    MOD = int(1e9) + 7
    A.sort()
    ans = 1
    if A[n - 1] == 0 and (k & 1):
        ans = 0
    elif A[n - 1] <= 0 and (k & 1):
        for i in range(n - 1, n - k - 1, -1):
            ans = (ans * A[i]) % MOD
    else:
        i = 0
        j = n - 1
        if k & 1:
            ans = (ans * A[j]) % MOD
            j -= 1
            k -= 1
        k >>= 1
        for itr in range(k):
            left_product = A[i] * A[i + 1]
            right_product = A[j] * A[j - 1]
            if left_product > right_product:
                ans = (ans * left_product) % MOD
                i += 2
            else:
                ans = (ans * right_product) % MOD
                j -= 2

    print(ans % MOD)

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

_,s,*a=map(int,open(0).read().split())
c,i,*d=[0]*6003
for a in a:
 for j in range(s,0,-1):d[a+j]+=d[j]
 d[a]-=~i;c+=d[s];i+=1
print(c%(998244353))

### FIXED CODE ###

n,s,*a=map(int,open(0).read().split())
d=[0]*3001
c=0
for i,a in enumerate(a):
    for j in range(s-a,0,-1):d[a+j]+=d[j]
    d[a]-=~i
    c+=d[s]
print(c%(998244353))
### BUGGY CODE ###

from collections import Counter

S = input()
S = list(map(int, S))

nums = S[::-1]
pow10 = 10
for i in range(1, len(nums)):
    nums[i] = nums[i - 1] + nums[i] * pow10
    pow10 *= 10

nums = [0] + [n % 2019 for n in nums]

counter = Counter(nums)
ans = 0
for count in counter.values():
    if count >= 2:
        ans += count * (count - 1) // 2

print(ans)


### FIXED CODE ###

S = input()
S = list(map(int, S))

t = 1
r = 0
d = [0] * 2019
d[0] += 1
for n in reversed(S):
    r = (r + n * t) % 2019
    t = t * 10 % 2019
    d[r] += 1

ans = 0
for m in d:
    ans += m * (m-1) // 2

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

x,k,d=input().split()
x=int(x)
k=int(k)
d=int(d)
i=1
while i*d<=abs(x):
  i+=1
if k<=i-1:
  print(abs(x)-d*k)
else:
  if (k-(i-1))%2==0:
    print(abs(x)-d*(i-1))
  else:
    print(abs(abs(x)-d*(i)))

### FIXED CODE ###

x,k,d=input().split()
x=abs(int(x))
k=int(k)
d=int(d)
a=x//d
b=x-a*d
c=(x-b)/d
i=int(c)
if k<=i:
  print(abs(x)-d*k)
else:
  if (k-(i))%2==0:
    print(abs(x)-d*(i))
  else:
    print(abs(abs(x)-d*(i+1)))
### BUGGY CODE ###

def main():
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    change = []
    process = []
    p_append = process.append

    for i in range(m):
        array = list(map(int, input().split()))
        p_append(array)

    process = sorted(process, key=lambda x: -x[1])
    for i in range(len(process)):
        change += [process[i][1]] * process[i][0]

    a = a + change
    a.sort(reverse=True)
    a = a[:n]
    print(sum(a))

if __name__ == '__main__':
    main()

### FIXED CODE ###

from operator import itemgetter

def main():
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    change = []
    process = []
    p_append = process.append

    for i in range(m):
        array = list(map(int, input().split()))
        p_append(array)

    process.sort(key = itemgetter(1),reverse=True)

    for i in range(len(process)):
        change += [process[i][1]] * process[i][0]
        if len(change) >= n:
            break

    a = a + change
    a.sort(reverse=True)
    a = a[:n]
    print(sum(a))

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

from collections import deque

X = int(input())
nums = deque([i for i in range(3, 10**5+10, 2)])

prime = 2
while prime < X:
    prime = nums.popleft()
    nums = deque([i for i in nums if i % prime != 0])
print(prime)

### FIXED CODE ###

X = int(input())

def check(num):
    max_div = int(num**0.5)
    for div in range(2, max_div+1):
        if num % div == 0:
            return False
    else:
        return True

num = X
while True:
    if check(num):
        print(num)
        break
    
    num += 1
### BUGGY CODE ###

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

i = 0
ans = 0
while i < n:
    if b[i]-b[(i-1)%n]-b[(i+1)%n] >= a[i]:
        b[i] =  b[i]-b[(i-1)%n]-b[(i+1)%n]
        ans += 1
        if a[i] == b[i] and a[(i+1)%n] == b[(i+1)%n] and a[(i-1)%n] == b[(i-1)%n]:
            if a == b:
                print(ans)
                exit()
        i = 0
    else:
        i += 1
print(-1)

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
 
i = 0
ans = 0
chg = 1
mul = 0
while chg == 1:
    chg = 0
    for i in range(n):
        if b[i]-b[i-1]-b[(i+1)%n] >= a[i]:
            mul = (b[i]-a[i])//(b[i-1]+b[(i+1)%n])
            b[i] -= (b[i-1]+b[(i+1)%n])*mul
            ans += mul
            chg = 1
print(ans if a==b else -1)
### BUGGY CODE ###

n, k = map(int, input().split())
a = []

for i in range(n):
    x, y = map(int, input().split())
    for j in range(y):
        a.append(x)

a.sort()
print (a[k-1])


### FIXED CODE ###

n, k = map(int, input().split())
a = []
count = 0

for i in range(n):
    a.append(list(map(int, input().split())))
a = sorted(a, key = lambda x: x[0])
i = 0
while count < k:
    count += a[i][1]
    ans = a[i][0]
    i += 1
print (ans)
### BUGGY CODE ###

N, K, Q, *A = map(int, open(0).read().split())

import numpy as np
scores = np.array([K]*N)

for a in A:
    scores -= 1
    scores[a-1] += 1

for s in scores:
    if s > 0:
        print("Yes")
    else:
        print("No")

### FIXED CODE ###

N, K, Q, *A = map(int, open(0).read().split())

import numpy as np
answered = np.array([0]*N)

for a in A:
    answered[a-1] += 1

for s in answered + K - len(A):
    if s > 0:
        print("Yes")
    else:
        print("No")
### BUGGY CODE ###

N=int(input())
A=[int(i) for i in input().split()]
arr={}
P={}
for a in A:
	if a in arr:
		arr[a]+=1
	else:
		arr[a]=1
for k,v in arr.items():
	P[k]=k*v
Q=int(input())
for q in range(Q):
	B,C=[int(i) for i in input().split()]
	if B in arr:
		if C in arr:
			arr[C]+=arr[B]
			P[C]+=arr[B]*C
		else:
			arr[C]=arr[B]
			P[C]=arr[B]*C
		del arr[B]
		del P[B]
	print(sum([i for i in P.values()]))


### FIXED CODE ###

N=int(input())
A=[int(i) for i in input().split()]
arr={}
P={}
for a in A:
	if a in arr:
		arr[a]+=1
	else:
		arr[a]=1
for k,v in arr.items():
	P[k]=k*v
S=sum([i for i in P.values()])
Q=int(input())
for q in range(Q):
	B,C=[int(i) for i in input().split()]
	if B in arr:
		if C in arr:
			arr[C]+=arr[B]
			P[C]+=arr[B]*C
		else:
			arr[C]=arr[B]
			P[C]=arr[B]*C
		S-=P[B]
		S+=arr[B]*C
		del arr[B]
		del P[B]
	print(S)