Chef is the event manager of his college. He has been assigned the task to manage the upcoming tech fest. There are $K$ rooms where the event can take place, and at a particular time only one event can be organized in a room for a particular time interval.
Each event coordinator has their strictly preferred room $P_i$, and if the room is already occupied he simply cancels that event.Chef wants to maximize the total number of events,and so he allows or disallows certain events in order to achieve the task . Chef is busy handling his events so the chef needs your help .
Given a list of $N$ events with their start time $S_i$,end time $E_i$ and preferred room $P_i$,you need to calculate the maximum number of events that can take place.
Note that the $i$th event wants to occupy the $p_i$ room from [$s_i$, $f_i$) .

-----Input:-----
The first line contains an integer $T$ denoting the number of test cases . Each of the next $T$ lines contains two integers $N$ and $K$ , the number of events and the number of rooms respectively . Each of the next $N$ lines contains three integers $s_i$ ,$e_i$ and $p_i$,the start time ,end time and the preferred room of ith event.

-----Output:-----
Print the maximum number of events that can take place.

-----Constraints-----
- $1 \leq T \leq 100$
- $1 \leq N \leq 10^3$
- $1 \leq  K \leq 10^5$
- $1 \leq  Si <  Ei \leq 10^9$
- $1 \leq Pi \leq K$

-----Sample Input:-----
1

4 2
1 10 1
10 20 2
15 50 2
20 30 2

-----Sample Output:-----
3

-----EXPLANATION:-----
Chef can allow events 1st ,2nd and 4th,to get the maximum 3.

Source: codechef | Difficulty: UNKNOWN_DIFFICULTY


Solutions:
import sys
from math import gcd
input = lambda : sys.stdin.readline().strip()
inp = lambda : list(map(int, sys.stdin.readline().strip().split()))
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	d = [[] for i in range(k + 1)]
	for i in range(n):
		(l, r, p) = map(int, input().split())
		d[p].append([l, r])
	ans = 0
	for i in d:
		if len(i) == 0:
			continue
		ans += 1
		t = sorted(i, key=lambda x: (x[1], x[0]))
		final = t[0][1]
		for j in range(1, len(t)):
			if t[j][0] >= final:
				ans += 1
				final = t[j][1]
	print(ans)


def solve(array):
	L = len(array)
	if L <= 1:
		return L
	array.sort()
	cnt = 1
	for i in range(1, L):
		if array[i][1] * -1 >= array[i - 1][0]:
			cnt += 1
	return cnt
for _ in range(int(input())):
	(N, K) = list(map(int, input().split()))
	rooms = [[] for i in range(K)]
	for i in range(N):
		(s, e, p) = list(map(int, input().split()))
		rooms[p - 1].append([e, -s])
	ans = 0
	for i in rooms:
		ans += solve(i)
	print(ans)


def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def printMaxActivities(s, f):
	n = len(f)
	i = 0
	cnt = 1
	for j in range(n):
		if s[j] >= f[i]:
			cnt += 1
			i = j
	return cnt
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	mat = []
	for i in range(k):
		mat.append([[], []])
	for i in range(n):
		(a, b, c) = map(int, input().split())
		mat[c - 1][0].append(a)
		mat[c - 1][1].append(b)
	ans = 0
	for i in range(k):
		if len(mat[i][0]) == 1:
			ans += 1
		elif len(mat[i][0]) > 1:
			ans += printMaxActivities(sort_list(mat[i][0], mat[i][1]), sorted(mat[i][1]))
	print(ans)


def cal(a):
	a.sort(key=lambda x: x[1])
	len1 = len(a)
	c = 1
	curr = a[0][1]
	for p in range(0, len1 - 1):
		if curr <= a[p + 1][0]:
			c += 1
			curr = a[p + 1][1]
	return c
t = int(input())
for i in range(t):
	(n, k) = list(map(int, input().split()))
	list1 = []
	a = []
	count = 0
	for j in range(n):
		(s, f, p) = list(map(int, input().split()))
		list1.append((s, f, p))
	list1.sort(key=lambda x: x[2])
	for j in range(0, n - 1):
		if list1[j][2] == list1[j + 1][2] and j != n - 2:
			a.append((list1[j][0], list1[j][1]))
		elif list1[j][2] == list1[j + 1][2] and j == n - 2:
			a.append((list1[j][0], list1[j][1]))
			a.append((list1[j + 1][0], list1[j + 1][1]))
			count += cal(a)
			a = []
		elif list1[j][2] != list1[j + 1][2] and j == n - 2:
			a.append((list1[j][0], list1[j][1]))
			count += cal(a)
			a = []
			a.append((list1[j + 1][0], list1[j + 1][1]))
			count += cal(a)
			a = []
		else:
			a.append((list1[j][0], list1[j][1]))
			count += cal(a)
			a = []
	print(count)


from sys import stdin, stdout
input = stdin.readline
print2 = stdout.write

def get_list():
	return tuple(map(int, stdin.readline().split()))
for _ in range(int(input())):
	(n, k) = map(int, stdin.readline().split())
	event = sorted([get_list() for _ in range(n)])
	occup = {}
	count = 0
	for e in event:
		if not occup.get(e[2]) or e[0] >= occup[e[2]][1]:
			occup[e[2]] = e
			count += 1
		elif e[1] < occup[e[2]][1]:
			occup[e[2]] = e
	print(count)


from collections import defaultdict
from operator import itemgetter
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	diz = defaultdict(lambda : [])
	for _ in range(n):
		array = list(map(int, input().split()))
		diz[array[-1]].append((array[0], array[1]))
	counter = 0
	for key in diz:
		diz[key].sort(key=itemgetter(0))
		last_ev = diz[key][0]
		counter += 1
		for i in range(1, len(diz[key])):
			if diz[key][i][1] < last_ev[1]:
				last_ev = diz[key][i]
			elif diz[key][i][0] >= last_ev[1]:
				counter += 1
				last_ev = diz[key][i]
	print(counter)


for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	md = {}
	for i in range(n):
		(s, e, p) = list(map(int, input().split()))
		if p in md:
			md[p].append((s, e))
		else:
			md[p] = [(s, e)]
	sm = 0
	for i in range(k):
		if i + 1 in md:
			md[i + 1].sort(key=lambda x: x[1])
			c = 1
			curr = md[i + 1][0][1]
			for x in md[i + 1]:
				if x[0] >= curr:
					curr = x[1]
					c += 1
			sm += c
	print(sm)