The only difference between easy and hard versions is the maximum value of n.

You are given a positive integer number n. You really love good numbers so you want to find the smallest good number greater than or equal to n.

The positive integer is called good if it can be represented as a sum of distinct powers of 3 (i.e. no duplicates of powers of 3 are allowed).

For example:

  * 30 is a good number: 30 = 3^3 + 3^1, 
  * 1 is a good number: 1 = 3^0, 
  * 12 is a good number: 12 = 3^2 + 3^1, 
  * but 2 is not a good number: you can't represent it as a sum of distinct powers of 3 (2 = 3^0 + 3^0), 
  * 19 is not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representations 19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0 are invalid), 
  * 20 is also not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representation 20 = 3^2 + 3^2 + 3^0 + 3^0 is invalid). 



Note, that there exist other representations of 19 and 20 as sums of powers of 3 but none of them consists of distinct powers of 3.

For the given positive integer n find such smallest m (n ≤ m) that m is a good number.

You have to answer q independent queries.

Input

The first line of the input contains one integer q (1 ≤ q ≤ 500) — the number of queries. Then q queries follow.

The only line of the query contains one integer n (1 ≤ n ≤ 10^4).

Output

For each query, print such smallest integer m (where n ≤ m) that m is a good number.

Example

Input


7
1
2
6
13
14
3620
10000


Output


