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
#include <bits/stdc++.h>
using namespace std;

#define fru(j,n) for(int j=0; j<(n); ++j)
#define tr(it,v) for(auto it=(v).begin(); it!=(v).end(); ++it)
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define ALL(G) (G).begin(),(G).end()

#if 0
	#define DEB printf
#else
	#define DEB(...)
#endif
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long  ull;
typedef long long LL;
typedef double D;
typedef pair<int,int> pii;
typedef vector<int> vi;

const uint inft = 2000000009;
const int mod = 1000000007;
const int MAXN = 2006;

pii P[MAXN];
uint A[MAXN];
int n,p;

ull area(){
	ull ret=0;
	fru(i,n)ret+=1LL*A[i]*A[i];
	return ret;
}

bool ssolve(ll W){
	DEB(" $$$$$      W= %lld $$$$$$$$$\n",W);
	set<pair<uint,bool> > S;
	S.insert(make_pair((uint)W,0));
	set<pair<pair<uint,int>, int> > E;
	fru(i,n)A[i]=-1;
	fru(i,n)E.insert(mp(mp(P[i].y,-P[i].x),i));
	while(!E.empty()){
		auto u=*E.begin();E.erase(E.begin());
		vi K;// elements on the same y coord
		K.pb(u.y);
		while(!E.empty() && E.begin()->x.x==u.x.x){
			K.pb(E.begin()->y);
			E.erase(E.begin());
		}
		DEB("K dla %u\n",u.x.x);
		tr(it,K)DEB("%d ",*it);DEB("\n");
		bool ins=0;
		for(auto k:K)if(A[k]==-1){
			if(S.lower_bound(mp(P[k].x,1))->y==1){
				DEB("pole %d ins\n",k);ins=1;
				break;
			}
		}
		if(ins)return false;
		vi KK;
		for(auto k:K)if(A[k]==-1){
			S.insert(mp((uint)P[k].x,0));
			uint B=S.upper_bound(mp((uint)P[k].x,0))->x;
			DEB("pole %d na  [%u,%u]\n",k,P[k].x,B-1);
			S.insert(mp(B-1,1));
			A[k]=B-P[k].x;
			E.insert(mp(mp((uint)P[k].y+A[k]-1,-P[k].x),k));
		}else KK.pb(k);
		while(!E.empty() && E.begin()->x.x==u.x.x){
			KK.pb(E.begin()->y);
			E.erase(E.begin());
		}
		DEB("KK dla %u\n",u.x.x);
		tr(it,KK)DEB("%d ",*it);DEB("\n");
		for(auto k:KK){
			DEB("usuwam %d na  [%u,%u]\n",k,P[k].x,(uint)P[k].x+A[k]-1);
			S.erase(mp(P[k].x,0));
			S.erase(mp((uint)P[k].x+A[k]-1,1));
		}
	}
	DEB("RETURN TRUE\n");
	return true;
}
void solve() {
	scanf("%d",&n);
	vi Y;
	p=0;
	uint mx=inft;
	fru(i,n){
		scanf("%d%d",&P[i].x,&P[i].y);
		mx=min(mx,(uint)P[i].x);
		Y.pb(P[i].y);
		if(P[i].y<P[p].y || (P[i].y==P[p].y && P[i].x>P[p].x))p=i;
	}
	if(n==1){
		printf("TAK 1\n");return;
	}
	DEB("P %d (%d %d)\n",p,P[p].x,P[p].y);
	if(ssolve(P[p].x+inft)){
		uint wi=0,he=0;
		fru(i,n)if(i!=p)he=max(he,P[i].y+A[i]-P[p].y);
		A[p]=he;
		fru(i,n)wi=max(wi,P[i].x+A[i]-mx);
		DEB("wi %u hi %u\n",wi,he);
		if(A[p]>0 && area()==1LL*wi*he){
			printf("TAK ");
			fru(i,n)printf("%d ",A[i]);puts("");
			return;
		}
	}
	//rest
	for(auto y: Y)if(y!=P[p].y && ssolve(P[p].x+y-P[p].y)){
		uint wi=0,he=0;
		fru(i,n)he=max(he,P[i].y+A[i]-P[p].y);
		fru(i,n)wi=max(wi,P[i].x+A[i]-mx);
		if(area()==1LL*wi*he){
			printf("TAK ");
			fru(i,n)printf("%d ",A[i]);puts("");
			return;
		}
	}
	puts("NIE");
}

int main() {
	int te = 1;
	scanf("%d",&te);
	fru(ti,te) solve();
	return 0;
}