You are given a spreadsheet that contains a list of $N$ athletes and their details (such as age, height, weight and so on). You are required to sort the data based on the $K^{th}$ attribute and print the final resulting table. Follow the example given below for better understanding. 

Note that $\mbox{K}$ is indexed from $\mbox{0}$ to $M-1$, where $\mbox{M}$ is the number of attributes. 

Note: If two attributes are the same for different rows, for example, if two atheletes are of the same age, print the row that appeared first in the input.

Input Format

The first line contains $N$ and $\mbox{M}$ separated by a space. 

The next $N$ lines each contain $\mbox{M}$ elements. 

The last line contains $\mbox{K}$.     

Constraints

$1\leq N,M\leq1000$ 

$0\leq K<M$ 

Each element $\leq1000$

Output Format

Print the $N$ lines of the sorted table. Each line should contain the space separated elements. Check the sample below for clarity.   

Sample Input 0
5 3
10 2 5
7 1 0
9 9 9
1 23 12
6 5 9
1

Sample Output 0
7 1 0
10 2 5
6 5 9
9 9 9
1 23 12

Explanation 0

The details are sorted based on the second attribute, since $\mbox{K}$ is zero-indexed.

Difficulty: MEDIUM

Solutions:
(n, m) = [int(i) for i in input().split()]
narr = [0 for i in range(n)]
for i in range(n):
	narr[i] = [int(j) for j in input().split()]
k = int(input())
narr.sort(key=lambda x: int(x[k]))
for i in range(n):
	print(*narr[i], sep=' ')


import sys

def LineToIntArr(line):
	return list(map(int, line.split()))
[n, m] = LineToIntArr(sys.stdin.readline())
records = [sys.stdin.readline() for i in range(n)]
[K] = LineToIntArr(sys.stdin.readline())
vals = map(lambda l: LineToIntArr(l)[K], records)
for (v, k) in sorted(zip(vals, range(0, n))):
	sys.stdout.write(records[k])


(N, M) = input().split()
N = int(N)
M = int(M)
table = []
for i in range(N):
	table += [[int(x) for x in input().split()]]
K = int(input())
table.sort(key=lambda x: x[K])
for i in range(N):
	print(' '.join(map(str, table[i])))


(n, m) = map(int, input().split())
l = []
for i in range(n):
	line = input()
	l.append(list(map(int, line.split())))
k = int(input())
newl = sorted(l, key=lambda x: x[k])
for i in newl:
	print(' '.join(map(str, i)))


import sys

def array_to_i(a):
	return [int(x) for x in a]
(N, M) = array_to_i(sys.stdin.readline().split())
elements = [array_to_i(sys.stdin.readline().split()) for _ in range(N)]
K = int(input())
for line in sorted(elements, key=lambda x: x[K]):
	print(' '.join((str(x) for x in line)))


(n, m) = input().split()
n = int(n)
m = int(m)
data = []
i = 0
while i < n:
	data.append([int(i) for i in input().split()])
	i += 1
k = input()
k = int(k)
s = sorted(data, key=lambda row: row[k])
for line in s:
	print(' '.join(map(str, line)))


import sys
stdin = sys.stdin
(n, m) = map(int, stdin.readline().split())
rows = []
for i in range(n):
	rows.append(list(map(int, stdin.readline().split())))
col = int(stdin.readline())
for row in sorted(rows, key=lambda x: int(x[col])):
	print(' '.join(map(str, row)))


import sys
file = sys.stdin
(N, M) = (int(x) for x in file.readline().split())
data = [file.readline().split() for i in range(N)]
K = int(file.readline())
for row in sorted(data, key=lambda x: int(x[K])):
	print(*row)


[N, M] = map(int, input().split())
A = []
for i in range(N):
	A.append([int(a) for a in input().split()])
K = int(input())
B = sorted(A, key=lambda a: a[K])
for i in range(N):
	print(' '.join(map(str, B[i])))


(N, M) = [int(x) for x in input().split()]
data = []
for _ in range(N):
	data.append([int(x) for x in input().split()])
K = int(input())
data.sort(key=lambda row: row[K])
for row in data:
	print(' '.join([str(x) for x in row]))


i = input()
l = i.split(' ')
N = int(l[0])
M = int(l[1])
b = list()

