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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
//package pa;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Scanner;

public class lus
{
	public static void main(String[] args)
	{
		runalg();
	}
	
	private static void runalg()
	{
		if (fromFile)
		{
			setInFromFile();
		}
		else
		{
			setInFromSystemIn();
		}
		body();
	}
	private static final boolean debug = false;
	
	private static void debugln(String msg)
	{
		if (debug)
		{
			System.out.println(msg);
		}
	}
	
	private static void debug(String msg)
	{
		if (debug)
		{
			System.out.print(msg);
		}
	}
	private static final boolean fromFile = false;
	
	private static void setInFromFile()
	{
		File plik = new File(fileName);
		try
		{
			in = new Scanner(plik);
		}
		catch(IOException e)
		{
			System.exit(1);
		}
	}
	
	private static Scanner in;
	private static final String fileName = "lus.txt";
	
	private static void setInFromSystemIn()
	{
		in = new Scanner(System.in);
	}
	
	private static void body()
	{
		int tests = in.nextInt();
		for (int i = 1; i <= tests; i++)
		{
			lus test = readTest();
			bool solution = test.solve();
			System.out.println(solution);
		}
	}
	
	private static lus readTest()
	{
		int corps = in.nextInt();
		lus result = new lus(corps);
		return result;
	}
	
	private lus(int corps)
	{
		this.corps = readCorps(corps);
	}
	
	private List<corp> corps;
	
	private List<corp> readCorps(int corps)
	{
		List<corp> result = new ArrayList<corp>();
		for (int i = 1; i <= corps; i++)
		{
			corp corp = new corp();
			result.add(corp);
		}
		return result;
	}
	
	private class corp
	{
		public final int minH, maxH;
		public final int minW, maxW;
		
		public corp()
		{
			minW = in.nextInt();
			maxW = in.nextInt();
			minH = in.nextInt();
			maxH = in.nextInt();
		}
		
		public String toString()
		{
			String result = "(";
			result += "minW="+minW+", ";
			result += "maxW="+maxW+", ";
			result += "minH="+minH+", ";
			result += "maxH="+maxH;
			result += ")";
			return result;
		}
		
		public boolean better(corp other)
		{
			List<Integer[]> myDimensions = dimensions();
			writeArray("myDimensions", myDimensions);
			List<Integer[]> otherDimensions = other.dimensions();
			writeArray("otherDimensions", otherDimensions);
			List<Integer[]> onlyMy = diff(myDimensions, otherDimensions);
			writeArray("onlyMy", onlyMy);
			boolean result = false;
			for (Integer[] i : otherDimensions)
			{
				boolean underResult = onlyMy.contains(i);
				if (!underResult)
				{
					result = true;
					break;
				}
			}
			debug("result="+result);
			return result;			
		}
		
		public List<Integer[]> dimensions()
		{
			List<Integer[]> result = new ArrayList<Integer[]>();
			for (int i = minW; i <= maxW; i++)
			{
				for (int j = minH; j <= maxH; j++)
				{
					Integer[] pair = new Integer[2];
					pair[0] = i;
					pair[1] = j;
					result.add(pair);
				}
			}
			return result;
		}
		
	}
	
	private static <K> int sumObject(List<K> list, K obj)
	{
		int result = 0;
		for (K curr : list)
		{
			if (curr.equals(obj))
			{
				result ++;
			}
		}
		return result;
	}
	
	private static List<Integer[]> diff(List<Integer[]> list1, List<Integer[]> list2)
	{
		List<Integer[]> result = new ArrayList<Integer[]>();
		for (Integer[] i : list1)
		{
			boolean found = false;
			for (Integer[] j : list2)
			{
				boolean contains = Arrays.equals(i, j);
				if (contains)
				{
					found = true;
					break;
				}
			}
			if (!found)
			{
				result.add(i);
			}
		}
		return result;
	}
	
	
	
	private static void writeArray(String opis, List<Integer[]> list)
	{
		debug(opis+" ");
		for (Integer[] i : list)
		{
			String s = Arrays.toString(i);
			debug(s);
		}
		debugln("");
	}
	
	
	private bool solve()
	{
		List<corp> best = new ArrayList<corp>();
		for (int i = 0; i < corps.size()-1; i++)
		{
			corp a = corps.get(i);
			for (int j = i+1; j < corps.size(); j++)
			{
				corp b = corps.get(j);
				if (a.better(b))
				{
					debug("better("+a+", "+b+")="+a);
					best.add(a);
				}
				else
				{
					debug("better("+a+", "+b+")="+b);
					best.add(b);
				}
			}
		}
		debug(best+"");
		List<corp> uniqueCorps = unique(best);
		List<Integer> max = new ArrayList<Integer>();
		for (corp corp : uniqueCorps)
		{
			int sum = sumObject(best, corp);
			max.add(sum);
		}
		int walk = corps.size()-1;
		//System.out.println(walk);
		int maxSum = Collections.max(max);
		if (maxSum >= walk)
		{
			return bool.TAK;
		}
		return bool.NIE;
	}
	
	private static <K> List<K> unique(List<K> list)
	{
		List<K> result = new ArrayList<K>();
		for (K obj : list)
		{
			boolean contains = result.contains(obj);
			if (!contains)
			{
				result.add(obj);
			}
		}
		return result;
	}
	private enum bool
	{
		TAK, NIE;
	}
	
	public class Para<T1, T2> implements Entry<T1, T2>, Comparable<Entry<T1, T2>>
	{
		private T1 pierwsza;
		private T2 druga;
		private Comparator<T1> porownywaczKluczy;
		private Comparator<T2> porownywaczWartosci;
		
		public Para(T1 klucz)
		{
			pierwsza = klucz;
		}
		
		public Para(T1 klucz, T2 wartosc)
		{
			pierwsza = klucz;
			druga = wartosc;
		}

		public Para(Comparator<T1> porownywaczKluczy,  Comparator<T2> porownywaczWartosci)
		{
			this.porownywaczKluczy = porownywaczKluczy;
			this.porownywaczWartosci = porownywaczWartosci;
		}
		
		public Para()
		{
		}

		@Override
		public T1 getKey() {
			return pierwsza;
		}

		@Override
		public T2 getValue() {
			return druga;
		}

		@Override
		public T2 setValue(T2 value) 
		{
			T2 poprzedniaWartosc = druga;
			druga = value;
			return poprzedniaWartosc;
		}
		
		public void setKey(T1 key) 
		{
			pierwsza = key;
		}
		
		public String toString()
		{
			return "( "+pierwsza+" , "+druga+" )";
		}

		@Override
		public int compareTo(Entry<T1, T2> o)
		{
			T1 klucz = o.getKey();
			T2 wartosc = o.getValue();
			if (klucz == pierwsza && wartosc == druga)
			{
				return 0;
			}
			else if (porownywaczKluczy.compare(pierwsza, klucz) > 0 && porownywaczWartosci.compare(druga, wartosc) > 0)
			{
				return 1;
			}
			else
			{
				return -1;
			}
		}

	}
}