#!/usr/bin/env python3.8 # %% import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines from functools import lru_cache # %% def is_kadomatsu(a, b, c): if a == c: return False return (a < b > c) or (a > b < c) # %% @lru_cache(None) def count_kadomatsu(N, x): """counting kadomatsu number K such that: 1 <= K <= N and K = x mod 100""" if N < 100: return 0 ret = 0 q, r = divmod(x, 10) for i in range(10): if 100 * i + x <= N and is_kadomatsu(i, q, r): if i: ret += 1 ret += count_kadomatsu((N - r) // 10, 10 * i + q) return ret def count_kadomatsu_all(N): return sum(count_kadomatsu(N, x) for x in range(100)) # %% def solve(K): left = 0 right = K while count_kadomatsu_all(right) < K: right *= 10 while left + 1 < right: mid = (left + right) // 2 if count_kadomatsu_all(mid) >= K: right = mid else: left = mid return right # %% T, *K = map(int, read().split()) print('\n'.join(map(str, map(solve, K))))