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()
ZGVmIGdldF9jb2xvcih4LCB5KToKICAgICMgQmFzZSBjYXNlOiBTdGVwIDEKICAgIGlmIHggPT0gMToKICAgICAgICByZXR1cm4gMSAgIyBSZWQgcG9pbnQKCiAgICAjIFJlY3Vyc2l2ZSBkZXNjZW50IHRocm91Z2ggdGhlIHN0cnVjdHVyZQogICAgIyBEZXRlcm1pbmUgdGhlIHNpemUgb2YgdGhlIGN1cnJlbnQgdHJpYW5nbGUKICAgIHNpemUgPSAxCiAgICB3aGlsZSBzaXplIDwgeDoKICAgICAgICBzaXplID0gc2l6ZSAqIDIgKyAxICAjIEVhY2ggc2l6ZSBncm93cyBhcyAyICogcHJldmlvdXMgc2l6ZSArIDEKCiAgICAjIE5vdyB3ZSBuZWVkIHRvIHRyYXZlcnNlIGJhY2sgdXAgdGhlIHRyaWFuZ2xlCiAgICB3aGlsZSB4ID4gMToKICAgICAgICAjIENoZWNrIGlmIHdlIGFyZSBpbiB0aGUgYmx1ZSB0cmlhbmdsZQogICAgICAgICMgVGhlIGJsdWUgdHJpYW5nbGUncyByb3dzIHdpbGwgaGF2ZSAoeC0xKSB0b3RhbCBwb2ludHMgYW5kIHkgd2lsbCBmYWxsIGludG8gdGhhdCByYW5nZQogICAgICAgIGlmIHkgPD0gKHggLSAxKSAvLyAyOgogICAgICAgICAgICAjIEluIGxlZnQgdHJpYW5nbGUKICAgICAgICAgICAgeCAtPSAxCiAgICAgICAgZWxpZiB5ID4gKHggLSAxKSAvLyAyICsgKHggLSAxKSAvLyAyICsgMToKICAgICAgICAgICAgIyBJbiByaWdodCB0cmlhbmdsZQogICAgICAgICAgICB4IC09IDEKICAgICAgICAgICAgeSAtPSAoeCAtIDEpIC8vIDIgKyAxICAjIEFkanVzdCB5IHRvIHRoZSByaWdodCB0cmlhbmdsZSdzIHktc3BhY2UKICAgICAgICBlbHNlOgogICAgICAgICAgICAjIFdlJ3JlIGluIHRoZSBibHVlIHRyaWFuZ2xlIGFyZWEgKGJsdWUgdHJpYW5nbGUgcm93cyBzdGFydCBmcm9tICh4LTEpKQogICAgICAgICAgICByZXR1cm4gMCAgIyBCbHVlIHBvaW50CgogICAgcmV0dXJuIDEgICMgV2UgcmVhY2hlZCBiYWNrIHRvIHN0ZXAgMSwgdGh1cyBpdCdzIHJlZAoKZGVmIG1haW4oKToKICAgIGltcG9ydCBzeXMKICAgIGlucHV0ID0gc3lzLnN0ZGluLnJlYWQKICAgIGRhdGEgPSBpbnB1dCgpLnN0cmlwKCkuc3BsaXRsaW5lcygpCiAgICAKICAgIFEgPSBpbnQoZGF0YVswXSkKICAgIHJlc3VsdHMgPSBbXQogICAgCiAgICBmb3IgaSBpbiByYW5nZSgxLCBRICsgMSk6CiAgICAgICAgeCwgeSA9IG1hcChpbnQsIGRhdGFbaV0uc3BsaXQoKSkKICAgICAgICBjb2xvciA9IGdldF9jb2xvcih4LCB5KQogICAgICAgIHJlc3VsdHMuYXBwZW5kKGNvbG9yKQogICAgCiAgICBwcmludCgiXG4iLmpvaW4obWFwKHN0ciwgcmVzdWx0cykpKQoKaWYgX19uYW1lX18gPT0gIl9fbWFpbl9fIjoKICAgIG1haW4oKQo=