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

K = int(input())
count = 1
rem = 7 % K

if K % 2 == 0:
    print(-1)
elif rem % K == 0:
    print(1)
else:
    while (rem % K) != 0:
        count += 1
        rem += 7 * pow(10, count-1, mod=K) % K
    print(count)


### FIXED CODE ###

K = int(input())
count = 1
rem = 7 % K

if K % 2 == 0:
    print(-1)
elif rem % K == 0:
    print(1)
else:
    while (rem % K) != 0 and count <= K:
        count += 1
        rem += 7 * pow(10, count-1, mod=K) % K
    if count <= K:
        print(count)
    else:
        print(-1)
### BUGGY CODE ###

import numpy as np
N = int(input())

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

P = [0] + P
     
count = 0
for i in range(1, N+1):
    #print(P[i])
    #print(P[1:i+1])
    a = np.array([P[i]] * i)
    b = np.array(P[1:i+1])
    #print( a > b )
    #print(any(a > b))
    
    if not any(a > b):
        count += 1

print(count)

### FIXED CODE ###

N = int(input())

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

P = [0] + P
     
count = 0
min = 10000000000000
for i in range(1, N+1):
    if min > P[i]:
        min = P[i]
    elif min < P[i]:
        continue
    """
    for j in range(1, i+1):
        #print(i, P[i], j , P[j])
        if P[i] > P[j]:
            #print("out", i)
            break
    else:
        #print("ok", i)
        count += 1
    """
    count += 1
        
    if P[i] == 1:
        break

print(count)
### BUGGY CODE ###


