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
#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 ALL(G) (G).begin(),(G).end()

//#define DEBUG
#ifdef DEBUG
#define DEB printf
#else
#define DEB(...)
#endif

typedef double D;
typedef long long LL;
typedef pair<int,short> pis;
typedef pair<int,int> pii;
typedef vector<int> vi;

const int inf = 1000000007;
const int MAXN = 1000006;//10^6

int n,moze_byc_zerem;
pii P[MAXN];
int ANS[MAXN];
int dol,lewo;

typedef pair<int,pis> triple;

priority_queue<triple> Q;
set<pii> S;

inline pis dokladam(int i){return pis(P[i].x,i);}
inline pis usuwam  (int i){return pis(P[i].x+inf,i);}

bool dasie(int prawo){
	 fru(i,n) ANS[i]=-1;
	 while(!Q.empty()) Q.pop();
	 S.clear();
	 S.insert(pii(prawo,prawo+5));
	 fru(i,n) Q.push(triple(-P[i].y,dokladam(i)));
	 int suma=0,dlugosc=prawo-lewo;
	 int maxwys=-1;
	 fru(i,n) maxwys=max(maxwys,P[i].y+1);
	 DEB("prawo = %d\n",prawo);
	 while(!Q.empty()){
		  triple p=Q.top();
		  Q.pop();
		  int nr=p.y.y;
		  int xx=P[nr].x;
		  DEB("%d na wys %d i x = %d\n",nr,-p.x,P[nr].x);
		  if(p.y.x>=inf){ //zdejmuje
				S.erase(pii(xx,5));
				S.erase(pii(xx+ANS[nr],-5));
				suma-=ANS[nr];
				assert(suma>=0);
				if(suma!=dlugosc && -p.x!=maxwys && (Q.empty() || Q.top().x!=p.x)) {
					 DEB("0 bo bo zdjeciu jest zle\n");
					 return 0;
				}
		  }
		  else{ //dodaje
				pii s(xx,-1);
				pii ne=*(S.upper_bound(s));
				if(ne.y==-5) {
					 DEB("zero bo dotykam konca a nie poczatku\n");
					 return 0;
				}
				ANS[nr]=ne.x-xx;
				DEB("ustawiam ANS[%d] = %d\n",nr,ANS[nr]);
				maxwys=max(maxwys,-p.x+ANS[nr]);
				suma+=ANS[nr];
				if(ANS[nr]==0){
					 if(nr!=moze_byc_zerem){
						  DEB("zerem jest %d\n",nr);
						  return 0;
					 }
				}
				else{
					 S.insert(pii(xx,5));
					 S.insert(pii(xx+ANS[nr],-5));
					 DEB("dorzucam %d %d %d\n",P[nr].y+ANS[nr],usuwam(nr).x,usuwam(nr).y);
					 Q.push(triple(-(P[nr].y+ANS[nr]),usuwam(nr)));
				}
				assert(suma<=dlugosc);
				if(suma!=dlugosc && (Q.empty() || Q.top().x!=p.x)) {
					 DEB("przerywam bo po dodawaniu jest zle\n");
					 return 0;
				}
		  }
//		  printf("set:"); tr(it,S) printf("(%d,%d)",it->x,it->y); printf("\n\n");
	 }
	 if(ANS[moze_byc_zerem]==0){
		  ANS[moze_byc_zerem]=maxwys-dol;
	 }
	 DEB("udalo sie dla prawo = %d\n",prawo);
	 return 1;
}

bool solve(){
	 scanf("%d",&n);
	 fru(i,n) scanf("%d%d",&P[i].x,&P[i].y);
	 if(n==1){
		  ANS[0]=42;
		  return 1;
	 }
	 moze_byc_zerem=-1;
	 pii ld(inf,inf);
	 fru(i,n) ld=min(ld,P[i]);
	 fru(i,n) if(P[i].y<ld.y) return 0;
	 dol=ld.y;
	 lewo=ld.x;
	 int pd=-1;
	 fru(i,n) if(P[i].y==dol) if(pd==-1 || P[i].x>P[pd].x) pd=i;
	 moze_byc_zerem=pd;
	 set<int> cands;
	 fru(i,n) cands.insert(P[i].y-dol);
	 tr(it,cands) if(dasie(P[pd].x+*it)) return 1;
	 return 0;
}

int main(){
	 int te;
	 scanf("%d",&te);
	 while(te--) if(solve()){
		  printf("TAK");
		  fru(i,n) printf(" %d",ANS[i]);
		  printf("\n");
	 }
	 else printf("NIE\n");


	 return 0;
}