#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 (level == 1) {
            cout << 1 << endl; // Base case: top point is always red
        } else 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;
}