#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;
}