1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <cmath>
#include <cstring>
#include <string>
#include <iostream>
#include <complex>
#include <sstream>
#include <cassert>
using namespace std;
 
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef vector<int> VI;
typedef pair<int,int> PII;
 
#define REP(i,n) for(int i=0;i<(n);++i)
#define SIZE(c) ((int)((c).size()))
#define FOR(i,a,b) for (int i=(a); i<(b); ++i)
#define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i)
#define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i)
#define ALL(v) (v).begin(), (v).end()
 
#define pb push_back
#define mp make_pair
#define st first
#define nd second

int _msb[20000];
vector<int> reduced_weights_for_p2[20000];
void init() {
    _msb[1] = 1;
    FOR(i,2,20000) _msb[i] = (i&(i-1)) ? _msb[i-1] : i;

    reduced_weights_for_p2[1].pb(1);
    for (int p2 = 2; p2 < 20000; p2 <<= 1) {
        map<int, int> tmp;
        for (auto& g: reduced_weights_for_p2[p2 / 2]) {
            ++tmp[g];
            ++tmp[2*g];
        }

        for (auto& p: tmp) {
            if (p.nd % 2) reduced_weights_for_p2[p2].pb(p.st);
        }
    }
}

bool is_odd_graph[20000][20000];
bool _computed[20000];
void compute(int N) {
    if (_computed[N]) return;
    _computed[N] = true;
    
    int msb = _msb[N];
    if (msb == N) {
        for (auto& g: reduced_weights_for_p2[N]) is_odd_graph[N][g] = true;
        return;
    }

    int rec = N - msb;
    compute(rec);
    
    FOR(i, 1, rec+1) {
        if (!is_odd_graph[rec][i]) continue;
        for (auto& b: reduced_weights_for_p2[msb]) {
            int graph = i;
            int new_vertex = b; // is a bit
            while (true) {
                is_odd_graph[N][graph|new_vertex] ^= 1;
                if (graph&new_vertex) {
                    graph -= new_vertex;
                    new_vertex <<= 1;
                } else {
                    break;
                }
            }                    
        }
    }
}

int query(int N, int K) {
    K = N-K;
    compute(N);
    int res = is_odd_graph[N][K];
    for (int lb = 1; !(lb&K); lb <<= 1) res ^= is_odd_graph[N][K + lb]; // can drop the lowest bit
    return res;
}

int main() {
    ios_base::sync_with_stdio(0);
    init();

    int Q;
    scanf("%d", &Q);
    REP(q,Q) {
        int N, K;
        scanf("%d%d", &N, &K);
        printf("%d\n", query(N, K));       
     }
}