#include <iostream>
#include <cmath>
using namespace std;
int main() {
int Q;
cin >> Q;
while (Q--) {
int x, y;
cin >> x >> y;
// Determine the level of the triangle
int level = 0;
while (x > 1) {
x /= 2;
level++;
}
// Calculate the number of points in the previous level
int prevLevelPoints = pow(2, level - 1) - 1;
// Determine the position of the point within the level, starting from 0
int position = y - 1;
// Determine the color based on the position within the level
if (position < prevLevelPoints) {
cout << 1 << endl; // Red
} else {
// Check if the position is in the inverted triangle or the bottom triangle
int invertedTrianglePoints = prevLevelPoints;
if (position < invertedTrianglePoints + prevLevelPoints + 1) {
cout << 0 << endl; // Blue
} else {
// Recursively check the color of the corresponding point in the previous level
x /= 2;
y = (position - invertedTrianglePoints - 1) / 2 + 1;
level--;
}
}
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y21hdGg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG1haW4oKSB7CiAgICBpbnQgUTsKICAgIGNpbiA+PiBROwoKICAgIHdoaWxlIChRLS0pIHsKICAgICAgICBpbnQgeCwgeTsKICAgICAgICBjaW4gPj4geCA+PiB5OwoKICAgICAgICAvLyBEZXRlcm1pbmUgdGhlIGxldmVsIG9mIHRoZSB0cmlhbmdsZQogICAgICAgIGludCBsZXZlbCA9IDA7CiAgICAgICAgd2hpbGUgKHggPiAxKSB7CiAgICAgICAgICAgIHggLz0gMjsKICAgICAgICAgICAgbGV2ZWwrKzsKICAgICAgICB9CgogICAgICAgIC8vIENhbGN1bGF0ZSB0aGUgbnVtYmVyIG9mIHBvaW50cyBpbiB0aGUgcHJldmlvdXMgbGV2ZWwKICAgICAgICBpbnQgcHJldkxldmVsUG9pbnRzID0gcG93KDIsIGxldmVsIC0gMSkgLSAxOwoKICAgICAgICAvLyBEZXRlcm1pbmUgdGhlIHBvc2l0aW9uIG9mIHRoZSBwb2ludCB3aXRoaW4gdGhlIGxldmVsLCBzdGFydGluZyBmcm9tIDAKICAgICAgICBpbnQgcG9zaX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