def printList(li):
	for im in li:
		print(' '.join((str(x) for x in im)))

def bubble(a):
	for i in range(len(a) - 1, 0, -1):
		for j in range(0, i):
			if a[j][k] > a[j + 1][k]:
				temp = a[j]
				a[j] = a[j + 1]
				a[j + 1] = temp
for y in range(N):
	r = input().split(' ')
	for g in range(M):
		r[g] = int(r[g])
	b.append(r)
k = int(input())
my_list = list()
bubble(b)
printList(b)


first_line = input()
(n, m) = first_line.strip().split(' ')
(n, m) = (int(n), int(m))
group = []
for _ in range(n):
	line = input()
	elements = [int(e) for e in line.strip().split(' ')]
	group.append(elements)
k = int(input().strip())
group = sorted(group, key=lambda e: e[k])
last_print = None
for elements in group:
	print(' '.join([str(e) for e in elements]))


def bubbleSort(itemList, K):
	swapping = True
	while swapping:
		swapping = False
		for i in range(len(itemList) - 1):
			if itemList[i][K] > itemList[i + 1][K]:
				temp = itemList[i]
				itemList[i] = itemList[i + 1]
				itemList[i + 1] = temp
				swapping = True
	return itemList
first = input().split(' ')
N = int(first[0])
M = int(first[1])
items = []
for i in range(N):
	items.append(list(input().split(' ')))
	for t in range(M):
		items[-1][t] = int(items[-1][t])
K = int(input())
sortedItems = bubbleSort(items, K)
for s in sortedItems:
	for t in range(M):
		print(str(s[t]), end='')
		if t != M - 1:
			print(' ', end='')
	print()


import sys
rows = sys.stdin.read().split('\n')
(N, M) = map(int, rows[0].split(' '))
K = int(rows[N + 1])
tableRows = [row.split(' ') for row in rows[1:N + 1]]
sortedRows = sorted(tableRows, key=lambda row: int(row[K]))
for row in sortedRows:
	sys.stdout.write(' '.join(row))
	sys.stdout.write('\n')


rows = []
rowCount = int(input().split(' ')[0])
for i in range(rowCount):
	rows.append(input().split(' '))
n = int(input())
rows.sort(key=lambda x: int(x[n]))
for i in rows:
	print(' '.join(i))


import string
from operator import itemgetter

def fun():
	line = input().split()
	tc = int(line[0])
	l = []
	for i in range(tc):
		j = [int(m) for m in input().split()]
		l.append(j)
	k = int(input())
	s = sorted(l, key=itemgetter(k))
	for row in s:
		print(' '.join((str(x) for x in row)))
fun()


(N, M) = map(int, input().split())
data = []
for _ in range(N):
	data.append(list(map(int, input().split())))
K = int(input())
data.sort(key=lambda x: x[K])
for row in data:
	print(' '.join((str(i) for i in row)))


items = list()
line = input()
while True:
	line = input().split()
	if len(line) == 1:
		key = int(line[0])
		break
	items.append([int(x) for x in line])
items = sorted(items, key=lambda x: x[key])
for item in items:
	item = [str(x) for x in item]
	print(' '.join(item))


N = list(input().split(' '))
M = int(N[1])
N = int(N[0])
L = []
for i in range(N):
	x = list(input().split(' '))
	x = list(map(lambda y: int(y), x))
	L.append(x)
K = int(input())
L.sort(key=lambda y: y[K])
for i in L:
	i = list(map(lambda y: str(y), i))
	x = ' '.join(i)
	print(x)


(n, m) = map(int, input().split(' '))
data = [list(map(int, input().split(' '))) for _ in range(n)]
k = int(input())
data = sorted(data, key=lambda x: x[k])
for d in data:
	print(' '.join(map(str, d)))


(n, m) = map(int, input().split())
lst = [[int(x) for x in input().split()] for _ in range(n)]
k = int(input())
for i in range(n):
	lst[i] = [lst[i][k], i] + lst[i]
for row in sorted(lst):
	print(' '.join((str(row[i]) for i in range(2, len(row)))))


(n, m) = input().strip().split()
(n, m) = (int(n), int(m))
data = []
for case in range(0, n):
	a = list(map(int, input().strip().split()))
	data.append(a)
