An N-length wobbly number is of the form "ababababab..." and so on of length N, where a != b.
A 3-length wobbly number would be of form "aba".
Eg: 101, 121, 131, 252, 646 etc
But 111, 222, 999 etc are not 3-length wobbly number, because here a != b condition is not satisfied.
Also 010 is not a 3-length wobbly number because it has preceding 0. So 010 equals 10 and 10 is not a 3-length wobbly number.
A 4-length wobbly number would be of form "abab".
Eg: 2323, 3232, 9090, 1414 etc
Similarly we can form a list of N-length wobbly numbers.
Now your task is to find K^th wobbly number from a lexicographically sorted list of N-length wobbly numbers. If the number does not exist print -1 else print the K^th wobbly number. See the sample test case and explanation for more clarity.
Input:
First line contains T - number of test cases
Each of the next T lines contains two space separated integers - N and K.
Output:
For each test case print the required output in a new line.
Constraints:
1 ≤ T ≤ 100
3 ≤ N ≤ 1000
1 ≤ K ≤ 100
SAMPLE INPUT
6
3 1
3 2
3 100
4 3
4 4
5 2
SAMPLE OUTPUT
101
121
-1
1313
1414
12121
Explanation
First 10 terms of 3-length wobbly numbers arranged lexicographically is as follows:
101, 121, 131, 141, 151, 161, 171, 181, 191, 202
1st wobbly number of length 3 is 101.
2nd wobbly number of length 3 is 121.
100th wobbly number of length 3 does not exist, so the output is -1.
First 10 terms of 4-length wobbly numbers arranged lexicographically is as follows:
1010, 1212, 1313, 1414, 1515, 1616, 1717, 1818, 1919, 2020
3rd wobbly number of length 4 is 1313.
4th wobbly number of length 4 is 1414.
Similarly 2nd wobbly number of length 5 is 12121
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to parse the problem: Given N (length) and K, we need to output K-th wobbly number of length N, where wobbly number defined as alternating digits 'abab...' with a != b, and the number cannot have leading zero (so first digit a cannot be 0). Also, the list is sorted lexicographically. Since numbers are strings of digits, lexicographic order is the same as numeric order when they have the same length? Actually for fixed-length numbers, lexicographic order (by digit strings) is the same as numeric order because leading zeros are not allowed, so each string length is N. So lexicographically sorted is same as increasing numeric order.
We need to find the K-th smallest N-length wobbly number.
Definition: pattern: if N is odd: the pattern repeats ababa... ending with a? Let's see: For N=3, pattern "aba": first digit a, second b, third a. So length odd: number of digits = 2k+1? Actually N can be any >=3. Pattern: digits positions 1..N. For position i (1-indexed), digit = a if i odd, = b if i even. And condition a != b, a in {1..9} (since leading digit cannot be 0), b in {0..9}? Actually b can be 0? For 3-length wobbly number, examples: 101 (b=0) is allowed, as per sample. Indeed 101 is listed. So b can be 0. But second digit b can be zero? Yes. However, the whole number must not have leading zero, which is satisfied because a is first digit and non-zero. Also b can be any digit 0-9 except possibly equal to a? Condition a != b. So b can be zero, but b cannot equal a.
Thus the set of wobbly numbers of length N: Choose a from 1..9, b from 0..9, a != b. Then form string: for i from 1 to N, digit = a if i%2==1 else b. That yields a number.
Now, lexicographic sorting: Since a is the first digit, the numbers are sorted primarily by a, then by b? Not exactly: For numbers with same a, the second digit is b, so they are sorted by b as well. But careful: For N>2, the third digit is a again, so the order among numbers with same a and b is same; they are distinct only by a and b. So the entire list is determined by pair (a,b) with a in 1..9, b in 0..9, a != b. So total count = 9 * 10 - (cases where a == b, i.e., a from 1..9, b = same digit? That is 9 possibilities (a=1,b=1; a=2,b=2; ... a=9,b=9). So total = 90 - 9? Actually 910 = 90 possible pairs. Remove those where a==b, which are 9. So total = 81. So for any N (>=3), maximum number of distinct wobbly numbers is 81. But wait: For N=3, there are 81 numbers? Let's check: a from 1..9, b from 0..9, a != b => 99? Actually if b can be 0..9 inclusive, there are 10 possibilities for b. For each a, exclude b = a (1 out of 10) -> 9 allowed. So 9*9=81. Yes. So maximum K is 81. However constraints: K ≤ 100, but it's possible K > 81, then output -1.
But check sample: For N=3, K=100 output -1, consistent with max 81.
For N=4, pattern "abab": first a, second b, third a, fourth b. So again determined by (a,b) with a!=b, a in 1..9, b in 0..9. So also 81 numbers. So for any N, it's always 81 numbers? But wait: Are there any numbers that might be duplicates? For even N, pattern repeats abab... length N even: digits positions: odd positions a, even positions b. So indeed it's fully determined by (a,b). So total 81 distinct numbers. For odd N, pattern "ababa..." with ending a, also determined by (a,b). So it's always 81. But let's check: Could there be any number that fits both patterns? No. So total count is 81.
Thus the problem reduces: Given N and K, if K > 81, output -1. Otherwise output the K-th smallest number formed by (a,b) pairs sorted lexicographically as strings of length N. Since the string is built from a and b, sorting order by (a,b) is consistent: The first digit a is primary, then second digit b is secondary (since the third digit is a again, but if two numbers have same a and b, they are identical). So we need to order all pairs (a,b) with a from 1..9, b from 0..9, a != b, sorted by a (increasing) and for each a, by b (increasing). This yields the lexicographic order of the strings? Let's verify: Compare two strings X and Y of length N, both wobbly numbers. Compare digit by digit. At position 1, they have a1 and a2. If a1 < a2, then X < Y. If a1 == a2, then at position 2, they have b1 and b2. If b1 < b2, then X < Y. If b1 == b2, then since pattern repeats, all subsequent digits will also match (since odd positions are a, even are b). So they are equal. But if a1==a2 and b1==b2, then it's the same number, and that pair is unique because we exclude duplicates. So sorting by (a,b) as primary key a, then b, is correct.
Thus the K-th wobbly number corresponds to the K-th pair in the sorted order: first a=1, b from 0 to 9 excluding b=1, then a=2, b from 0 to 9 excluding b=2, etc. However, careful: For a fixed a, the sorted b's increasing: b=0,1,2,...,9 except skip b=a. So the order within each a is: b=0,1,2,...,a-1, a+1,...9. That is, we go from 0 to 9, skipping a. So we can compute K. For each a from 1 to 9, there are 9 valid b's. So essentially, total pairs = 81. The K-th pair can be found by: Let idx = K-1. Let a_idx = idx // 9 (integer division) gives which a in order: 0-> a=1, 1-> a=2, ... up to 8-> a=9. Then b_idx = idx % 9 gives which b in the sorted list of 9 possible b values for that a. But careful: The sorted list of b for a given a is: 0,1,2,...,a-1, a+1,...,9. That's 9 numbers. So mapping b_idx to the actual b value: if b_idx < a? Actually need to see: For b values from 0 up to 9, skipping a. So for each b from 0 to 9, if b != a, it's included. So the b values sorted: all numbers from 0 to 9 except a. So the mapping: if b_idx < a, then b = b_idx; else b = b_idx + 1 (since we skip a). Because for indices before a, they correspond to numbers 0,...,a-1 (count a numbers). For b_idx >= a, we need to add 1 to skip a. Since b_idx ranges 0..8. Example: a=1. Sorted b: skip b=1, so b values: 0,2,3,4,5,6,7,8,9. That's 9 numbers. b_idx=0 => b=0; b_idx=1 => b=2; b_idx=2=>3; ... b_idx=8=>9. Using mapping: a=1, b_idx < 1? (b_idx<1) => b_idx=0 => b=0; else b = b_idx+1: for b_idx=1 => 1+1=2 correct; for b_idx=8 => 9 correct. So formula works.
Now after we have a and b, we construct the string of length N: for i from 1 to N: if i odd => digit = a (as character), else => digit = b. Then output.
Edge Cases: K > 81 => output -1. N can be up to 1000, but that's fine; we just generate the string.
We also need to consider if any additional constraints: K ≤ 100, but we handle up to 81 anyway.
Now test with sample:
N=3, K=1: idx=0, a_idx=0//9=0 => a=1. b_idx=0%9=0. For a=1, b_idx<1 => b=0. So number: "1 0 1" => "101". Good.
K=2: idx=1, a_idx=0 (since 1//9=0) => a=1. b_idx=1 => for a=1, b_idx>=1 => b=1+1=2 => number "121". Good.
But wait: Are there any other wobbly numbers? The problem description: "An N-length wobbly number is of the form "abababab..." and so on of length N, where a != b." So indeed it's exactly that.
Thus algorithm per test case:
Input N, K.
If K > 81: print -1.
Else:
compute idx = K-1.
a_num = idx // 9 + 1 (since a from 1 to 9)
b_idx = idx % 9
if b_idx < a_num: b_num = b_idx
else: b_num = b_idx + 1
Now construct string: list of characters of length N; for i in range(N): digit = a_num if i%2==0 else b_num; append str(digit).
Print string.
Complexity: O(N) per test case, N up to 1000, T up to 100 => up to 100k, fine.
Edge: N can be 3 to 1000. So constructing string of length up to 1000.
Now check if any leading zero issue? a_num is at least 1, so fine.
Also, note: b_num can be 0, but that's allowed.
Thus solution.
But wait: Check if there's any nuance about lexicographic order when N is even? For even length, the pattern is abab... So the string is determined by (a,b). Our sorting by (a,b) is consistent. However, consider a=1,b=0 => "1010" (N=4). Next a=1,b=2 => "1212". Compare "1010" vs "1212": first digit both 1; second digit 0 vs 2 => 0 < 2, so "1010" < "1212". That matches our ordering (b ascending). So fine.
Also, for a=2,b=0 => "2020". Compare to a=1,b=9 => "1919"? Actually a=1,b=9 => "1919". Since a=1 < 2, "1919" < "2020". So ordering by a then b is correct.
Thus done.
But the problem statement says "Kth wobbly number from a lexicographically sorted list". Our algorithm works.
Now I'll implement in Python.
Implementation steps:
def get_wobbly(N, K):
if K > 81:
return "-1"
idx = K - 1
a = idx // 9 + 1
b_idx = idx % 9
if b_idx < a:
b = b_idx
else:
b = b_idx + 1
# construct string
res = []
for i in range(N):
if i % 2 == 0:
res.append(str(a))
else:
res.append(str(b))
return "".join(res)
But careful: In Python, indexing starts at 0: i=0 => first digit.
Now check other cases: N=3, K=81? That should be last pair: a=9, b_idx=8? Let's compute: idx=80, a_idx=80//9=8 -> a=9, b_idx=80%9=8. For a=9, b_idx<9? 8<9, so b=8. So number: "989"? Actually pattern "aba": 9,8,9 -> "989". Check: Does "989" belong? a=9,b=8, a!=b, leading digit 9, yes. And it should be the largest? Let's sort: For a=9, b sorted from 0 to 8 (skip 9). So largest b for a=9 is 8. Then a=9 with b=8 is the last. So "989" is correct.
Now check K=80: idx=79, a_idx=79//9=8 (since 8*9=72, 79-72=7) -> a=9, b_idx=7 -> b=7 => "979"? Actually b_idx<9 => b=7, so "979". That is plausible.
Now, also we might want to check that the maximum K is 81, but constraint says K up to 100, but if K >81 we output -1.
Now, what about N even? Works.
Thus solution done.
But wait, there is a nuance: The problem statement: "Roy is looking for Wobbly Numbers." Possibly there might be a twist: For N=1 or N=2? But constraints N>=3, so fine.
Thus answer is simple.
However, we must consider input format: First line T, then T lines each with N K.
I'll implement accordingly.
But wait: Could there be any other constraints? N up to 1000, but K small, but we are constructing a string of length N which could be up to 1000, that's fine.
Now, just to be safe, we might want to compute the number as a string of repeated characters rather than loop; we can use: pattern = (a,b) repeated. For odd N, the last digit is a. So we can create a string = (str(a)+str(b)) * (N//2) + (str(a) if N%2 else '') . But careful: For N even, N//2 pairs. For N odd, (N//2) pairs plus an extra a at the end. So we can do:
pair = str(a) + str(b)
repeat = N//2
if N%2 == 0:
result = pair * repeat
else:
result = pair * repeat + str(a)
But this works only if we correctly handle that for odd N, the pattern is a,b,a,b,...,a. So yes.
But we need to ensure correct ordering? This yields same as loop.
Now check: For N=3, pair='10', repeat=1 (3//2=1) => pair*1 = '10', plus str(a) => '101' correct. For N=4, repeat=2 => '1010'? Actually if a=1,b=0 => pair='10', repeat=2 => '1010' correct.
Thus we can do that.
Now code.
Now there is a nuance: For N large, using multiplication to build string is efficient.
Thus solution:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data: return
it = iter(data)
t = int(next(it))
out = []
for _ in range(t):
N = int(next(it)); K = int(next(it))
if K > 81:
out.append("-1")
else:
idx = K - 1
a = idx // 9 + 1
b_idx = idx % 9
if b_idx < a:
b = b_idx
else:
b = b_idx + 1
pair = str(a) + str(b)
repeat = N // 2
if N % 2 == 0:
num_str = pair * repeat
else:
num_str = pair * repeat + str(a)
out.append(num_str)
sys.stdout.write("\n".join(out))
But wait: For N odd, we add an extra a at the end. However, careful: For N=3, we need "aba". Our method: pair='ab', repeat=1 => 'ab', then + str(a) => 'aba'. That's correct. For N=5: repeat=2 => 'abab' then + 'a' => 'ababa', correct.
But I must double-check the ordering for b mapping. For a given a, the sorted b values: 0,1,2,...,9 except a. The order of b_idx from 0 to 8 corresponds to these b values. The mapping: if b_idx < a then b = b_idx else b = b_idx+1. Works for all a from 1 to 9 inclusive. Let's test a=5: valid b: 0,1,2,3,4,6,7,8,9. According to formula, b_idx from 0 to 8: b_idx=0->0;1->1;2->2;3->3;4->4;5->6;6->7;7->8;8->9. Works.
Thus correct.
Now possible edge: K exactly 81 -> idx=80, a_idx=80//9=8 -> a=9, b_idx=8 -> b_idx<9? yes b=8, number = ... fine.
Now output -1 for K>81.
Now check constraints: T up to 100, N up to 1000, K up to 100, so fine.
Thus answer.
But wait, the sample included output for N=4 K=3 and K=4, we matched.
Now, is there any possibility that there are more than 81 wobbly numbers for some N? Let's think: For N=1? Not allowed. For N=2? Not allowed. So for N>=3, it's exactly 81.
But could there be any wobbly numbers where the pattern is not strictly alternating from the start? For example, could it be "ababa" with a and b swapped? Actually definition: "abababab..." of length N, where a != b. That pattern is defined with a as the first digit, b as second, then repeating. So the pattern is determined by a and b. So no.
Thus solution is correct.
Now I'll implement final code accordingly.
But before finalizing, let's consider the problem statement's note: "Also 010 is not a 3-length wobbly number because it has preceding 0. So 010 equals 10 and 10 is not a 3-length wobbly number." This reinforces that first digit cannot be 0.
Now, also they mention "Kth wobbly number from a lexicographically sorted list". Since numbers are strings of same length, lexicographic equals numeric.
Thus solution stands.
Now we can add some comments.
Now I'll produce final answer in Python.
However, I'm asked: "You are an AI programming assistant, respond only in code." So the final output should be the code.
Thus I'll output the solution code with function solve() and if name == 'main': solve().
I'll also include fast I/O.
Let's produce final code:
python
import sys
def solve():
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
t = int(next(it))
out_lines = []
MAX = 9 * 9 # 81
for _ in range(t):
N = int(next(it))
K = int(next(it))
if K > MAX:
out_lines.append("-1")
continue
idx = K - 1
a = idx // 9 + 1 # a from 1 to 9
b_idx = idx % 9
if b_idx < a:
b = b_idx
else:
b = b_idx + 1
# Construct the wobbly number string
pair = str(a) + str(b)
repeat = N // 2
if N % 2 == 0:
num = pair * repeat
else:
num = pair * repeat + str(a)
out_lines.append(num)
sys.stdout.write("\n".join(out_lines))
if __name__ == "__main__":
solve()
That is the solution. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
t = int(next(it))
out = []
MAX_WOBBLY = 9 * 9 # 81 possible numbers for any N >= 3
code
for _ in range(t):
N = int(next(it))
K = int(next(it))
if K > MAX_WOBBLY:
out.append("-1")
continue
idx = K - 1 # zero‑based index
a = idx // 9 + 1 # first digit, 1..9
b_idx = idx % 9 # position among the 9 allowed b's
# map b_idx to the actual digit b (0..9, b != a)
if b_idx < a:
b = b_idx
else:
b = b_idx + 1
pair = str(a) + str(b)
repeat = N // 2
if N % 2 == 0: # even length → "abab...ab"
num = pair * repeat
else: # odd length → "abab...a"
num = pair * repeat + str(a)
out.append(num)
sys.stdout.write("\n".join(out))