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
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;


public class par {

	public static void main(String[] args) throws IOException {
		
		par solver = new par();
		solver.init();
		solver.solve();
	}
	
	private void init() {
		
	}
	
	private static class Car {
		int h;
		int x1;
		int x2;
		int idx;
		public Car(int h, int x1) {
			super();
			this.h = h;
			this.x1 = x1;
		}
		
	}
	
	private void solve() throws IOException { 
		Reader in = new Reader(System.in);
		PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
		
		for(int t = in.nextInt(); t > 0; t--) {
			int n = in.nextInt();
			int w = in.nextInt();
			Car[] cars = new Car[n];
			for (int i = 0; i < n; i++) {
				int x1 = in.nextInt();
				int y1 = in.nextInt();
				int x2 = in.nextInt();
				int y2 = in.nextInt();
				cars[i] = new Car(y2 - y1, x1);
			}
			for (int i = 0; i < n; i++) {
				int x1 = in.nextInt();
				int y1 = in.nextInt();
				int x2 = in.nextInt();
				int y2 = in.nextInt();
				cars[i].x2 = x1;
			}
			Arrays.sort(cars, new Comparator<Car>() {
				@Override
				public int compare(Car o1, Car o2) {
					return o1.x2 - o2.x2;
				}
				
			});
			for (int i = 0; i < n; i++) cars[i].idx = i;
			Arrays.sort(cars, new Comparator<Car>() {
				@Override
				public int compare(Car o1, Car o2) {
					return o1.x1 - o2.x1;
				}
			});
			
			if (isOk(cars,w,0,n-1)) {
				out.println("TAK");
			} else {
				out.println("NIE");
			}
		}
		
		out.flush();
		out.close();
	}
	
	private boolean isOk(Car[] cars, int w, int from, int to) {
		int len = to - from + 1;
		if (len == 1) return true;
		int half = len/2;
		Car[] left = new Car[half];
		Car[] right = new Car[len - half];
		int leftIdx = 0;
		int rightIdx = 0;
		
		int maxToRight = 0;
		int pivot = (from + to + 1)/2;
		for (int i = 0; i < len ; i++) {
			if (cars[i].idx < pivot) {
				if (cars[i].h + maxToRight > w) return false;
				left[leftIdx++] = cars[i];
			} else {
				right[rightIdx++] = cars[i];
				maxToRight = Math.max(maxToRight, cars[i].h);
			}
		}
		
		return isOk(left,w,from,pivot-1) && isOk(right,w, pivot,to);
	}

	private static class Reader {
	    BufferedReader reader;
	    StringTokenizer tokenizer;

	    /** call this method to initialize reader for InputStream */
	    Reader(InputStream input) {
	        reader = new BufferedReader(
	                     new InputStreamReader(input) );
	        tokenizer = new StringTokenizer("");
	    }

	    public void skipLine() throws IOException {
			reader.readLine();
		}

		/** get next word */
	    public String next() throws IOException {
	        while ( ! tokenizer.hasMoreTokens() ) {
	            //TODO add check for eof if necessary
	            tokenizer = new StringTokenizer(
	                   reader.readLine() );
	        }
	        return tokenizer.nextToken();
	    }

	    public int nextInt() throws IOException {
	        return Integer.parseInt( next() );
	    }
	    
	    public double nextDouble() throws IOException {
	        return Double.parseDouble( next() );
	    }
	    
	    public long nextLong() throws IOException {
	    	return Long.parseLong(next());
	    }
	}
}