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

n, k = map(int, input().split())
A = list(map(lambda x: int(x)-1, input().split()))
bit = k.bit_length()
table = [[0]*n for _ in range(bit)]
table[0] = A

for i in range(1, bit):
    for j in range(n):
        table[i][j] = table[i-1][table[i-1][j]]

ans = 0
for i in reversed(range(bit)):
    if (1 << i) <= k:
        k -= 1 << i
        ans = table[i][ans]
print(ans+1)

### FIXED CODE ###

from collections import Counter
import sys
sys.setrecursionlimit(10**7)
n, k = map(int, input().split())
A = list(map(lambda x: int(x)-1, input().split()))

# A[0]から出発して何回でループするか数える

visited = [0]*n


def dfs(v):
    if visited[v]+1 == 3:
        return 0
    visited[v] += 1
    dfs(A[v])


dfs(0)

c = Counter(visited)
loop_cnt = c[2]
init_cnt = c[1]
k_ = k-init_cnt
k_ %= loop_cnt

cnt = 0
visited = [0]*n


ans = 0
loop = k if k < init_cnt else k_+init_cnt
for _ in range(loop):
    ans = A[ans]
print(ans+1)
### BUGGY CODE ###

import heapq
n,m=map(int,input().split())
a=list(map(int,input().split()))
heapq.heapify(a)

for i in range(m):
    b,c=map(int,input().split())
    for j in range(b):
        temp=heapq.heappop(a)
        if temp<c:
            heapq.heappush(a,c)
        else:
            heapq.heappush(a,temp)
print(sum(a))

### FIXED CODE ###

import sys
input=sys.stdin.readline
n,m=map(int,input().split())
a=sorted(list(map(int,input().split())))
bc=[list(map(int,input().split())) for i in range(m)]
bc=sorted(bc,reverse=True,key=lambda x:x[1])
c=[]

cnt=0
for B,C in bc:
    for i in range(B):
        if cnt==n:break
        cnt+=1
        c.append(C)
c=sorted(c,reverse=True)

ans=sum(a)
temp=ans
for i in range(n):
    if cnt<=i:continue
    temp+=-a[i]+c[i]
    if temp>ans:ans=temp
print(ans)
### BUGGY CODE ###

import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)

k = int(input())
total = 0
for i in range(1,k+1):
    for j in range(1,k+1):
        for l in range(1, k+1):
            total += gcd(i,j,l)
print(total)


### FIXED CODE ###

import math

k = int(input())
total = 0
for i in range(1,k+1):
    for j in range(1,k+1):
        sub_gcd = math.gcd(i,j)
        for l in range(1, k+1):
            total += math.gcd(sub_gcd, l)
print(total)
### BUGGY CODE ###

import sys
n,k = map(int,input().split())
m = n
c = 0
r = True
if n%k == 0:
    print(0)
    sys.exit()
while r == True:
    if k >= 2*m:
        r = False
    elif k >= m:
        c += 1
        r = False
    else:
        c += 1
        m -= k

print(abs(n-(c*k)))

### FIXED CODE ###

n,k = map(int,input().split())
a = [n%k,abs(n%k-k)]
print(min(a))
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**8)
def ii(): 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 dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value

## BFS

#入力受け取り
N, Q = mi()
x = li2(N-1)

cnt = [0]*N
for i in range(Q):
    p0, p1 = mi()
    cnt[p0-1] += p1

#隣接リスト作成
adj = [[] for i in range(N)]
for i in range(N-1):
    x[i][0] -= 1
    x[i][1] -= 1
    adj[x[i][0]].append(x[i][1])
    adj[x[i][1]].append(x[i][0])

global ans
ans = [0]*N

def dfs(fr, nx):
    ans[nx] += ans[fr] + cnt[nx]
    for v in adj[nx]:
        if v != fr:
            dfs(nx, v)

dfs(0, 0)

for k in ans:
    print(k, '', end='')

### FIXED CODE ###

import sys
def ii(): 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 dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
'''
from collections import deque
N, Q = mi()
D = li2(N-1)
px = li2(Q)
que = deque()

adj = [[] for i in range(N+1)]

for i in range(N-1):
    adj[D[i][0]].append(D[i][1])

ans = [0]*(N+1)
for j in range(Q):
    ind, a = px[j][0], px[j][1]
    que.append(ind)
    while(que!=deque()):
        v = que.popleft()
        ans[v] += a
        for e in adj[v]:
            que.append(e)


for i in range(1, N+1):
    print(ans[i], ' ', end='')
'''