N, A,B = map(int, input().split())
str = []
for j in range(N//(A+B)+1):
    for i in range(A):
        str.append('b')
    for i in range(B):
        str.append('r')

count = 0
for k in range(N):
    if str[k] == 'b':
        count+=1
print(count)



### FIXED CODE ###

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

count = N//(A+B)
if (N-count*(A+B))>A:
    print(count*A+A)
else:
    print(count*A+(N-count*(A+B)))
### BUGGY CODE ###

import sys
N, M = (int(i) for i in input().split())
X = [int(i) for i in input().split()]
X.sort()

pos = [[0] * 2 for i in range(N)]
total = 0

if M <= N:
    print(0)
    sys.exit()

for i in range(N):
    pos[i][0] = i
    pos[i][1] = abs(X[i+1] - X[i])
    total = total + pos[i][1]

pos = sorted(pos, key=lambda x:(x[1]))

for j in range(N, M-1):
    dim = abs(X[j+1]-X[j])
    total = total + dim
    if pos[0][1] < dim:
        pos[0][0] = j
        pos[0][1] = dim
        pos = sorted(pos, key=lambda x:(x[1]))



pos = sorted(pos, key=lambda x:(x[1]), reverse = True)
for i in range(N-1):
    total = total - pos[i][1]

print(total)


### FIXED CODE ###

import sys
N, M = (int(i) for i in input().split())
X = [int(i) for i in input().split()]
X.sort()

L = []

for i in range(1, M):
    L.append(X[i]-X[i-1])

L = sorted(L, reverse=True)

print(X[M-1]-X[0]-sum(L[:N-1]))
### BUGGY CODE ###

import sys
from bisect import bisect
def input():
	return sys.stdin.buffer.readline()[:-1]

class BIT():#1-indexed
	def __init__(self, size):
		self.table = [0 for _ in range(size+2)]
		self.size = size

	def Get(self, i):#iを返す
		s = 0
		while i > 0:
			s += self.table[i]
			i -= (i & -i)
		return s

	def PointAdd(self, i, x):#
		while i <= self.size:
			self.table[i] += x
			i += (i & -i)
		return

	def SegAdd(self, l, r, x):#lからrにxを足す
		self.PointAdd(l, x)
		self.PointAdd(r+1, -x)
		return


n, d, a = map(int, input().split())
d *= 2
mo = sorted([list(map(int, input().split())) for _ in range(n)])
for i in range(n):
	mo[i][1] = (mo[i][1]-1)//a + 1
reach = []
moo = [x[0] for x in mo]
mooo = [x[1] for x in mo]
for x in moo:
	reach.append(bisect(moo, x+d)-1)
#print(reach)

bit = BIT(n)

ans = 0
for i, h in enumerate(mooo):
	gap = max(0, h - bit.Get(i+1))
	bit.SegAdd(i+1, reach[i]+1, gap)
	ans += gap

print(ans)

### FIXED CODE ###

import sys
from bisect import bisect
def input():
	return sys.stdin.buffer.readline()[:-1]

class BIT():#1-indexed
	def __init__(self, size):
		self.table = [0 for _ in range(size+2)]
		self.size = size

	def Get(self, i):#iを返す
		s = 0
		while i > 0:
			s += self.table[i]
			i -= (i & -i)
		return s

	def PointAdd(self, i, x):#
		while i <= self.size:
			self.table[i] += x
			i += (i & -i)
		return

	def SegAdd(self, l, r, x):#lからrにxを足す
		self.PointAdd(l, x)
		self.PointAdd(r+1, -x)
		return


n, d, a = map(int, input().split())
d *= 2
mo = sorted([list(map(int, input().split())) for _ in range(n)])
for i in range(n):
	mo[i][1] = (mo[i][1]-1)//a + 1
reach = []
moo = [x[0] for x in mo]
mooo = [x[1] for x in mo]
for x in moo:
	reach.append(bisect(moo, x+d)-1)
#print(reach)

bit = BIT(n)

ans = 0
for i, h in enumerate(mooo):
	gap = max(0, h - bit.Get(i+1))
	bit.SegAdd(i+1, reach[i]+1, gap)
	ans += gap

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

# coding: UTF-8
n = int(raw_input())
count = 0
for i in range(n):
    break_f = False
    j = 3
    num = int(raw_input())
    if (num < 2):
        continue
    elif (num == 2):
        count += 1
        continue
    elif (num % 2 == 0):
        continue
    while j < num:
        if (num % j == 0):
            j += 2
            break_f = True
            break
        j += 2
    if break_f == True:
        continue
    count += 1
print(count)



### FIXED CODE ###

# coding: UTF-8
n = int(raw_input())
count = 0
for i in range(n):
    num = int(raw_input())
    if (num < 2):
        continue
    elif (num == 2):
        count += 1
        continue
    elif (num % 2 == 0):
        continue
    if pow(2,num-1,num) == 1:
        count += 1
print(count)
### BUGGY CODE ###

import numpy as np
n = int(input())
A = [int(i) * 2 for i in input().split()]

L1 = [[0 for _ in range(n)] for _ in range(n)]
for i in range(n):
    if i + 1 <= n-1:
        L1[i][i] = 1
        L1[i][i+1] = 1
    else:
        L1[i][i] = 1
        L1[i][0] = 1
        
L1 = np.linalg.inv(L1) 
L2 = np.array(A)
ans = list(np.dot(L1, L2))
print(" ".join([str(int(n)) for n in ans]))

### FIXED CODE ###

n = int(input())
A = [int(i) for i in input().split()]

x1 = sum(A)
for i in range(n):
    if i % 2 != 0:
        x1 -= 2 * A[i]
ans = [x1]
for i in range(n-1):
    tmp = 2 * A[i] - ans[i]
    ans.append(tmp)

print(" ".join([str(int(n)) for n in ans]))
### BUGGY CODE ###

import itertools as it

N = int(input())
A = [0] + list(map(int, input().split()))
A = A + [0]

L = [0]
for i in range(N):
    L = L + [L[-1] + abs(A[i+1] - A[i])]

L = L + [L[-1] + abs(A[-2] - A[0])]

for i in range(N):
    print(L[i] + (L[-1] - L[i+2]) + abs(A[i] - A[i+2]))


### FIXED CODE ###

import itertools as it

N = int(input())
A = [0] + list(map(int, input().split())) + [0]

L = 0
for i in range(N+1):
    L += abs(A[i+1] - A[i])
    
for i in range(1,N+1):
    print(L - (abs(A[i] - A[i-1]) + abs(A[i+1] - A[i])) + abs(A[i+1] - A[i-1]))
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10000)
n, x, y = list(map(int, input().split()))
x -= 1
y -= 1

dist = []
for i in range(n):
    dist.append([110110] * n)

