#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
int position = y - 1;
// If the position is within the top triangle, it's red
if (position < prevLevelPoints) {
cout << 1 << endl;
} else {
// Otherwise, it's in the inverted triangle or the bottom triangle
// We can determine the color by checking if the position is odd or even
cout << (position % 2 == 0 ? 1 : 0) << endl;
}
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y21hdGg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG1haW4oKSB7CiAgICBpbnQgUTsKICAgIGNpbiA+PiBROwoKICAgIHdoaWxlIChRLS0pIHsKICAgICAgICBpbnQgeCwgeTsKICAgICAgICBjaW4gPj4geCA+PiB5OwoKICAgICAgICAvLyBEZXRlcm1pbmUgdGhlIGxldmVsIG9mIHRoZSB0cmlhbmdsZQogICAgICAgIGludCBsZXZlbCA9IDA7CiAgICAgICAgd2hpbGUgKHggPiAxKSB7CiAgICAgICAgICAgIHggLz0gMjsKICAgICAgICAgICAgbGV2ZWwrKzsKICAgICAgICB9CgogICAgICAgIC8vIENhbGN1bGF0ZSB0aGUgbnVtYmVyIG9mIHBvaW50cyBpbiB0aGUgcHJldmlvdXMgbGV2ZWwKICAgICAgICBpbnQgcHJldkxldmVsUG9pbnRzID0gcG93KDIsIGxldmVsIC0gMSkgLSAxOwoKICAgICAgICAvLyBEZXRlcm1pbmUgdGhlIHBvc2l0aW9uIG9mIHRoZSBwb2ludCB3aXRoaW4gdGhlIGxldmVsCiAgICAgICAgaW50IHBvc2l0aW9uID0geSAtIDE7CgogICAgICAgIC8vIElmIHRoZSBwb3NpdGlvbiBpcyB3aXRoaW4gdGhlIHRvcCB0cmlhbmdsZSwgaXQncyByZWQKICAgICAgICBpZiAocG9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