def get_color(x, y):
    # Base case: Step 1
    if x == 1:
        return 1  # Red point

    # Recursive descent through the structure
    # Determine the size of the current triangle
    size = 1
    while size < x:
        size = size * 2 + 1  # Each size grows as 2 * previous size + 1

    # Now we need to traverse back up the triangle
    while x > 1:
        # Check if we are in the blue triangle
        # The blue triangle's rows will have (x-1) total points and y will fall into that range
        if y <= (x - 1) // 2:
            # In left triangle
            x -= 1
        elif y > (x - 1) // 2 + (x - 1) // 2 + 1:
            # In right triangle
            x -= 1
            y -= (x - 1) // 2 + 1  # Adjust y to the right triangle's y-space
        else:
            # We're in the blue triangle area (blue triangle rows start from (x-1))
            return 0  # Blue point

    return 1  # We reached back to step 1, thus it's red

def main():
    import sys
    input = sys.stdin.read
    data = input().strip().splitlines()
    
    Q = int(data[0])
    results = []
    
    for i in range(1, Q + 1):
        x, y = map(int, data[i].split())
        color = get_color(x, y)
        results.append(color)
    
    print("\n".join(map(str, results)))

if __name__ == "__main__":
    main()
