#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
// Fonction pour calculer la taille du triangle à l'étape n
ll getSize(ll n) {
if (n == 1) return 2;
return (1LL << n) - 1;
}
// Fonction pour déterminer si un point est rouge ou bleu
bool isRed(ll x, ll y) {
// Si x ou y est hors des limites ou y > x, point invalide
if (x < 1 || y < 1 || y > x) return false;
// Cas de base : triangle de taille 2 (étape 1)
if (x <= 2) return true;
// Trouver l'étape n où ce point apparaît pour la première fois
ll n = 1;
while (getSize(n) < x) n++;
// Tant qu'on n'atteint pas l'étape 1
while (n > 1) {
ll size = getSize(n - 1);
ll totalSize = getSize(n);
// Si le point est dans le triangle bleu du milieu
if (x > size && x <= totalSize && y > (x - size - 1) && y <= (x - size - 1) + size) {
return false;
}
// Transformer les coordonnées pour le sous-triangle approprié
if (x > size) {
if (y <= (x - size - 1)) {
// Triangle gauche
x -= size;
y = y;
} else if (y > (x - size - 1) + size) {
// Triangle droit
x -= size;
y -= ((x - size - 1) + size);
} else {
// Point bleu
return false;
}
}
n--;
}
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int Q;
cin >> Q;
while (Q--) {
ll x, y;
cin >> x >> y;
cout << (isRed(x, y) ? 1 : 0) << "\n";
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdXNpbmcgbGwgPSBsb25nIGxvbmc7CgovLyBGb25jdGlvbiBwb3VyIGNhbGN1bGVyIGxhIHRhaWxsZSBkdSB0cmlhbmdsZSDDoCBsJ8OpdGFwZSBuCmxsIGdldFNpemUobGwgbikgewogICAgaWYgKG4gPT0gMSkgcmV0dXJuIDI7CiAgICByZXR1cm4gKDFMTCA8PCBuKSAtIDE7Cn0KCi8vIEZvbmN0aW9uIHBvdXIgZMOpdGVybWluZXIgc2kgdW4gcG9pbnQgZXN0IHJvdWdlIG91IGJsZXUKYm9vbCBpc1JlZChsbCB4LC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aWFuZ2xlIGdhdWNoZQogICAgICAgICAgICAgICAgeCAtPSBzaXplOwogICAgICAgICAgICAgICAgeSA9IHk7CiAgICAgICAgICAgIH0gZWxzZSBpZiAoeSA+ICh4IC0gc2l6ZSAtIDEpICsgc2l6ZSkgewogICAgICAgICAgICAgICAgLy8gVHJpYW5nbGUgZHJvaXQKICAgICAgICAgICAgICAgIHggLT0gc2l6ZTsKICAgICAgICAgICAgICAgIHkgLT0gKCh4IC0gc2l6ZSAtIDEpICsgc2l6ZSk7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAvLyBQb2ludCBibGV1CiAgICAgICAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgCiAgICAgICAgbi0tOwogICAgfQogICAgCiAgICByZXR1cm4gdHJ1ZTsKfQoKaW50IG1haW4oKSB7CiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKICAgIGNpbi50aWUobnVsbHB0cik7CiAgICAKICAgIGludCBROwogICAgY2luID4+IFE7CiAgICAKICAgIHdoaWxlIChRLS0pIHsKICAgICAgICBsbCB4LCB5OwogICAgICAgIGNpbiA+PiB4ID4+IHk7CiAgICAgICAgY291dCA8PCAoaXNSZWQoeCwgeSkgPyAxIDogMCkgPDwgIlxuIjsKICAgIH0KICAgIAogICAgcmV0dXJuIDA7Cn0=