C. A Good Problem

Problem Statement

You are given four positive integers n,l,r,kn, l, r, k. You need to find the lexicographically smallest* array aa of length nn, consisting of integers, such that:

  1. For every 1in1 \leq i \leq n, lairl \leq a_i \leq r.
  2. a1&a2&&an=a1a2ana_1 \And a_2 \And \ldots \And a_n = a_1 \oplus a_2 \oplus \ldots \oplus a_n, where &\And denotes the bitwise AND operation and \oplus denotes the bitwise XOR operation.

If no such array exists, output 1-1. Otherwise, since the entire array might be too large to output, output aka_k only.

Note: An array aa is lexicographically smaller than an array bb if and only if one of the following holds:

  • aa is a prefix of bb, but aba \neq b; or
  • In the first position where aa and bb differ, the array aa has a smaller element than the corresponding element in bb.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t104)(1 \leq t \leq 10^4). The description of the test cases follows.

Each test case contains four positive integers n,l,r,kn, l, r, k (1kn1018,1lr1018)(1 \leq k \leq n \leq 10^{18}, 1 \leq l \leq r \leq 10^{18}).

Output

For each test case, output aka_k or 1-1 if no array meets the conditions.

Solution

If nn is odd, we can construct the array with all elements equal to ll.

Then we can think what happens if nn is even. In this case, we can have n2n-2 elements equal to ll which means the & operation will be ll but the xor operation will be 00.

The last two elements should satisfy the condition that l&v1&v2=v1v2=resl \And v_1 \And v_2 = v_1 \oplus v_2 = res. Consider a single bit in ll, if we want to keep this bit in the result resres, this bit should be set in both v1v_1 and v2v_2. If both bits are set, then xorxor will be 00 and that means they are not valid. So for all bits that are set in ll, we can not set them in both v1v_1 and v2v_2. And the values v1v_1 and v2v_2 should be greater than ll. So we can have v1=v2=2(largest bit of l)v_1 = v_2 = 2^{\text{(largest bit of } l\text{)}}. In this case, res=0\text{res} = 0 and v1v_1, v2v_2 is the smallest possible value.

submission link