def get_color(x, y):
    while x > 1:
        # Calculate the size of the triangle at level x
        size = 2 * (2 ** (x - 1)) - 1
        # Calculate the position of the inverted triangle
        if y > size // 2 + 1:  # In the inverted triangle
            return 0  # Blue
        # Check if it's in the left or right triangle
        if y <= (size // 2 + 1):
            # It's in the left triangle
            x -= 1
            # y remains the same
        else:
            # It's in the right triangle
            x -= 1
            y -= (size // 2 + 1)
    return 1  # Red

def main():
    import sys
    input = sys.stdin.read
    data = input().splitlines()
    
    Q = int(data[0])  # Read number of queries
    results = []
    
    for i in range(1, Q + 1):
        n, y = map(int, data[i].split())
        results.append(get_color(n, y))
    
    # Print all results, one per line
    print('\n'.join(map(str, results)))

# Ensure the main function is called when the script is executed
if __name__ == "__main__":
    main()