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()
ZGVmIGdldF9jb2xvcih4LCB5KToKICAgIHdoaWxlIHggPiAxOgogICAgICAgICMgQ2FsY3VsYXRlIHRoZSBzaXplIG9mIHRoZSB0cmlhbmdsZSBhdCBsZXZlbCB4CiAgICAgICAgc2l6ZSA9IDIgKiAoMiAqKiAoeCAtIDEpKSAtIDEKICAgICAgICAjIENhbGN1bGF0ZSB0aGUgcG9zaXRpb24gb2YgdGhlIGludmVydGVkIHRyaWFuZ2xlCiAgICAgICAgaWYgeSA+IHNpemUgLy8gMiArIDE6ICAjIEluIHRoZSBpbnZlcnRlZCB0cmlhbmdsZQogICAgICAgICAgICByZXR1cm4gMCAgIyBCbHVlCiAgICAgICAgIyBDaGVjayBpZiBpdCdzIGluIHRoZSBsZWZ0IG9yIHJpZ2h0IHRyaWFuZ2xlCiAgICAgICAgaWYgeSA8PSAoc2l6ZSAvLyAyICsgMSk6CiAgICAgICAgICAgICMgSXQncyBpbiB0aGUgbGVmdCB0cmlhbmdsZQogICAgICAgICAgICB4IC09IDEKICAgICAgICAgICAgIyB5IHJlbWFpbnMgdGhlIHNhbWUKICAgICAgICBlbHNlOgogICAgICAgICAgICAjIEl0J3MgaW4gdGhlIHJpZ2h0IHRyaWFuZ2xlCiAgICAgICAgICAgIHggLT0gMQogICAgICAgICAgICB5IC09IChzaXplIC8vIDIgKyAxKQogICAgcmV0dXJuIDEgICMgUmVkCgpkZWYgbWFpbigpOgogICAgaW1wb3J0IHN5cwogICAgaW5wdXQgPSBzeXMuc3RkaW4ucmVhZAogICAgZGF0YSA9IGlucHV0KCkuc3BsaXRsaW5lcygpCiAgICAKICAgIFEgPSBpbnQoZGF0YVswXSkgICMgUmVhZCBudW1iZXIgb2YgcXVlcmllcwogICAgcmVzdWx0cyA9IFtdCiAgICAKICAgIGZvciBpIGluIHJhbmdlKDEsIFEgKyAxKToKICAgICAgICBuLCB5ID0gbWFwKGludCwgZGF0YVtpXS5zcGxpdCgpKQogICAgICAgIHJlc3VsdHMuYXBwZW5kKGdldF9jb2xvcihuLCB5KSkKICAgIAogICAgIyBQcmludCBhbGwgcmVzdWx0cywgb25lIHBlciBsaW5lCiAgICBwcmludCgnXG4nLmpvaW4obWFwKHN0ciwgcmVzdWx0cykpKQoKIyBFbnN1cmUgdGhlIG1haW4gZnVuY3Rpb24gaXMgY2FsbGVkIHdoZW4gdGhlIHNjcmlwdCBpcyBleGVjdXRlZAppZiBfX25hbWVfXyA9PSAiX19tYWluX18iOgogICAgbWFpbigp