<p>You are given an array <code>nums</code> consisting of non-negative integers. You are also given a <code>queries</code> array, where <code>queries[i] = [x<sub>i</sub>, m<sub>i</sub>]</code>.</p>

<p>The answer to the <code>i<sup>th</sup></code> query is the maximum bitwise <code>XOR</code> value of <code>x<sub>i</sub></code> and any element of <code>nums</code> that does not exceed <code>m<sub>i</sub></code>. In other words, the answer is <code>max(nums[j] XOR x<sub>i</sub>)</code> for all <code>j</code> such that <code>nums[j] &lt;= m<sub>i</sub></code>. If all elements in <code>nums</code> are larger than <code>m<sub>i</sub></code>, then the answer is <code>-1</code>.</p>

<p>Return <em>an integer array </em><code>answer</code><em> where </em><code>answer.length == queries.length</code><em> and </em><code>answer[i]</code><em> is the answer to the </em><code>i<sup>th</sup></code><em> query.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,1,2,3,4], queries = [[3,1],[1,3],[5,6]]
<strong>Output:</strong> [3,3,7]
<strong>Explanation:</strong>
1) 0 and 1 are the only two integers not greater than 1. 0 XOR 3 = 3 and 1 XOR 3 = 2. The larger of the two is 3.
2) 1 XOR 2 = 3.
3) 5 XOR 2 = 7.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [5,2,4,6,6,3], queries = [[12,4],[8,1],[6,3]]
<strong>Output:</strong> [15,-1,5]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length, queries.length &lt;= 10<sup>5</sup></code></li>
	<li><code>queries[i].length == 2</code></li>
	<li><code>0 &lt;= nums[j], x<sub>i</sub>, m<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
</ul>

class Node:
    """Node structure for the Trie"""

    def __init__(self):
        # Array to store links
        # to child nodes (0 and 1)
        self.links = [None, None]
        self.min0 = float("inf")
        self.min1 = float("inf")

    def set_min0(self, data):
        self.min0 = min(self.min0, data)

    def set_min1(self, data):
        self.min1 = min(self.min1, data)

    def contains_key(self, bit):
        # Method to check if a specific
        # bit key is present in the child nodes
        # Returns True if the link at
        # index 'bit' is not None
        return self.links[bit] is not None

    def get(self, bit):
        # Method to get the child node
        # corresponding to a specific bit
        # Returns the child
        # node at index 'bit'
        return self.links[bit]

    def put(self, bit):
        # Method to set a child node at a
        # specific index in the links array
        # Sets the child node at index
        # 'bit' to the provided node
        self.links[bit] = Node()


class Trie:
    """Trie class"""

    def __init__(self):
        # Constructor to initialize
        # the Trie with a root node
        # Creates a new root
        # node for the Trie
        self.root = Node()

    def insert(self, num):
        # Method to insert a number into the Trie
        # Start from the root node
        node = self.root
        # Iterate through each bit of the
        # number (from left to right)
        for i in range(31, -1, -1):
            # Extract the i-th bit of the number
            bit = (num >> i) & 1

            if bit:
                node.set_min1(num)
            else:
                node.set_min0(num)

            # If the current node doesn't have a
            # child node with the current bit
            if not node.contains_key(bit):
                # Create a new child node
                # with the current bit
                node.put(bit)

            # Move to the child node
            # corresponding to the current bit
            node = node.get(bit)

    def get_max(self, num, mx):
        # Method to find the maximum
        # XOR value for a given number
        # Start from the root node
        node = self.root

        # Initialize the maximum XOR value
        max_num = 0

        # Iterate through each bit of
        # the number (from left to right)
        for i in range(31, -1, -1):
            # Extract the i-th
            # bit of the number
            bit = (num >> i) & 1

            # If the complement of the current
            # bit exists in the Trie

            if bit and node.contains_key(0) and node.min0 <= mx:
                node = node.get(0)
                max_num |= (1 << i)
            elif not bit and node.contains_key(1) and node.min1 <= mx:
                node = node.get(1)
                max_num |= (1 << i)
            elif not bit and node.contains_key(0) and node.min0 <= mx:
                node = node.get(0)
            elif bit and node.contains_key(1) and node.min1 <= mx:
                node = node.get(1)
            else:
                return -1
            


        # Return the maximum XOR value
        return max_num


class Solution:

    def maximizeXor(self, nums: List[int], queries: List[List[int]]) -> List[int]:
        nums.sort()
        answer = []

        for a, m in queries:
            
            bit = 2 ** (m.bit_length() - 1)
            num  = 0
            start, end = 0, bisect_right(nums, m)
            # pivot = num + bit
            if nums[start] > m:
                answer.append(-1)
                continue

            while bit:
                pivot = num + bit
                if nums[start] >= pivot:
                    num = pivot
                elif nums[end-1] >= pivot:
                    

                    cut = bisect_left(nums, pivot, start, end)

                    if a & bit:
                        end = cut
                    else:
                        start = cut
                        num = pivot
                bit = bit // 2

            answer.append(num^a)

        return answer
60873