def get_color(x, y):
while x > 1:
# Calculate the size of the triangle at step x
size = (1 << (x - 1)) - 1 # size = 2^(x-1) - 1
# Check if (x, y) is in the inverted triangle
if y > size:
return 0 # Blue
# Determine if we are in the left or right triangle
if y == size:
return 1 # Red (the point at the tip of the inverted triangle)
# Move to the previous triangle
x -= 1
# Adjust y for the next layer
if y > size // 2:
y -= (size // 2 + 1) # Move to the right triangle
# If y <= size // 2, we stay in the left triangle
return 1 # If we reach step 1, it is always red
import sys
# Read input
input = sys.stdin.read
data = list(map(int, input().split()))
Q = data[0]
results = []
# Process each query
for i in range(1, 2 * Q, 2):
x = data[i]
y = data[i + 1]
results.append(get_color(x, y))
# Print results
print(" ".join(map(str, results)))
ZGVmIGdldF9jb2xvcih4LCB5KToKICAgIHdoaWxlIHggPiAxOgogICAgICAgICMgQ2FsY3VsYXRlIHRoZSBzaXplIG9mIHRoZSB0cmlhbmdsZSBhdCBzdGVwIHgKICAgICAgICBzaXplID0gKDEgPDwgKHggLSAxKSkgLSAxICAjIHNpemUgPSAyXih4LTEpIC0gMQogICAgICAgIAogICAgICAgICMgQ2hlY2sgaWYgKHgsIHkpIGlzIGluIHRoZSBpbnZlcnRlZCB0cmlhbmdsZQogICAgICAgIGlmIHkgPiBzaXplOgogICAgICAgICAgICByZXR1cm4gMCAgIyBCbHVlCiAgICAgICAgCiAgICAgICAgIyBEZXRlcm1pbmUgaWYgd2UgYXJlIGluIHRoZSBsZWZ0IG9yIHJpZ2h0IHRyaWFuZ2xlCiAgICAgICAgaWYgeSA9PSBzaXplOgogICAgICAgICAgICByZXR1cm4gMSAgIyBSZWQgKHRoZSBwb2ludCBhdCB0aGUgdGlwIG9mIHRoZSBpbnZlcnRlZCB0cmlhbmdsZSkKICAgICAgICAKICAgICAgICAjIE1vdmUgdG8gdGhlIHByZXZpb3VzIHRyaWFuZ2xlCiAgICAgICAgeCAtPSAxCiAgICAgICAgCiAgICAgICAgIyBBZGp1c3QgeSBmb3IgdGhlIG5leHQgbGF5ZX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