k = int(input().strip())
out = sorted(data, key=lambda x: x[k])
for a in out:
	print(' '.join(map(str, a)))


import sys
(entries, entry_size) = [int(inp) for inp in sys.stdin.readline().rstrip().split(' ')]
table = list()
for _ in range(entries):
	table.append([int(inp) for inp in sys.stdin.readline().rstrip().split(' ')])
target_attr = int(sys.stdin.readline().rstrip())
sorted_table = sorted(table, key=lambda entry: entry[target_attr])
for line_list in sorted_table:
	print(' '.join([str(inp) for inp in line_list]))


import sys
raw_input = sys.stdin.readline
reader = lambda : list(map(int, raw_input().split(' ')))
(y, x) = reader()
data = [reader() for _ in range(y)]
(nth,) = reader()
for r in sorted(data, key=lambda x: x[nth]):
	print(' '.join((str(x) for x in r)))


(N, M) = (int(x) for x in input().split())
data = []
for _ in range(N):
	data.append([int(x) for x in input().split()])
K = int(input())
data.sort(key=lambda item: item[K])
for line in data:
	print(' '.join((str(x) for x in line)))


def sortData(g, K):
	for i in range(1, len(g)):
		j = i - 1
		while j >= 0 and int(g[j][K]) > int(g[j + 1][K]):
			temp = g[j]
			g[j] = g[j + 1]
			g[j + 1] = temp
			j -= 1

def main():
	grid = []
	lengths = input().split()
	N = int(lengths[0])
	M = int(lengths[1])
	for i in range(N):
		row = input().split()
		grid.append(row)
	K = int(input())
	sortData(grid, K)
	for a in range(N):
		print(' '.join(grid[a]))
main()


(N, M) = list(map(int, input().split(' ')))
Data = [list(map(int, input().split(' '))) for _ in range(N)]
K = int(input())
Data.sort(key=lambda x: x[K])
for d in Data:
	print(' '.join(map(str, d)))


(i, k) = map(int, input().split())
g = [list(map(int, input().split())) for _ in range(i)]
n = int(input())
g.sort(key=lambda x: x[n])
for x in g:
	print(' '.join(map(str, x)))


import readline
(N, M) = input().split()
(N, M) = (int(N), int(M))
myList = []
for i in range(0, N):
	data = [int(x) for x in input().split()]
	myList.append(data)
K = int(input())
myList.sort(key=lambda x: x[K])
for l in myList:
	line = ' '.join(map(str, l))
	print(line)


line = input().split(' ')
rows = int(line[0])
cols = int(line[1])
table = []
for i in range(rows):
	line = input().split(' ')
	table.append([int(x) for x in line])
sort_key = int(input())
table.sort(key=lambda x: x[sort_key])
for row in table:
	print(' '.join((str(x) for x in row)))


__author__ = 'woboz'
from operator import itemgetter
(tests, ele) = map(int, input().split())
lis = []
for i in range(tests):
	lis.append(list(map(int, input().split())))
colsort = int(input())
lis.sort(key=itemgetter(colsort))
for each in lis:
	each = list(map(str, each))
	print(' '.join(each))


__author__ = 'srikanth'
from operator import itemgetter
z = tuple((int(x.strip()) for x in input().split(' ')))
data = []
for i in range(int(z[0])):
	data.append(tuple((int(x.strip()) for x in input().split(' '))))
k = int(input())
xx = sorted(data, key=itemgetter(k))
for x in xx:
	print(str(x).replace('(', '').replace(')', '').replace(',', ''))


import operator

def readline():
	return [int(x) for x in input().strip().split()]
(n, m) = readline()
data = []
for _ in range(int(n)):
	data.append(readline())
k = readline()[0]
data.sort(key=operator.itemgetter(k))
for sample in data:
	print(' '.join((str(x) for x in sample)))


(N, M) = map(int, input().split())
T = []
for _ in range(N):
	T.append(list(map(int, input().split())))
K = int(input())
T = sorted(T, key=lambda x: x[K])
for i in T:
	print(' '.join(list(map(str, i))))


import sys
(n, m) = list(map(int, sys.stdin.readline().split()))
numlist = []
for _ in range(n):
	nums = list(map(int, sys.stdin.readline().split()))
	numlist.append(nums)