1
3
9
13
27
6561
19683
T = [0, 1, 3, 4, 9, 10, 12, 13, 27, 28, 30, 31, 36, 37, 39, 40, 81, 82, 84, 85, 90, 91, 93, 94, 108, 109, 111, 112, 117, 118, 120, 121, 243, 244, 246, 247, 252, 253, 255, 256, 270, 271, 273, 274, 279, 280, 282, 283, 324, 325, 327, 328, 333, 334, 336, 337, 351, 352, 354, 355, 360, 361, 363, 364, 729, 730, 732, 733, 738, 739, 741, 742, 756, 757, 759, 760, 765, 766, 768, 769, 810, 811, 813, 814, 819, 820, 822, 823, 837, 838, 840, 841, 846, 847, 849, 850, 972, 973, 975, 976, 981, 982, 984, 985, 999, 1000, 1002, 1003, 1008, 1009, 1011, 1012, 1053, 1054, 1056, 1057, 1062, 1063,
1065, 1066, 1080, 1081, 1083, 1084, 1089, 1090, 1092, 1093, 2187, 2188, 2190, 2191, 2196, 2197, 2199, 2200, 2214, 2215, 2217, 2218, 2223, 2224, 2226, 2227, 2268, 2269, 2271, 2272, 2277, 2278, 2280, 2281, 2295, 2296, 2298, 2299, 2304, 2305, 2307, 2308, 2430, 2431, 2433, 2434, 2439, 2440, 2442, 2443, 2457, 2458, 2460, 2461, 2466, 2467, 2469, 2470, 2511, 2512, 2514, 2515, 2520, 2521, 2523, 2524, 2538, 2539, 2541, 2542, 2547, 2548, 2550, 2551, 2916, 2917, 2919, 2920, 2925, 2926, 2928, 2929, 2943, 2944, 2946, 2947, 2952, 2953, 2955, 2956, 2997, 2998, 3000, 3001, 3006, 3007, 3009, 3010, 3024, 3025, 3027, 3028, 3033, 3034, 3036, 3037, 3159, 3160, 3162, 3163, 3168, 3169, 3171, 3172, 3186,
3187, 3189, 3190, 3195, 3196, 3198, 3199, 3240, 3241, 3243, 3244, 3249, 3250, 3252, 3253, 3267, 3268, 3270, 3271, 3276, 3277, 3279, 3280, 6561, 6562, 6564, 6565, 6570, 6571, 6573, 6574, 6588, 6589, 6591, 6592, 6597, 6598, 6600, 6601, 6642, 6643, 6645, 6646, 6651, 6652, 6654, 6655, 6669, 6670, 6672, 6673, 6678, 6679, 6681, 6682, 6804, 6805, 6807, 6808, 6813, 6814, 6816, 6817, 6831, 6832, 6834, 6835, 6840, 6841, 6843, 6844, 6885, 6886, 6888, 6889, 6894, 6895, 6897, 6898, 6912, 6913, 6915, 6916, 6921, 6922, 6924, 6925, 7290, 7291, 7293, 7294, 7299, 7300, 7302, 7303, 7317, 7318, 7320, 7321, 7326, 7327, 7329, 7330, 7371, 7372, 7374, 7375, 7380, 7381, 7383, 7384, 7398, 7399, 7401, 7402,
7407, 7408, 7410, 7411, 7533, 7534, 7536, 7537, 7542, 7543, 7545, 7546, 7560, 7561, 7563, 7564, 7569, 7570, 7572, 7573, 7614, 7615, 7617, 7618, 7623, 7624, 7626, 7627, 7641, 7642, 7644, 7645, 7650, 7651, 7653, 7654, 8748, 8749, 8751, 8752, 8757, 8758, 8760, 8761, 8775, 8776, 8778, 8779, 8784, 8785, 8787, 8788, 8829, 8830, 8832, 8833, 8838, 8839, 8841, 8842, 8856, 8857, 8859, 8860, 8865, 8866, 8868, 8869, 8991, 8992, 8994, 8995, 9000, 9001, 9003, 9004, 9018, 9019, 9021, 9022, 9027, 9028, 9030, 9031, 9072, 9073, 9075, 9076, 9081, 9082, 9084, 9085, 9099, 9100, 9102, 9103, 9108, 9109, 9111, 9112, 9477, 9478, 9480, 9481, 9486, 9487, 9489, 9490, 9504, 9505, 9507, 9508, 9513, 9514, 9516,
9517, 9558, 9559, 9561, 9562, 9567, 9568, 9570, 9571, 9585, 9586, 9588, 9589, 9594, 9595, 9597, 9598, 9720, 9721, 9723, 9724, 9729, 9730, 9732, 9733, 9747, 9748, 9750, 9751, 9756, 9757, 9759, 9760, 9801, 9802, 9804, 9805, 9810, 9811, 9813, 9814, 9828, 9829, 9831, 9832, 9837, 9838, 9840, 9841, 19683, 19684, 19686, 19687, 19692, 19693, 19695, 19696, 19710, 19711, 19713, 19714, 19719, 19720, 19722, 19723, 19764, 19765, 19767, 19768, 19773, 19774, 19776, 19777, 19791, 19792, 19794, 19795, 19800, 19801, 19803, 19804, 19926, 19927, 19929, 19930, 19935, 19936, 19938, 19939, 19953, 19954, 19956, 19957, 19962, 19963, 19965, 19966, 20007, 20008, 20010, 20011, 20016, 20017, 20019, 20020, 20034, 20035, 20037, 20038, 20043, 20044, 20046, 20047, 20412, 20413, 20415, 20416, 20421, 20422, 20424, 20425, 20439,
20440, 20442, 20443, 20448, 20449, 20451, 20452, 20493, 20494, 20496, 20497, 20502, 20503, 20505, 20506, 20520, 20521, 20523, 20524, 20529, 20530, 20532, 20533, 20655, 20656, 20658, 20659, 20664, 20665, 20667, 20668, 20682, 20683, 20685, 20686, 20691, 20692, 20694, 20695, 20736, 20737, 20739, 20740, 20745, 20746, 20748, 20749, 20763, 20764, 20766, 20767, 20772, 20773, 20775, 20776, 21870, 21871, 21873, 21874, 21879, 21880, 21882, 21883, 21897, 21898, 21900, 21901, 21906, 21907, 21909, 21910, 21951, 21952, 21954, 21955, 21960, 21961, 21963, 21964, 21978, 21979, 21981, 21982, 21987, 21988, 21990, 21991, 22113, 22114, 22116, 22117, 22122, 22123, 22125, 22126, 22140, 22141, 22143, 22144, 22149, 22150, 22152, 22153, 22194, 22195, 22197, 22198, 22203, 22204, 22206, 22207, 22221, 22222, 22224, 22225,
22230, 22231, 22233, 22234, 22599, 22600, 22602, 22603, 22608, 22609, 22611, 22612, 22626, 22627, 22629, 22630, 22635, 22636, 22638, 22639, 22680, 22681, 22683, 22684, 22689, 22690, 22692, 22693, 22707, 22708, 22710, 22711, 22716, 22717, 22719, 22720, 22842, 22843, 22845, 22846, 22851, 22852, 22854, 22855, 22869, 22870, 22872, 22873, 22878, 22879, 22881, 22882, 22923, 22924, 22926, 22927, 22932, 22933, 22935, 22936, 22950, 22951, 22953, 22954, 22959, 22960, 22962, 22963, 26244, 26245, 26247, 26248, 26253, 26254, 26256, 26257, 26271, 26272, 26274, 26275, 26280, 26281, 26283, 26284, 26325, 26326, 26328, 26329, 26334, 26335, 26337, 26338, 26352, 26353, 26355, 26356, 26361, 26362, 26364, 26365, 26487, 26488, 26490, 26491, 26496, 26497, 26499, 26500, 26514, 26515, 26517, 26518, 26523, 26524, 26526,
26527, 26568, 26569, 26571, 26572, 26577, 26578, 26580, 26581, 26595, 26596, 26598, 26599, 26604, 26605, 26607, 26608, 26973, 26974, 26976, 26977, 26982, 26983, 26985, 26986, 27000, 27001, 27003, 27004, 27009, 27010, 27012, 27013, 27054, 27055, 27057, 27058, 27063, 27064, 27066, 27067, 27081, 27082, 27084, 27085, 27090, 27091, 27093, 27094, 27216, 27217, 27219, 27220, 27225, 27226, 27228, 27229, 27243, 27244, 27246, 27247, 27252, 27253, 27255, 27256, 27297, 27298, 27300, 27301, 27306, 27307, 27309, 27310, 27324, 27325, 27327, 27328, 27333, 27334, 27336, 27337, 28431, 28432, 28434, 28435, 28440, 28441, 28443, 28444, 28458, 28459, 28461, 28462, 28467, 28468, 28470, 28471, 28512, 28513, 28515, 28516, 28521, 28522, 28524, 28525, 28539, 28540, 28542, 28543, 28548, 28549, 28551, 28552, 28674, 28675,
28677, 28678, 28683, 28684, 28686, 28687, 28701, 28702, 28704, 28705, 28710, 28711, 28713, 28714, 28755, 28756, 28758, 28759, 28764, 28765, 28767, 28768, 28782, 28783, 28785, 28786, 28791, 28792, 28794, 28795, 29160, 29161, 29163, 29164, 29169, 29170, 29172, 29173, 29187, 29188, 29190, 29191, 29196, 29197, 29199, 29200, 29241, 29242, 29244, 29245, 29250, 29251, 29253, 29254, 29268, 29269, 29271, 29272, 29277, 29278, 29280, 29281, 29403, 29404, 29406, 29407, 29412, 29413, 29415, 29416, 29430, 29431, 29433, 29434, 29439, 29440, 29442, 29443, 29484, 29485, 29487, 29488, 29493, 29494, 29496, 29497, 29511, 29512, 29514, 29515, 29520, 29521, 29523, 29524, 59049, 59050, 59052, 59053, 59058, 59059, 59061, 59062, 59076, 59077, 59079, 59080, 59085, 59086, 59088, 59089, 59130, 59131, 59133, 59134, 59139,
59140, 59142, 59143, 59157, 59158, 59160, 59161, 59166, 59167, 59169, 59170, 59292, 59293, 59295, 59296, 59301, 59302, 59304, 59305, 59319, 59320, 59322, 59323, 59328, 59329, 59331, 59332, 59373, 59374, 59376, 59377, 59382, 59383, 59385, 59386, 59400, 59401, 59403, 59404, 59409, 59410, 59412, 59413, 59778, 59779, 59781, 59782, 59787, 59788, 59790, 59791, 59805, 59806, 59808, 59809, 59814, 59815, 59817, 59818, 59859, 59860, 59862, 59863, 59868, 59869, 59871, 59872, 59886, 59887, 59889, 59890, 59895, 59896, 59898, 59899, 60021, 60022, 60024, 60025, 60030, 60031, 60033, 60034, 60048, 60049, 60051, 60052, 60057, 60058, 60060, 60061, 60102, 60103, 60105, 60106, 60111, 60112, 60114, 60115, 60129, 60130, 60132, 60133, 60138, 60139, 60141, 60142, 61236, 61237, 61239, 61240, 61245, 61246, 61248, 61249,
61263, 61264, 61266, 61267, 61272, 61273, 61275, 61276, 61317, 61318, 61320, 61321, 61326, 61327, 61329, 61330, 61344, 61345, 61347, 61348, 61353, 61354, 61356, 61357, 61479, 61480, 61482, 61483, 61488, 61489, 61491, 61492, 61506, 61507, 61509, 61510, 61515, 61516, 61518, 61519, 61560, 61561, 61563, 61564, 61569, 61570, 61572, 61573, 61587, 61588, 61590, 61591, 61596, 61597, 61599, 61600, 61965, 61966, 61968, 61969, 61974, 61975, 61977, 61978, 61992, 61993, 61995, 61996, 62001, 62002, 62004, 62005, 62046, 62047, 62049, 62050, 62055, 62056, 62058, 62059, 62073, 62074, 62076, 62077, 62082, 62083, 62085, 62086, 62208, 62209, 62211, 62212, 62217, 62218, 62220, 62221, 62235, 62236, 62238, 62239, 62244, 62245, 62247, 62248, 62289, 62290, 62292, 62293, 62298, 62299, 62301, 62302, 62316, 62317, 62319,
62320, 62325, 62326, 62328, 62329, 65610, 65611, 65613, 65614, 65619, 65620, 65622, 65623, 65637, 65638, 65640, 65641, 65646, 65647, 65649, 65650, 65691, 65692, 65694, 65695, 65700, 65701, 65703, 65704, 65718, 65719, 65721, 65722, 65727, 65728, 65730, 65731, 65853, 65854, 65856, 65857, 65862, 65863, 65865, 65866, 65880, 65881, 65883, 65884, 65889, 65890, 65892, 65893, 65934, 65935, 65937, 65938, 65943, 65944, 65946, 65947, 65961, 65962, 65964, 65965, 65970, 65971, 65973, 65974, 66339, 66340, 66342, 66343, 66348, 66349, 66351, 66352, 66366, 66367, 66369, 66370, 66375, 66376, 66378, 66379, 66420, 66421, 66423, 66424, 66429, 66430, 66432, 66433, 66447, 66448, 66450, 66451, 66456, 66457, 66459, 66460, 66582, 66583, 66585, 66586, 66591, 66592, 66594, 66595, 66609, 66610, 66612, 66613, 66618, 66619,
66621, 66622, 66663, 66664, 66666, 66667, 66672, 66673, 66675, 66676, 66690, 66691, 66693, 66694, 66699, 66700, 66702, 66703, 67797, 67798, 67800, 67801, 67806, 67807, 67809, 67810, 67824, 67825, 67827, 67828, 67833, 67834, 67836, 67837, 67878, 67879, 67881, 67882, 67887, 67888, 67890, 67891, 67905, 67906, 67908, 67909, 67914, 67915, 67917, 67918, 68040, 68041, 68043, 68044, 68049, 68050, 68052, 68053, 68067, 68068, 68070, 68071, 68076, 68077, 68079, 68080, 68121, 68122, 68124, 68125, 68130, 68131, 68133, 68134, 68148, 68149, 68151, 68152, 68157, 68158, 68160, 68161, 68526, 68527, 68529, 68530, 68535, 68536, 68538, 68539, 68553, 68554, 68556, 68557, 68562, 68563, 68565, 68566, 68607, 68608, 68610, 68611, 68616, 68617, 68619, 68620, 68634, 68635, 68637, 68638, 68643, 68644, 68646, 68647, 68769,
68770, 68772, 68773, 68778, 68779, 68781, 68782, 68796, 68797, 68799, 68800, 68805, 68806, 68808, 68809, 68850, 68851, 68853, 68854, 68859, 68860, 68862, 68863, 68877, 68878, 68880, 68881, 68886, 68887, 68889, 68890, 78732, 78733, 78735, 78736, 78741, 78742, 78744, 78745, 78759, 78760, 78762, 78763, 78768, 78769, 78771, 78772, 78813, 78814, 78816, 78817, 78822, 78823, 78825, 78826, 78840, 78841, 78843, 78844, 78849, 78850, 78852, 78853, 78975, 78976, 78978, 78979, 78984, 78985, 78987, 78988, 79002, 79003, 79005, 79006, 79011, 79012, 79014, 79015, 79056, 79057, 79059, 79060, 79065, 79066, 79068, 79069, 79083, 79084, 79086, 79087, 79092, 79093, 79095, 79096, 79461, 79462, 79464, 79465, 79470, 79471, 79473, 79474, 79488, 79489, 79491, 79492, 79497, 79498, 79500, 79501, 79542, 79543, 79545, 79546,
79551, 79552, 79554, 79555, 79569, 79570, 79572, 79573, 79578, 79579, 79581, 79582, 79704, 79705, 79707, 79708, 79713, 79714, 79716, 79717, 79731, 79732, 79734, 79735, 79740, 79741, 79743, 79744, 79785, 79786, 79788, 79789, 79794, 79795, 79797, 79798, 79812, 79813, 79815, 79816, 79821, 79822, 79824, 79825, 80919, 80920, 80922, 80923, 80928, 80929, 80931, 80932, 80946, 80947, 80949, 80950, 80955, 80956, 80958, 80959, 81000, 81001, 81003, 81004, 81009, 81010, 81012, 81013, 81027, 81028, 81030, 81031, 81036, 81037, 81039, 81040, 81162, 81163, 81165, 81166, 81171, 81172, 81174, 81175, 81189, 81190, 81192, 81193, 81198, 81199, 81201, 81202, 81243, 81244, 81246, 81247, 81252, 81253, 81255, 81256, 81270, 81271, 81273, 81274, 81279, 81280, 81282, 81283, 81648, 81649, 81651, 81652, 81657, 81658, 81660,
81661, 81675, 81676, 81678, 81679, 81684, 81685, 81687, 81688, 81729, 81730, 81732, 81733, 81738, 81739, 81741, 81742, 81756, 81757, 81759, 81760, 81765, 81766, 81768, 81769, 81891, 81892, 81894, 81895, 81900, 81901, 81903, 81904, 81918, 81919, 81921, 81922, 81927, 81928, 81930, 81931, 81972, 81973, 81975, 81976, 81981, 81982, 81984, 81985, 81999, 82000, 82002, 82003, 82008, 82009, 82011, 82012, 85293, 85294, 85296, 85297, 85302, 85303, 85305, 85306, 85320, 85321, 85323, 85324, 85329, 85330, 85332, 85333, 85374, 85375, 85377, 85378, 85383, 85384, 85386, 85387, 85401, 85402, 85404, 85405, 85410, 85411, 85413, 85414, 85536, 85537, 85539, 85540, 85545, 85546, 85548, 85549, 85563, 85564, 85566, 85567, 85572, 85573, 85575, 85576, 85617, 85618, 85620, 85621, 85626, 85627, 85629, 85630, 85644, 85645,
85647, 85648, 85653, 85654, 85656, 85657, 86022, 86023, 86025, 86026, 86031, 86032, 86034, 86035, 86049, 86050, 86052, 86053, 86058, 86059, 86061, 86062, 86103, 86104, 86106, 86107, 86112, 86113, 86115, 86116, 86130, 86131, 86133, 86134, 86139, 86140, 86142, 86143, 86265, 86266, 86268, 86269, 86274, 86275, 86277, 86278, 86292, 86293, 86295, 86296, 86301, 86302, 86304, 86305, 86346, 86347, 86349, 86350, 86355, 86356, 86358, 86359, 86373, 86374, 86376, 86377, 86382, 86383, 86385, 86386, 87480, 87481, 87483, 87484, 87489, 87490, 87492, 87493, 87507, 87508, 87510, 87511, 87516, 87517, 87519, 87520, 87561, 87562, 87564, 87565, 87570, 87571, 87573, 87574, 87588, 87589, 87591, 87592, 87597, 87598, 87600, 87601, 87723, 87724, 87726, 87727, 87732, 87733, 87735, 87736, 87750, 87751, 87753, 87754, 87759,
87760, 87762, 87763, 87804, 87805, 87807, 87808, 87813, 87814, 87816, 87817, 87831, 87832, 87834, 87835, 87840, 87841, 87843, 87844, 88209, 88210, 88212, 88213, 88218, 88219, 88221, 88222, 88236, 88237, 88239, 88240, 88245, 88246, 88248, 88249, 88290, 88291, 88293, 88294, 88299, 88300, 88302, 88303, 88317, 88318, 88320, 88321, 88326, 88327, 88329, 88330, 88452, 88453, 88455, 88456, 88461, 88462, 88464, 88465, 88479, 88480, 88482, 88483, 88488, 88489, 88491, 88492, 88533, 88534, 88536, 88537, 88542, 88543, 88545, 88546, 88560, 88561, 88563, 88564, 88569, 88570, 88572, 88573]

from bisect import bisect_left

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

for n in A:
    print(T[bisect_left(T, n)])