def dfs(now, s):
    if now != 0:
        if dist[s][now] + 1 < dist[s][now-1]:
            dist[s][now-1] = dist[s][now] + 1
            dfs(now-1, s)
    if now != n-1:
        if dist[s][now] + 1 < dist[s][now+1]:
            dist[s][now+1] = dist[s][now] + 1
            dfs(now+1, s)
    if now == x:
        if dist[s][now] + 1 < dist[s][y]:
            dist[s][y] = dist[s][now] + 1
            dfs(y, s)
    if now == y:
        if dist[s][now] + 1 < dist[s][x]:
            dist[s][x] = dist[s][now] + 1
            dfs(x, s)

cnt = [0] * n

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

for i in range(n):
    for j in range(n):
        cnt[dist[i][j]] += 1

for i in range(1, n):
    print(cnt[i] // 2)


### FIXED CODE ###

n, x, y = list(map(int, input().split()))
x -= 1
y -= 1

cnt = [0] * n

dist = []
for i in range(n):
    dist.append([-1] * n)
for i in range(n):
    dist[i][x] = abs(i-x)
    dist[i][y] = abs(i-y)
    dist[x][i] = abs(i-x)
    dist[y][i] = abs(i-y)

for i in range(n):
    for j in range(n):
        if i >= j:
            continue
        dis = min(j - i, dist[j][y] + 1 + dist[i][x], dist[j][x] + 1 + dist[i][y])
        cnt[dis] += 1
for i in range(1, n):
    print(cnt[i])
### BUGGY CODE ###

n = int(input())
a = [int(i) for i in input().split()]
ans = 1
for i in a:
  ans *= i
print(ans if ans <= 10**18 else -1)

### FIXED CODE ###

n = int(input())
a = [int(i) for i in input().split()]
ans = 1 if 0 not in a else 0
for i in a:
  ans *= i
  if ans > 10**18:
    ans = -1
    break
print(ans)
### BUGGY CODE ###

inf = float('inf')
N = int(input())
A = list(map(int, input().split()))
dp0=[[-inf]*(2+N//2) for _ in range(N+1)]
dp1=[[-inf]*(2+N//2) for _ in range(N+1)]
dp0[0][0] = 0

for i, x in enumerate(A):
    j = i//2
    for n in (j, j + 1):
        dp0[i+1][n] = max(dp0[i][n], dp1[i][n])
        dp1[i+1][n] = dp0[i][n - 1] + x
    #print(dp0)
    #print(dp1)

print(max(dp0[N][N//2], dp1[N][N//2]))


### FIXED CODE ###

from collections import defaultdict
n=int(input())
a=list(map(int,input().split()))
dp=[defaultdict(lambda:-float('inf'))for _ in range(n)]
for i in range(n):dp[i][0]=0
for i,a in enumerate(a):
    for j in range(n//2-(n-i)//2,i//2+2):
        dp[i][j]=max(dp[i-2][j],dp[i-2][j-1]+a,dp[i-1][j])
print(dp[-1][n//2])
### BUGGY CODE ###

N=int(input())
for i in range(int(N**0.5),N+1):
    if N%i==0:
        print(len(str(i)))
        exit(0)

### FIXED CODE ###

N=int(input())
for i in range(int(N ** 0.5 + 1),-1,-1):
    if N % i == 0:
        print(len(str(N // i)))
        exit(0)
### BUGGY CODE ###

import sys
from operator import itemgetter
inputs = sys.stdin.readline
N, Q= map(int,inputs().split())
def main():
	E = [None]*2*N
	for n in range(N):
		a,b,c = map(int,inputs().split())
		E[2*n] = (a - c, 1, c)
		E[2*n + 1] = (b - c, -1, c)
	E.sort(key=itemgetter(0))
	tuple(tuple(E))
	mini = 1000000001
	kouho = {1000000001}
	my_add = kouho.add
	my_discard = kouho.discard
	q = 0
	d = int(inputs())
	flag = False
	cont = False
	for e in E:
		while(e[0] > d):
			if cont == True:
				if mini < 1000000001:
					print(mini)
				else:
					print(-1)
			else:
				mini = min(kouho)
				if mini < 1000000001:
					print(mini)
				else:
					print(-1)
			cont = True
			if q < Q-1:
				q = q + 1
				d = int(inputs())
			else:
				flag = True
				q = q + 1
				break
		if flag == True:
			break
		if e[1] == 1:
			my_add(e[2])
			if mini > e[2]:
				mini = e[2]
		elif e[1] == -1:
			my_discard(e[2])
			cont = False
	for t in [None]*(Q-q):
		print(-1)
		
def sub():
	V = [list(map(int,inputs().split())) for p in [0]*N]
	mini = 1000000001
	count = 0
	for q in [None]*Q:
		m = int(inputs())
		num = mini
		for v in E:
			if v[0]-v[2]<=m and m <= v[1]-v[2]-1:
				num = min(v[2],num)
		if num == mini:
			print(-1)
		else:
			print(num)

if N > 10*Q:
	sub()
else:
	main()

### FIXED CODE ###

import sys
from operator import itemgetter
def main():
	inputs = sys.stdin.readline
	N, Q= map(int,inputs().split())
	E = [None]*2*N
	for n in range(N):
		a,b,c = map(int,inputs().split())
		E[2*n] = (a - c, 1, c)
		E[2*n + 1] = (b - c, -1, c)
	E.sort(key=itemgetter(0))
	tuple(tuple(E))
	mini = 1000000001
	kouho = {1000000001}
	my_add = kouho.add
	my_discard = kouho.discard
	q = 0
	d = int(inputs())
	flag = False
	cont = False
	for e in E:
		while e[0] > d:
			if cont == True:
				print(mini-(mini//1000000001)*1000000002)
			else:
				mini = min(kouho)
				print(mini-(mini//1000000001)*1000000002)
			cont = True
			q = q + 1
			if q > Q-1:
				flag = True
				break
			d = int(inputs())
		if flag == True:
			break
		if e[1] == 1:
			my_add(e[2])
			if mini > e[2]:
				mini = e[2]
		elif e[1] == -1:
			my_discard(e[2])
			if mini == e[2]:
				cont = False
	for t in [None]*(Q-q):
		print(-1)
main()
### BUGGY CODE ###

S = input()

count = 0
mod_check = {}
mod_check[0] = 1
check_S = 0
for i in reversed(S):
    check_S += int(i) * (10 ** count)
    mod = check_S % 2019
    mod_check.setdefault(mod, 0)
    mod_check[mod] += 1
    count += 1
mod_match = 0
for x in mod_check.values():
    mod_match += x * (x - 1) // 2
print(mod_match)


### FIXED CODE ###

S = input()

count = 0
mod_check = [0] * 2019
mod_check[0] = 1
check_S = 0
d = 1
for i in reversed(S):
    check_S += int(i) * d
    check_S %= 2019
    mod_check[check_S] += 1
    d *= 10
    d %= 2019
    count += 1
mod_match = 0
for x in mod_check:
    mod_match += x * (x - 1) // 2
print(mod_match)
### BUGGY CODE ###

n = int(input())
ans  = [i for i in range(1,n) if n//i == n%i]
print(sum(ans))


### FIXED CODE ###

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

n = int(input())
div  = make_divisors(n)

l = []
for i in div:
    var = i - 1
    if var != 0 :
        if n//var == n%var:
            l.append(var)

print(sum(l))
### BUGGY CODE ###

import sys
input = sys.stdin.readline

n, m = [int(x) for x in input().split()]

islands = [set() for _ in range(n + 1)]

for _ in range(m):
    ab = [int(x) for x in input().split()]
    islands[min(ab)].add(max(ab))

ans = 0

for i in range(1, n):
    if i + 1 in islands[i]:
        ans += 1
    else:
        islands[i + 1] = islands[i] | islands[i + 1]

ans += len(islands[n])

print(ans)


### FIXED CODE ###

import sys
input = sys.stdin.readline

n, m = [int(x) for x in input().split()]

islands = [set() for _ in range(n + 1)]

for _ in range(m):
    ab = [int(x) for x in input().split()]
    islands[min(ab)].add(max(ab))

ans = 0

for i in range(1, n):
    if i + 1 in islands[i]:
        ans += 1
    elif len(islands[i]) > 0:
        islands[i + 1].add(min(islands[i]))

ans += len(islands[n])

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

n = int(input())
 
def bellman_ford(s):
	d = [float('inf')]*n # 各頂点への最小コスト
	d[s] = 0 # 自身への距離は0
	for i in range(n):
		update = False # 更新が行われたか
		for x, y, z in g:
			if d[y] > d[x] + z:
				d[y] = d[x] + z
				update = True
		if not update:
 			break
		# 負閉路が存在
		if i == n-1:
			return -1
	return d

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

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

### FIXED CODE ###

import heapq

def dijkstra(s):
	hq = [(0, s)]
	heapq.heapify(hq) # リストを優先度付きキューに変換
	cost = [float('inf')] * n # 行ったことのないところはinf
	cost[s] = 0 # 開始地点は0
	while hq:
		c, v = heapq.heappop(hq)
		if c > cost[v]: # コストが現在のコストよりも高ければスルー
			continue
		for d, u in e[v]:
			tmp = d + cost[v]
			if tmp < cost[u]:
				cost[u] = tmp
				heapq.heappush(hq, (tmp, u))
	return cost

n = int(input())
e = [[] for _ in range(n)]
for _ in range(n-1):
	a,b,c = map(int,input().split())
	a,b = a-1, b-1
	e[a].append((c, b))
	e[b].append((c, a))
q,k = map(int,input().split())
l = dijkstra(k-1)
for _ in range(q):
	x,y = map(int,input().split())
	print(l[x-1] + l[y-1])
### BUGGY CODE ###

import sys
from collections import deque, defaultdict
import copy
import bisect
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
 
 
N, Q = list(map(int, input().split()))
STX=[]
D=[]
for i in range(N):
    STX.append(list(map(int, input().split())))
for i in range(Q):
    D.append(int(input()))
 
dist = [10000000000000 for i in range(Q)]
 
for i in range(N):
    s = STX[i][0] - STX[i][2]
    t = STX[i][1] - STX[i][2] - 1
    a = bisect.bisect_left(D, s)
    b = bisect.bisect_right(D, t)
    for j in range(a, b):
        dist[j] = min(dist[j], STX[i][2])
 
 
for i in range(Q):
    if dist[i] > 100000000000:
        print(-1)
    else:
        print(dist[i])

### FIXED CODE ###

import sys
from collections import deque, defaultdict
import copy
import bisect
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 9)


N, Q = list(map(int, input().split()))
STX=[]
D=[]
for i in range(N):
    S, T, X = list(map(int, input().split()))
    STX.append((X, S, T))
STX = sorted(STX, key=lambda x: x[0])

for i in range(Q):
    D.append(int(input()))

skip = [-1]*Q

dist = [-1]*Q

for X, S, T in STX:
    s = S - X
    t = T - X - 1
    a = bisect.bisect_left(D, s)
    b = bisect.bisect_right(D, t)
    #print(D)
    j = a
    while j < b:
        if skip[j] == -1:
            skip[j] = b
            dist[j] = X
            j+=1
        else:
            j=skip[j]


for i in range(Q):
    print(dist[i])
### BUGGY CODE ###

import numpy as np
N, X, Y = map(int,input().split())

NN = np.array([np.zeros(N,dtype='bool')]*N)
count = 0
#initialize
for i in range(N-1):
  NN[i][i+1] = True
NN[X-1][Y-1] = True
print(N)
for _ in range(N-2):
  NN2 = np.array(NN)
  for i in range(N):
    for j in range(i+1,N):
      if NN[i][j] == True:
        #top
        if i !=0:
          if (not NN[i-1][j]) and (i-1<j):
            NN2[i-1][j] = True
        #bottom
        if i != N-1:
          if (not NN[i+1][j]) and (i+1<j):
            NN2[i+1][j] = True
            count += 1
        #left
        if j != 0:
          if (not NN[i][j-1]) and (i<j-1):
            NN2[i][j-1] = True
        #right
        if j != N-1:
          if (not NN[i][j+1]) and (i<j+1):
            NN2[i][j+1] = True
  count = np.sum(NN2)-np.sum(NN)
  if count==0:
    NN = NN2
    print(0)
  else:
    NN = NN2
    print(count)
          


### FIXED CODE ###

N, X, Y = map(int,input().split())
X = X-1
Y = Y-1
CNT = [0]*(N)
for i in range(N):
  for j in range(i+1,N):
    nn1 = j-i
    nn2 = abs(i-X)+abs(j-Y)+1
    if nn1>nn2:
      CNT[nn2] += 1
    else:
      CNT[nn1] += 1
for i in range(1,N):
  print(CNT[i])
### BUGGY CODE ###

R, C, K = map(int, input().split())
rcvs = [tuple(map(int, input().split())) for _ in range(K)]

rcvs.sort(key = lambda t: t[1] * 30000 + t[0])

dp = [[0] * (R+1) for _ in range(4)]
index = 0
for c in range(C+1):
    new_dp = [row.copy() for row in dp]
    while index < K and rcvs[index][1] == c:
        x, _, v = rcvs[index]
        if new_dp[3][x] < dp[2][x] + v:
            new_dp[3][x] = dp[2][x] + v
        if new_dp[2][x] < dp[1][x] + v:
            new_dp[2][x] = dp[1][x] + v
        if new_dp[1][x] < dp[0][x] + v:
            new_dp[1][x] = dp[0][x] + v
        index += 1
        for r in range(R):
            new_dp[0][r+1] = max(new_dp[0][r], new_dp[1][r], new_dp[2][r], new_dp[3][r])
        dp = new_dp
        #print(dp)   
    
print(max(max(row) for row in dp))

### FIXED CODE ###

R, C, K = map(int, input().split())
points_dict = dict()
for _ in range(K):
    r, c, v = map(int, input().split())
    points_dict[r * 30000 + c] = v

dp = [[[0] * (C+1) for _ in range(R+1)] for _ in range(4)] # dp[4][R+1][C+1]
index = 0
for i in range(1, R+1):
    for j in range(1, C+1):
        #移動
        for k in range(4):
            if dp[0][i][j] < dp[k][i-1][j]:
                dp[0][i][j] = dp[k][i-1][j]
            if dp[k][i][j] < dp[k][i][j-1]:
                dp[k][i][j] = dp[k][i][j-1]
        #あれば、拾うかどうか
        if i * 30000 + j in points_dict:
            v = points_dict[i * 30000 + j]
            for k in range(3, 0, -1):
                if dp[k][i][j] < dp[k-1][i][j] + v:
                    dp[k][i][j] = dp[k-1][i][j] + v
"""
for i in range(4):
    print(i)
    print(*dp[i], sep = '\n')
"""
print(max((dp[i][R][C] for i in range(4))))
### BUGGY CODE ###

N = int(input())
S = [''.join(sorted(input())) for _ in range(0, N)]
d = {}

for s in S:
    if s[0] in d.keys():
        d[s[0]].append(s)
    else:
        d[s[0]] = [s]

result = 0

for k in d.values():
    if len(k) > 1:
        for i, s in enumerate(k):
            for j in range(i + 1, len(k)):
                if s == k[j]:
                    result += 1

print(result)

### FIXED CODE ###

N = int(input())
S = [''.join(sorted(input())) for _ in range(0, N)]
d = {}
result = 0
for s in S:
    if s in d.keys():
        result += d[s]
        d[s] += 1
    else:
        d[s] = 1
print(result)
### BUGGY CODE ###

q = int(input())
for _ in range(q):
    a, b = sorted(map(int, input().split()))
    if a == b:
        print(2 * a - 2)
    elif a + 1 == b:
        print(2 * a - 2)
    else:
        c = a
        while True:
            if (c + 1) * (c + 1) >= a * b:
                break
            c += 1

        if c * (c + 1) >= a * b:
            print(2 * c - 2)
        else:
            print(2 * c - 1)

### FIXED CODE ###

import math

q = int(input())
for _ in range(q):
    a, b = sorted(map(int, input().split()))
    if a == b:
        print(2 * a - 2)
    elif a + 1 == b:
        print(2 * a - 2)
    else:
        c = math.ceil(math.sqrt(a * b)) - 1

        if c * (c + 1) >= a * b:
            print(2 * c - 2)
        else:
            print(2 * c - 1)
### BUGGY CODE ###

a=[]
n=int(input())
while(n>0):
    b=int(input())
    a.append(b)
    n-=1

a.reverse()
sa=a[0]-a[1]
for i,e in enumerate(a):
    for j in a[i+1:]:
        sb=e-j
        if(sb>sa):
            sa=sb

print(sa)

### FIXED CODE ###

a=[]
n=int(input())
i=0
while(n>i):
    b=int(input())
    a.append(b)
    i+=1
score=-1000000000
mini = a[0]
for i in a[1:]:
    if(i-mini>score):
        score = i-mini
    if(mini>i):
        mini=i
    

print(score)
### BUGGY CODE ###

#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin


def main(init=int(2**31-1)):
    n, q = (int(s) for s in stdin.readline().split())

    a = [init] * n
    px = py = init

    for _ in range(q):
        com, x, y = (int(s) for s in stdin.readline().split())
        if com:
            if x != px or py != y:
                m = min(a[x:y+1])
                px = x
                py = y
            print(m)
        else:
            a[x] = y
            px = py = init


main()

### FIXED CODE ###

#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin


def main(init=int(2**31-1), readline=stdin.readline):
    n, q = (int(s) for s in readline().split())

    a = [init] * n
    b = [init] * ((n // 100) + 1)

    for _ in range(q):
        com, x, y = (int(s) for s in readline().split())
        if com:
            bx = x - (x % 100) + 100
            by = y - (y % 100)
            if bx < by:
                m = min(min(a[x:bx]), min(b[bx//100:by//100]),
                        min(a[by:y+1]))
            else:
                m = min(a[x:y+1])
            print(m)
        else:
            a[x] = y
            bx = x - (x % 100)
            b[x//100] = min(a[bx:bx+100])


main()
### BUGGY CODE ###

from functools import reduce
import fractions
import math
k=int(input())
m=0
for i in range(1,k+1):
    for j in range(1,k+1):
        for p in range(1,k+1):
            l=[i,j,p]
            m+=reduce(fractions.gcd,l)
print(m)

### FIXED CODE ###

from functools import reduce
import fractions
import math
k=int(input())
m=0
for i in range(1,k+1):
    for j in range(i,k+1):
        for p in range(j,k+1):
            l=[i,j,p]
            if i==j==p:
                m+=reduce(math.gcd,l)
            elif (i==j and i!=p) or (i==p and i!=j) or (j==p and i!=p):
                m+=3*reduce(math.gcd,l)
            else:
                m+=6*reduce(math.gcd,l)
print(m)
### BUGGY CODE ###

S = input()
nQ = int(input())

Ques = []
for i in range(nQ):
  Ques.append(input())
  
for iQ in Ques:
  iQ = iQ.split()
  if int(iQ[0]) == 1:
    S = S[::-1]
  else:
    if int(iQ[1]) == 1:
      S = iQ[2] + S
    elif int(iQ[1]) == 2:
      S = S + iQ[2]
print(S)

### FIXED CODE ###

S = input()
nQ = int(input())

Ques = []
nReverse = 0
nAddFront= 0
nAddBack = 0
for i in range(nQ):
  line = input().split()
  if int(line[0]) == 1:
    nReverse += 1
    Ques.append([int(line[0])])
  else:
    if int(line[1]) == 1:
      if nReverse % 2 == 0:
        nAddFront += 1
      else:
        nAddBack  += 1
    else:
      if nReverse % 2 == 0:
        nAddBack += 1
      else:
        nAddFront  += 1      
    Ques.append([int(line[0]),int(line[1]),line[2]])

#print(nAddFront,nAddBack)
SS = [-1] * (nAddFront + len(S) + nAddBack)
for i in range(len(S)):
   SS[nAddFront + i] = S[i]

left = nAddFront -1
right= nAddFront + len(S)

nReverseLoc = 0
for iQ in Ques:
   if iQ[0] == 1:
      nReverseLoc += 1
   if iQ[0] == 2:
    if int(iQ[1]) == 1:
      if nReverseLoc % 2 == 0:
        #nAddFront += 1
        SS[left] = iQ[2]
        left -= 1
      else:
        SS[right] = iQ[2]
        right += 1
    else:
      if nReverseLoc % 2 == 0:
        SS[right] = iQ[2]
        right += 1
      else:
        SS[left] = iQ[2]
        left  -= 1 

S = "".join(SS)
if nReverse % 2 == 1:
  S = S[::-1]
  
print(S)