k = int(sys.stdin.readline())
numlist = sorted(numlist, key=lambda elem: elem[k])
for num in numlist:
	print(' '.join(map(str, num)))


(N, M) = (int(i) for i in input().split())
arr = [tuple((int(i) for i in input().split())) for _ in range(N)]
K = int(input())
arr.sort(key=lambda x: x[K])
for _ in range(N):
	print(*arr[_])


(n, m) = map(int, input().split())
a = []
for r in range(0, n):
	a.append(list(map(int, input().split())))
index = int(input())
b = sorted(a, key=lambda student: student[index])
for x in b:
	print(str(x).replace('[', '').replace(',', '').replace(']', ''))


(N, M) = map(int, input().split())
data = [list(map(int, input().split())) for i in range(N)]
K = int(input())
data.sort(key=lambda x: x[K])
print('\n'.join([' '.join([str(data[i][j]) for j in range(M)]) for i in range(N)]))


import sys
import operator
(n, m) = map(int, sys.stdin.readline().split())
data = []
for i in range(n):
	row = tuple(map(int, sys.stdin.readline().split()))
	data.append(row)
k = int(sys.stdin.readline())
data.sort(key=operator.itemgetter(k))
print('\n'.join((' '.join(map(str, row)) for row in data)))


arr = []
(n, m) = map(int, input().split())
for _ in range(n):
	arr.append(list(map(int, input().split())))
k = int(input())
arr.sort(key=lambda x: x[k])
for _ in arr:
	print(' '.join(list(map(str, _))))


from operator import itemgetter
(N, M) = [int(i) for i in input().split(' ')]
items = [[int(j) for j in input().split(' ')] for _ in range(0, N)]
K = int(input())
print('\n'.join((' '.join((str(j) for j in i)) for i in sorted(items, key=itemgetter(K)))))


(N, M) = [int(x) for x in input().split()]
rows = []
for _ in range(N):
	rows.append([int(x) for x in input().split()])
K = int(input())
for row in sorted(rows, key=lambda x: x[K]):
	print(' '.join([str(x) for x in row]))


t = input().split()
n = int(t[0])
m = int(t[1])
mat = [[0 for x in range(m)] for x in range(n)]
for i in range(n):
	t = input().split()
	t = [int(x) for x in t]
	for j in range(m):
		mat[i][j] = t[j]
k = int(input())
temp = []
for i in range(n):
	temp.append(mat[i][k])
arr = [i[0] for i in sorted(enumerate(temp), key=lambda x: x[1])]
for x in arr:
	ps = mat[x]
	str1 = ''
	str1 = ' '.join((str(e) for e in ps))
	print(str1)


from operator import itemgetter

def ksort(lst, k):
	l = sorted(lst, key=itemgetter(k))
	for args in l:
		print(*args)
(N, M) = input().split()
(N, M) = (int(N), int(M))
arr = []
for i in range(0, N):
	arr.append(list(map(int, input().split())))
K = int(input())
ksort(arr, K)


def getkey(lisx):
	return lisx[I]
(N, M) = input('').split(' ')
N = int(N)
M = int(M)
superl = []
for i in range(N):
	inputl = input('')
	lis = inputl.rsplit()
	for j in range(len(lis)):
		lis[j] = int(lis[j])
	superl.append(lis)
I = int(input(''))
X = sorted(superl, key=getkey)
for i in X:
	for j in range(len(i)):
		i[j] = str(i[j])
	printitem = ' '.join(i)
	print(printitem)


import sys
from operator import itemgetter
firstline = input().split()
N = int(firstline[0])
M = int(firstline[1])
table = []
data = sys.stdin.readlines()
for n in range(N):
	d = data[n]
	attrs = d.strip().split()
	table.append([int(a) for a in attrs])
K = int(data[N][0])
result = sorted(table, key=itemgetter(K))
for r in result:
	print(' '.join([str(re) for re in r]))


def order_map(list, K):
	return_list = []
	for i in range(len(list)):
		if i == 0:
			return_list.append(list[i])
		else:
			inserted = False
			for k in range(len(return_list)):
				if list[i][K] < return_list[k][K]:
					return_list.insert(k, list[i])
					inserted = True
					break
			if not inserted:
				return_list.append(list[i])
	return return_list
