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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <bits/stdc++.h>

using namespace std;
#define PB push_back
#define MP make_pair
#define LL long long
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define RE(i,n) FOR(i,1,n)
#define REP(i,n) FOR(i,0,(int)(n)-1)
#define R(i,n) REP(i,n)
#define VI vector<int>
#define PII pair<int,int>
#define LD long double
#define FI first
#define SE second
#define st FI
#define nd SE
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

template<class C> void mini(C&a4, C b4) { a4 = min(a4, b4); }
template<class C> void maxi(C&a4, C b4) { a4 = max(a4, b4); }

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<endl;}
template<class TH, class...TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<",";_dbg(sdbg+1, a...);
}

template<class T> ostream& operator<<(ostream &os, vector<T> V) {
  os<<"[";for(auto vv:V)os<<vv<<","; os<<"]";
  return os;
}
template<class L, class R> ostream& operator<<(ostream &os, pair<L, R> P) {
  os<<"("<<P.first<<","<<P.second<<")";
  return os;
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#endif


#pragma GCC optimize ("tree-vectorize")

#warning
const int MaxN = (1 << 13);
const int MaxSize = (MaxN + 7) / 8;

struct Bitmask {
  unsigned data[MaxSize];
  Bitmask() {
    fill_n(data, MaxSize, 0);
  }
  
  void flip(int pos, bool bit) {
    data[pos >> 5] ^= (bit << (pos & 31));
  }

  bool get(int pos) const {
    return (data[pos >> 5] >> (pos & 31)) & 1;
  }

  bool andParity(const Bitmask &other, int size) const {
    size = (size + 31) >> 5;
    unsigned mask = 0;
    for (int i = 0; i < size; i++) {
      mask ^= data[i] & other.data[i];
    }
    return __builtin_popcount(mask) % 2;
  }
};


Bitmask dp[2][MaxN * 2];
Bitmask res[MaxN];


void preproc() {
  dp[0][0].flip(0, 1);
  dp[1][1].flip(0, 1);

  for (int n = 2; n < MaxN * 2; n++) {
    int mRange = min(n, MaxN - 1);
    for (int k = 1; k <= mRange; k++) {
      dp[0][n].flip(k, dp[0][n - 1].get(k) ^ dp[0][n - 2].get(k - 1));
    }
    for (int k = 0; k <= mRange; k++) {
      bool b = dp[1][n - 1].get(k);
      if (k) { b ^= dp[1][n - 2].get(k - 1); }
      dp[1][n].flip(k, b);

      debug(n, k, dp[0][n].get(k), dp[1][n].get(k));
    }
  }

  res[0].flip(0, 1);
  res[1].flip(1, 1);
  for (int k = 0; k < MaxN; k++) {
    if (k & 1) {
      for (int n = max(2, k); n < MaxN; n++) {
        res[k].flip(n, res[k].get(n - 1) ^ res[k - 1].get(n - 1));
        debug(n, k, res[k].get(n));
      }
    } else {
      for (int n = max(2, k); n < MaxN; n++) {
        res[k].flip(n, dp[0][n].andParity(res[k >> 1], n / 2 + 1));
        debug(n, k, res[k].get(n));
      }
    }
  }
}


int getResTab(int n, int k) {
  if (k > n) { return 0; }
  int x;
  if (n < MaxN) {
    x = res[k].get(n);
  } else {
    debug(k & 1, n, k >> 1, n / 2 + 1);
    x = dp[k & 1][n].andParity(res[k >> 1], n / 2 + 1);
  }
  debug(n, k, x);
  return x;
}

int getRes(int n, int k) {
  bool result = getResTab(n, k);

  for (int bit = 0; k % (1 << (bit + 1)) == 0; bit++) {
    result ^= getResTab(n, k | (1 << bit));
  }
  return result;
}



int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(12);
  cerr << fixed << setprecision(6);

  preproc();

  int Q;
  cin >> Q;

  for (int i = 0; i < Q; i++) {
    int n, k;
    cin >> n >> k;
    cout << getRes(n, n - k) << "\n";
  }
}