/* CPP Tempelate
* @author Devashish Tyagi
*/
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <list>
#define tr(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define pi pair<int,int>
#define vi vector<int>
#define all(v) v.begin(),v.end()
#define PB push_back
#define MP make_pair
#define sz(a) (int)(a).size()
#define FOR(i,a,b) for(int (i) = (a); (i) < (b); ++(i))
#define RFOR(i,a,b) for(int (i) = (a)-1; (i) >= (b); --(i))
#define CLEAR(a) memset((a),0,sizeof(a))
#define INF 100000000
#define PI 2*acos(0.0)
using namespace std;
typedef long long ll;
string convertInt(int number)
{
stringstream ss;//create a stringstream
ss << number;//add number to the stream
return ss.str();//return a string with the contents of the stream
}
int convertString(string s){
int num;
stringstream sstr(s); // create a stringstream
sstr>>num; // push the stream into the num
return num;
}
vector<int> parent(100001,0);
vector<int> rank(100001,0);
int n;
void reset(int n){
FOR(i,0,2*n){
parent[i] = i;
rank[i] = 0;
}
}
int find(int v){
if (parent[v] == v)
return v;
return parent[v] = find(parent[v]);
}
bool add(int t, int i, int j){
if (i>=n || j>= n) return false;
if (t == 0){
int ni=i+n, nj=j+n;
int p1 = find(i), p2 = find(j), p3 = find(ni), p4 = find(nj);
if (p1 == p4)
return false;
else if (p2 == p3)
return false;
else if (p1 != p2){
if (rank[p1] < rank[p2])
swap(p1,p2);
parent[p2] = p1;
if (rank[p1] == rank[p2])
rank[p1]++;
return true;
}
else
return true;
}
else{
int ni=i+n, nj=j+n;
int p1 = find(i), p2 = find(j), p3 = find(ni), p4 = find(nj);
if (p1 == p2)
return false;
else if (p1 == p4)
return false;
else if (p2 != p3){
if (rank[p2] < rank[p3])
swap(p2,p3);
parent[p2] = p3;
if (rank[p2] == rank[p3])
rank[p2]++;
return true;
}
else
return true;
}
}
int main(void){
int t;
scanf("%d",&t);
while(t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Qgbmk9aStuLCBuaj1qK247CiAgICAgICAgaW50IHAxID0gZmluZChpKSwgcDIgPSBmaW5kKGopLCBwMyA9IGZpbmQobmkpLCBwNCA9IGZpbmQobmopOwogICAgICAgIGlmIChwMSA9PSBwNCkKICAgICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgIGVsc2UgaWYgKHAyID09IHAzKQogICAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgZWxzZSBpZiAocDEgIT0gcDIpewogICAgICAgICAgICBpZiAocmFua1twMV0gPCByYW5rW3AyXSkKICAgICAgICAgICAgICAgIHN3YXAocDEscDIpOwogICAgICAgICAgICBwYXJlbnRbcDJdID0gcDE7CiAgICAgICAgICAgIGlmIChyYW5rW3AxXSA9PSByYW5rW3AyXSkKICAgICAgICAgICAgICAgIHJhbmtbcDFdKys7CiAgICAgICAgICAgIHJldHVybiB0cnVlOwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgICAgIHJldHVybiB0cnVlOwogICAgfQogICAgZWxzZXsKICAgICAgICBpbnQgbmk9aStuLCBuaj1qK247CiAgICAgICAgaW50IHAxID0gZmluZChpKSwgcDIgPSBmaW5kKGopLCBwMyA9IGZpbmQobmkpLCBwNCA9IGZpbmQobmopOwogICAgICAgIGlmIChwMSA9PSBwMikKICAgICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgIGVsc2UgaWYgKHAxID09IHA0KQogICAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgZWxzZSBpZiAocDIgIT0gcDMpewogICAgICAgICAgICBpZiAocmFua1twMl0gPCByYW5rW3AzXSkKICAgICAgICAgICAgICAgIHN3YXAocDIscDMpOwogICAgICAgICAgICBwYXJlbnRbcDJdID0gcDM7CiAgICAgICAgICAgIGlmIChyYW5rW3AyXSA9PSByYW5rW3AzXSkKICAgICAgICAgICAgICAgIHJhbmtbcDJdKys7CiAgICAgICAgICAgIHJldHVybiB0cnVlOwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgICAgIHJldHVybiB0cnVlOwogICAgfQp9CgppbnQgbWFpbih2b2lkKXsKICAgIGludCB0OwogICAgc2NhbmYoIiVkIiwmdCk7CiAgICB3aGlsZSh0LS0pewogICAgICAgIGludCBrOwogICAgICAgIHNjYW5mKCIlZCAlZCIsJm4sJmspOwogICAgICAgIHJlc2V0KG4pOwogICAgICAgIGludCBhbnMgPSAwOwogICAgICAgIEZPUihpLDAsayl7CiAgICAgICAgICAgIGludCB0LGksajsKICAgICAgICAgICAgc2NhbmYoIiVkICVkICVkIiwmdCwmaSwmaik7IAogICAgICAgICAgICBpZiAoIWFkZCh0LTEsaS0xLGotMSkpCiAgICAgICAgICAgICAgICBhbnMrKzsKICAgICAgICB9CiAgICAgICAgcHJpbnRmKCIlZFxuIixhbnMpOwogICAgfQp9