(N, M) = list(map(int, input().split()))
map_list = []
for _ in range(0, N):
	n = list(map(int, input().split()))
	map_list.append(n)
K = int(input())
new_map = order_map(map_list, K)
for n in new_map:
	print(' '.join((str(x) for x in n)))


stringinput = input().split()
M = int(stringinput[0])
N = int(stringinput[1])
totalrows = []
total = []
for i in range(0, M):
	totalrows = list(map(int, input().split()))
	total.append(totalrows)
K = int(input())
temp = []
for i in range(0, M - 1):
	for j in range(0, M - 1 - i):
		if total[j][K] > total[j + 1][K]:
			temp = total[j]
			total[j] = total[j + 1]
			total[j + 1] = temp
for i in range(0, M):
	print(*total[i], sep=' ')


(count, _) = input().split()
count = int(count)
data = []
for i in range(count):
	numbers = input().split()
	data.append(numbers)
index = int(input())
for i in sorted(data, key=lambda item: int(item[index])):
	i[index] = str(i[index])
	print(' '.join(i))


from sys import stdin
(n, s) = map(int, input().split())
ar = []
for ln in range(n):
	tup = tuple(map(int, input().split()))
	ar.append(tup)
k = int(input())
for t in sorted(ar, key=lambda x: x[k]):
	for e in range(s):
		if e == s - 1:
			print(t[e])
		else:
			print(t[e], end=' ')


(n, m) = map(int, input().split())
a = [list(map(int, input().split())) for i in range(n)]
(k,) = map(int, input().split())
a.sort(key=lambda x: x[k])
print('\n'.join([' '.join([str(x) for x in y]) for y in a]))


line = input().split()
number = int(line[0])
attributes = int(line[1])
matrix = [[0] * attributes for n in range(number)]
for row in range(number):
	line = input().split()
	for col in range(attributes):
		matrix[row][col] = int(line[col])
sort_id = int(input())
matrix = sorted(matrix, key=lambda x: x[sort_id])
print('\n'.join([' '.join(['{:1}'.format(attribute) for attribute in row]) for row in matrix]))


class El:
	K = 0

	def __init__(self, a):
		self.a = a

	def __repr__(self):
		return ' '.join(map(str, self.a))

	def __lt__(self, other):
		return self.a[K] < other.a[K]
(N, M) = list(map(int, input().split()))
a = []
for i in range(N):
	el = El(list(map(int, input().split())))
	a.append(el)
K = int(input())
El.K = K
a = sorted(a)
for el in a:
	print(el)


import sys

def __starting_point():
	(n, m) = input().strip().split(' ')
	(n, m) = [int(n), int(m)]
	arr = []
	for arr_i in range(n):
		arr_t = [int(arr_temp) for arr_temp in input().strip().split(' ')]
		arr.append(arr_t)
	k = int(input().strip())
	for el in sorted(arr, key=lambda x: x[k]):
		print(' '.join(map(str, el)))
__starting_point()


a = []
for _ in range(int(input().split()[0])):
	a.append(list(map(int, input().split())))
b = int(input())
a.sort(key=lambda x: x[b], reverse=False)
for c in a:
	print(' '.join([str(e) for e in c]))


import operator
(n, _) = map(int, input().split())
r = []
for _ in range(n):
	r.append(list(map(int, input().split())))
k = int(input())
r.sort(key=operator.itemgetter(k))
for i in r:
	print(' '.join(map(str, i)))


from sys import stdin

def sort_kth_column():
	(n, m) = map(int, input().split())
	matrix = []
	for times in range(n):
		row = list(map(int, input().split()))
		matrix.append(row)
	k = int(input().strip())
	return sorted(matrix, key=lambda element: element[k])
for row in sort_kth_column():
	print(*row)


(h, w) = (int(x) for x in input().split())
data = [[int(x) for x in input().split()] for y in range(h)]
key = int(input())
data.sort(key=lambda row: row[key])
for row in data:
	print(' '.join((str(x) for x in row)))


(n, m) = map(int, input().split())
lis1 = []
for i in range(0, n):
	x = input()
	x1 = x.split()
	x2 = list(map(int, x1))
	lis1.append(x2)
k = int(input())
lis1.sort(key=lambda x: x[k])
for i in lis1:
	str1 = ' '.join((str(e) for e in i))
	print(str1)