from collections import deque
sys.setrecursionlimit(10**9)

#入力受け取り
N, Q = mi()
x = li2(N-1)
#p = li2(Q)

cnt = [0]*N
for i in range(Q):
    p0, p1 = mi()
    cnt[p0-1] += p1

#隣接リスト作成
adj = [[] for i in range(N)]
for i in range(N-1):
    x[i][0] -= 1
    x[i][1] -= 1
    adj[x[i][0]].append(x[i][1])
    adj[x[i][1]].append(x[i][0])

que = deque()
ans = [0]*N

#for a in range(N):
que.append((0, 0))
num = 0
while que!=deque():
    v = que.popleft()
    ans[v[1]] = ans[v[0]] + cnt[v[1]]
    for next in adj[v[1]]:
        if next != v[0]:
            que.append((v[1], next))

for k in ans:
    print(k, '', end='')
### BUGGY CODE ###

def power(m, n):
    if n == 1:return m
    return power(m * m, n // 2) * (m if n % 2 else 1) % 1000000007
    

from sys import stdin
readline = stdin.readline

m, n = map(int, readline().split())
print(power(m, n))

### FIXED CODE ###

def power(m, n):
    if n:return power(m * m % 1000000007, n // 2) * (m if n % 2 else 1) % 1000000007
    return 1


from sys import stdin
readline = stdin.readline

m, n = map(int, readline().split())
print(power(m, n))
### BUGGY CODE ###

N = int(input())
nums = [i for i in map(int, input().split(" "))]

target_count = 0
for index in range(N):
    target = nums[index]
    divisible_count = 0
    for num in nums:
        residue = target % num
        if residue ==0:
            divisible_count +=1
        if divisible_count>=2:
            break
    if divisible_count ==1:
        target_count+=1

print(target_count)

### FIXED CODE ###

N = int(input())
nums = [i for i in map(int, input().split())]
nums = sorted(nums)
max = nums[-1]
dp = [1]*(max+1)
ans = 0

for i in range(len(nums)-1):
    p = nums[i]
    if dp[p] ==1:
        for q in range(max//p+1):
            dp[p*q]=0
        if nums[i] != nums[i+1]:
            ans+=1
if dp[max]==1:
    ans+=1
print(ans)
### BUGGY CODE ###

import math

A, B, N = (int(x) for x in input().split())
h = []
a = 0
for i in range(0, N+1):
    a = math.floor(A * i / B) - A * math.floor(i / B)
    h.append(a)
print(max(h))

### FIXED CODE ###

import math

A, B, N = (int(x) for x in input().split())
ans = math.floor(A * min(B-1, N) / B)
print(ans)
### BUGGY CODE ###

#TLE
N, M = map(int, input().split())
a_list = list(map(int, input().split()))

for i in range(M):
    b, c = map(int, input().split())
    a_list += [c] * b

a_list.sort(reverse = True)

print(sum(a_list[:N]))


### FIXED CODE ###

#TLE
N, M = map(int, input().split())
a_list = list(map(int, input().split()))

written_nums = []
for i in range(M):
    b, c = map(int, input().split())
    written_nums.append((c, b))

written_nums.sort(reverse = True)
append_list = []
for c, b in written_nums:
    append_list += [c for _ in range(b)]

    if len(append_list) >= N:
        break

a_list += append_list
a_list.sort(reverse = True)

print(sum(a_list[:N]))
### BUGGY CODE ###

N = int(input())

A = sorted(list(map(int, input().split())))
B = sorted(list(map(int, input().split())))
C = sorted(list(map(int, input().split())))

import bisect

count_B = {}
count_C = {}


for b in B:
    count_C[b] = N - bisect.bisect_right(C, b)

count = 0
for a in A:
    for b in B[bisect.bisect_right(B, a):]:
        count += count_C[b]
        # print(a, b)
print(count)

### FIXED CODE ###

N = int(input())

A = sorted(list(map(int, input().split())))
B = sorted(list(map(int, input().split())))
C = sorted(list(map(int, input().split())))

import bisect

count_B = {}
count_C = {}
count_sum = {}

count = 0
for idx, b in enumerate(reversed(B)):
    count_C[b] = N - bisect.bisect_right(C, b)
    count += count_C[b]
    count_sum[N-idx-1] = count
# print(count_sum)
count = 0
for a in A:
    index = bisect.bisect_right(B, a)
    if index in count_sum:
        count += count_sum[bisect.bisect_right(B, a)]
        # print(a, b)
print(count)
### BUGGY CODE ###

N, Q = map(int,input().split())
S = str(input())
for i in range(Q):
    l, r = map(int,input().split())
    p = l-1
    S_tmp = S[p:r]
    S_tmp.strip("T")
    S_tmp.strip("G")
    print(S_tmp.count("AC"))

### FIXED CODE ###

N, Q = map(int,input().split())
S = str(input())
count = 0
li = [0]
for i in range(N-1):
    if S[i:i+2] == "AC":
        count += 1
    li.append(count)
for i in range(Q):
    l,r = map(int,input().split())
    print(li[r-1]-li[l-1])
### BUGGY CODE ###

import numpy as np

n, d, a = map(int, input().split())
lst = []
for i in range(n):
    xi, hi = map(int, input().split())
    if hi % a == 0:
        hi = hi // a
    else:
        hi = hi // a + 1
    lst.append([xi, hi])
lst.sort(key=lambda x: x[0])
x, h = [], []
for i in range(n):
    x.append(lst[i][0])
    h.append(lst[i][1])

x.append(10**11)
h.append(0)

h = np.array(h)

kougeki_start = 0
kougeki_end = 0
cnt = 0
for i in range(n):
    if h[i] > 0:
        left = i # 可能
        right = n # 不可能
        while left+1 < right:
            mid = (left + right) // 2
            if x[mid] - x[i] <= 2*d:
                left = mid
            else:
                right = mid
        cnt += h[i]
        h[i:left+1] -= h[i]

print(cnt)

### FIXED CODE ###

n, d, a = map(int, input().split())
lst = []
for i in range(n):
    xi, hi = map(int, input().split())
    if hi % a == 0:
        hi = hi // a
    else:
        hi = hi // a + 1
    lst.append([xi, hi])
lst.sort(key=lambda x: x[0])
x, h = [], []
for i in range(n):
    x.append(lst[i][0])
    h.append(lst[i][1])

x.append(10**11)
h.append(0)

# 攻撃に巻き込めるインデックスを求める
left = 0 # 可能
right = n # 不可能
while left+1 < right:
    mid = (left + right) // 2
    if x[mid] - x[0] <= 2*d:
        left = mid
    else:
        right = mid
makikomi = [left]
right = left
for i in range(1, n):
    while x[right] - x[i] <= 2*d:
        right += 1
    right -= 1
    makikomi.append(right)

cnt = [0 for i in range(n)]
left = 0
already = 0
for i in range(n):
    while x[makikomi[left]] < x[i]:
        already -= cnt[left]
        left += 1
    cnt[i] = max(h[i] - already, 0)
    already += cnt[i]

print(sum(cnt))
### BUGGY CODE ###

N, Q = map(int, input().split())
S = input()
q =[]
for i in range(Q):
    q.append(list(map(int, input().split())))
for i,j in q:
    tmp = S[i-1:j].replace("G","").replace("T","")
    print(tmp.count("AC"))

### FIXED CODE ###

N, Q = map(int, input().split())
S = input()
q =[]
temp = [0] * (N+1)
tmp = 0
for i in range(1,N):
    if S[i-1] == "A":
        if S[i] == "C":
            tmp +=1
    temp[i+1] = tmp
for j in range(Q):
    inp = input().split()
    print(temp[int(inp[1])] - temp[int(inp[0])])
### BUGGY CODE ###

n = int(input())
s = str(input())
count = 0
i = 0
j = 0
k = 0
for i in range(0,n):
    if i > j  :
        break
    for j in range(0,n):
        if j > k :
            break
        for k in range(0,n):
            if i >= j or j >= k or i >= k :
                continue
            if (s[i] != s[j] and s[i] != s[k] and s[j] != s[k] )and (j - i != k - j):
                count += 1
                #print(i,j,k)
print(count)

### FIXED CODE ###

n = int(input())
s = str(input())

R = s.count("R")
G = s.count("G")
B = s.count("B")

counter = R * G * B 
#j-i != k-j
# 1 2 3 ,1 3 5

for i in range(n-1):
    for j in range(i+1,(n+i+1)//2):
        if s[i] != s[j] and s[i] != s[2*j-i] and s[2*j -i] != s[j] :
            counter -= 1

print(counter)
### BUGGY CODE ###

S = input()
MOD = 10 ** 9 + 7

dp = [[0] * 4 for _ in range(len(S) + 1)]
dp[0][0] = 1
for i in range(len(S)):
    for j in range(4):
        if S[i] == '?':
            dp[i + 1][j] += dp[i][j] * 3
        else:
            dp[i + 1][j] += dp[i][j]
    
    if S[i] == 'A' or S[i] == '?': dp[i + 1][1] += dp[i][0]
    if S[i] == 'B' or S[i] == '?': dp[i + 1][2] += dp[i][1]
    if S[i] == 'C' or S[i] == '?': dp[i + 1][3] += dp[i][2]

print(dp[len(S)][3] % MOD)

### FIXED CODE ###

S = input()
MOD = 10 ** 9 + 7

dp = [[0] * 4 for _ in range(len(S) + 1)]
dp[0][0] = 1
for i in range(len(S)):
    for j in range(4):
        if S[i] == '?':
            dp[i + 1][j] = (dp[i + 1][j] + dp[i][j] * 3) % MOD
        else:
            dp[i + 1][j] = (dp[i + 1][j] + dp[i][j]) % MOD
    
    if S[i] == 'A' or S[i] == '?': dp[i + 1][1] = (dp[i + 1][1] + dp[i][0]) % MOD
    if S[i] == 'B' or S[i] == '?': dp[i + 1][2] = (dp[i + 1][2] + dp[i][1]) % MOD
    if S[i] == 'C' or S[i] == '?': dp[i + 1][3] = (dp[i + 1][3] + dp[i][2]) % MOD

print(dp[len(S)][3] % MOD)
### BUGGY CODE ###

'''
参考: https://beta.atcoder.jp/contests/agc026/submissions/2849460
'''

if __name__ == '__main__':
    n = int(input())
    s = input()

    s_left = s[:n]
    s_right = s[n:]
    m = dict()

    '''
    1 << n　は　2 ** n
    '''
    
    for i in range(2 ** n):
        s_red, s_blue = '', ''
        for j in range(n):
            if i & (2 ** j):
                s_red = s_red  + s_left[j]
            else:
                s_blue = s_left[j] + s_blue
        if (s_red, s_blue) in m:
            m[(s_red, s_blue)] += 1
        else:
            m[(s_red, s_blue)] = 1

    ans = 0
    for i in range(2 ** n):
        s_red, s_blue = '', ''
        for j in range(n):
            if i & (2 ** j):
                s_red = s_right[j] + s_red
            else:
                s_blue = s_blue + s_right[j]
       
        if (s_red, s_blue) in m:
            ans += m[(s_red, s_blue)]
    print(ans)

### FIXED CODE ###

def main():
    from collections import defaultdict

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

    L, R = S[:N], S[N * 2 - 1:N - 1:-1]

    def make_counter(s: str) -> defaultdict(int):
        ret = defaultdict(int)
        for bit in range(1 << N):
            red, blue = '', ''
            for j in range(N):
                if (bit >> j) & 1:
                    red += s[j]
                else:
                    blue += s[j]

            ret[red + '_' + blue] += 1
        return ret

    ld = make_counter(L)
    rd = make_counter(R)

    print(sum(ld[key] * rd[key] for key in ld))


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

N = int(input())
A = []
for _ in range(N):
  A.append(int(input())-1)
#[int(input())-1]
 
count = []
n = 0
ans = 1
for k in range(N+1):
  if A[n] == 2-1:
    break
 
  elif A[n] in count:
    ans = -1
    break
  
  else:
    count.append(A[n])
    ans += 1
    n = A[n]
 
print(ans)

### FIXED CODE ###

n = int(input())
al = []
for i in range(n):
    a = int(input())
    al.append(a)
 
al.insert(0, 0)
ans = -1
push = 1
for i in range(1, n+2):
    light = al[push]
    push = light
    if light == 2:
        ans = i
        break
print(ans)
### BUGGY CODE ###

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

sums = 0

for i in range(a):
    sums += 1
    k -= 1
    if k == 0:
        print(sums)

for i in range(b):
    sums += 0
    k -= 1
    if k == 0:
        print(sums)

for i in range(c):
    sums += -1
    k -= 1
    if k == 0:
        print(sums)


### FIXED CODE ###

import sys

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

sums = 0

if (k <= a):
    print(k)
    sys.exit()
else:
    sums += a
    k -= a

if (k <= b):
    print(sums)
    sys.exit()
else:
    k -= b

sums += -1 * k
print(sums)
### BUGGY CODE ###

x = int(input())

ans = 0

for i in range(1, 51):

 tmp = i

 while tmp * i < x + 1:

  tmp = tmp * i

  ans = max(ans, tmp)

print(ans)

### FIXED CODE ###

x = int(input())

ans = 0

if x == 1:

 print(1)

 exit()

for i in range(2, 51):

 tmp = i

 while tmp * i < x + 1:

  tmp = tmp * i

  ans = max(ans, tmp)

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

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

count = 0

x = 0
y = 0
z = 0

for targetX in range(ks[0]+1):
    
    
    if targetX <= ks[0] and ks[1]-(targetX+ks[0]+ks[0]) <= 0:
    
        for targetY in range(ks[0]+1):
            
            if targetY <= ks[0] and ks[1]-(targetX+targetY+ks[0]) <= 0:
        
                for targetZ in range(ks[0]+1):
                
                    if targetZ <= ks[0]:
                        
                        if targetX + targetY + targetZ == ks[1]:
                            count +=1
                    
                    targetZ +=1
        
            targetY +=1
        
    targetX +=1
        

print(count)


### FIXED CODE ###

ks= list(map(int, input().split())) 
 
count = 0
 
x = 0
y = 0
z = 0
 
for targetX in range(ks[0]+1):
    
    
    if targetX <= ks[0] and ks[1]-(targetX+ks[0]+ks[0]) <= 0:
    
        for targetY in range(ks[0]+1):
            
            z = ks[1] - (targetX + targetY)
            
            if 0 <= z and z <= ks[0]:
                count += 1
                
        
        
        
 
print(count)
### BUGGY CODE ###

from bisect import bisect_right

n = int(input())
order = sorted(list(map(int, input().split())))
ans = 0

for i in reversed(range(2, n)):
    for j in reversed(range(1, i)):
        ans += j - bisect_right(order[0:j], order[i]-order[j])

print(ans)

### FIXED CODE ###

from bisect import bisect_right

n = int(input())
order = sorted(list(map(int, input().split())))
ans = 0

for i in reversed(range(2, n)):
    for j in reversed(range(1, i)):
        ans += j - bisect_right(order[0:j], order[i]-order[j])

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

def sol():
    n = int(input())
    mul = 1
    arr = list(map(int, input().split()))
    for i in arr:
        mul*=i
    if(mul>10**18):
        print(-1)
    else:
        print(mul)
sol()

### FIXED CODE ###

def sol():
    n = int(input())
    mul = 1
    arr = list(map(int, input().split()))
    arr.sort()
    for i in arr:
        mul*=i
        if(mul>10**18):
            print(-1)
            return
    print(mul)
sol()
### BUGGY CODE ###

n=int(input())
INF=9999999999
d=[INF for _ in range(n)]
dist=[[INF for _ in range(n)] for _ in range(n)]
used=[False for _ in range(n)]
linked=[[] for _ in range(n)]

for i in range(n):
    dist[i][i]=0

for i in range(n-1):
    a,b,c=map(int,input().split())
    dist[a-1][b-1]=c
    dist[b-1][a-1]=c
    linked[a-1].append(b-1)
    linked[b-1].append(a-1)

def dfs(v,dv):
    d[v]=dv
    used[v]=True
    for i in linked[v]:
        if(used[i]):
            continue
        dfs(i,dv+dist[v][i])



q,k=map(int,input().split())

dfs(k-1,0)

for i in range(q):
    x,y=map(int,input().split())
    print(d[x-1]+d[y-1])




### FIXED CODE ###

import sys
sys.setrecursionlimit(10**6)
n=int(input())
INF=9999999999
d=[INF for _ in range(n)]
g=[[] for _ in range(n)]


for i in range(n-1):
    a,b,c=map(int,input().split())
    g[a-1].append([b-1,c])
    g[b-1].append([a-1,c])

def dfs(v,p,dv):
    d[v]=dv
    for gi,c in g[v]:
        if(gi==p):
            continue
        dfs(gi,v,dv+c)

q,k=map(int,input().split())

dfs(k-1,-1,0)

for i in range(q):
    x,y=map(int,input().split())
    print(d[x-1]+d[y-1])
### BUGGY CODE ###

n=int(input(''))
s=[]
for a in range(n):
    t=input('')
    if t in s:
        p=0
    else:
        s.append(t)

print(len(s))

### FIXED CODE ###

n=int(input(''))
s={}
for a in range(n):
    t=input('')
    if t in s:
        p=0
    else:
        s[t]=1

print(len(s))
### BUGGY CODE ###

# -*- coding: utf-8 -*-

from sys import stdin

import time
import numpy as np
import math


############################################

# read data for n sequences.
temp = stdin.readline().split()
N = int(temp[0])
temp = stdin.readline().split()
data = temp[0]


out = 0

if N > 2:
    for ii, i in enumerate(data[:-2]):
        for jj,j in enumerate(data[ii+1:-1]):
            jj = jj+ii+1
            if i!=j:
                for kk, k in enumerate(data[jj+1:]):
                    kk = kk+jj + 1
                    if i!=k and j!=k:
                        if (jj-ii) != (kk-jj):
                            out+=1
print(out)



### FIXED CODE ###

# -*- coding: utf-8 -*-

############################################

# read data for n sequences.
N = int(input())
data = input()

out = 0
r=0;g=0;b=0;
for d in data:
    if d =="R":
        r+=1
    elif d=="B":
        b+=1
    else:
        g+=1
out = r*g*b

if N > 2:
    for ii, i in enumerate(data[:-2]):
        for jj,j in enumerate(data[ii+1:-1]):
            jj = jj+ii+1
            if i!=j:
                sum1 = 0                    
                for d in [d for d in "RGB"]:
                    if i!=d and j!=d:
                        let = d

                if jj+jj-ii<N:
                    if data[jj+jj-ii]==let:                    
                        out-=1
print(out)
### BUGGY CODE ###

N = int(input())
M = 0
for i in range(N - 1):
    M += (i + 1) % (i + 2)
print(M)

### FIXED CODE ###

N = int(input())
print(N * (N + 1) // 2 - N)
### BUGGY CODE ###

n = int(input())
A = list(map(str, input().split()))
B = list()
cycle = n % 2
for a in A:
    if cycle % 2 == 1:
        B.insert(0, a)
    else:
        B.append(a)
    cycle += 1
print(' '.join(B))


### FIXED CODE ###

from collections import deque
n = int(input())
A = deque(map(str, input().split()))
B = deque()
cycle = n % 2
for a in A:
    if cycle % 2 == 1:
        B.appendleft(a)
    else:
        B.append(a)
    cycle += 1
print(' '.join(B))
### BUGGY CODE ###

N = int(input())
A = list(int(x) for x in input().split())
for i in range(N):
    minimum = A.index(i+1)
    print(minimum+1, end=" ")


### FIXED CODE ###

N = int(input())
A = list(int(x) for x in input().split())
d = {}

for i in range(N):
    d[i] = A[i]
d_sorted = sorted(d.items(), key=lambda x:x[1])
for key, value in d_sorted:
    print(key+1, end=" ")
### BUGGY CODE ###

N = raw_input()
ns = raw_input()
ns = ns.split(' ')

data = {}
for i in ns:
    i = int(i)
    if i in data:
        data[i] += 1
    else:
        data[i] = 1

Q = raw_input()
opr = []
for _ in range(int(Q)):
    tmp = raw_input()
    tmp = tmp.split(' ')
    opr.append([int(tmp[0]), int(tmp[1])])

ans = 0
for k, v in data.iteritems():
    ans += k * v
for op in opr:
    if op[0] not in data:
        print ans
        continue
    ans = 0
    old = data[op[0]]
    del data[op[0]]
    old2 = 0
    if op[1] in data:
        old2 = data[op[1]]
    data[op[1]] = old + old2
    for k, v in data.iteritems():
        ans += k * v
    print ans


### FIXED CODE ###


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

val = {}

suss = 0
for i in s:
    if i not in val:
        val[i] = 1
    else:
        val[i] += 1

for k, v in val.items():
    suss += k *v

while q:
    c, d = map(int, input().split())
    # print(c, d)
    if c in val:
        if d in val:
            val[d] += val[c]
        else:
            val[d] = val[c]
        suss -= c * val[c]
        suss += d * val[c]
        del val[c]
    # print(val)


    print(suss)

    q -= 1