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.

Source: hackerrank | 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())
a = []
while n:
	t = tuple(map(int, input().split()))
	a.append(t)
	n = n - 1
k = int(input())
a.sort(key=lambda tup: tup[k])
for t in a:
	for i in t:
		print(i, end=' ')
	print()


(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)))


(N, M) = (int(x) for x in input().split())
A = [[int(x) for x in input().split()] for i in range(N)]
K = int(input())
A = sorted(A, key=lambda t: t[K])
for l in A:
	for i in l:
		print(i, end=' ')
	print()


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)


(n, m) = map(int, input().split())
a = []
for i in range(n):
	c = list(map(int, input().split()))
	a.append(c)
p = int(input())
a = sorted(a, key=lambda x: x[p])
for i in a:
	for j in i:
		print(j, end=' ')
	print()


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()


from operator import itemgetter
(N, M) = map(int, input().split())
Arr = []
for i in range(0, N):
	temp = []
	temp = list(map(int, input().split()))
	Arr.append([])
	Arr[i] = temp
ind = int(input())
Arr = sorted(Arr, key=itemgetter(ind))
for i in range(N):
	for j in range(M):
		(print(Arr[i][j], 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()


import sys
(n, m) = [int(x) for x in sys.stdin.readline().split(' ')]
array = []
for i in range(0, n):
	array.append([int(x) for x in sys.stdin.readline().split(' ')])
k = [int(x) for x in sys.stdin.readline().split(' ')][0]

def ky(x):
	return x[k]
array.sort(key=ky)
for i in range(0, n):
	for j in range(0, m):
		sys.stdout.write('%d ' % array[i][j])
	sys.stdout.write('\n')


(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, m) = input().split(' ')
n = int(n)
m = int(m)
nestedList = list()
for i in range(0, n):
	row = input().split(' ')
	row = [int(j) for j in row]
	nestedList.append(row)
k = int(input())
nestedList.sort(key=lambda x: int(x[k]))
for row in nestedList:
	for element in row:
		print(element, end=' ')
	print(end='\n')


x = list(map(int, input().split()))
n = x[0]
m = x[1]
y = []
for i in range(n):
	y.append(list(map(int, input().split())))
k = int(input())
import operator

def pr(l):
	for i in range(len(l)):
		string = ''
		for j in range(len(l[i])):
			string += str(l[i][j]) + ' '
		print(string)
pr(list(sorted(y, key=operator.itemgetter(k))))


def main():
	(m, n) = (int(i) for i in input().split())
	data = []
	for i in range(m):
		data.append([int(x) for x in input().split()])
	k = int(input())

	def fun(lst):
		return lst[k]
	data.sort(key=fun)
	for i in data:
		for j in i:
			print(j, end=' ')
		print()
main()


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)))))


(t, c) = input().split()
(t, c) = (int(t), int(c))
a = []
for _ in range(t):
	a.append(input().split())
k = int(input())
for l in range(t):
	a[l][:] = (int(x) for x in a[l])
a = sorted(a, key=lambda x: x[k])
for i in range(t):
	for j in range(c):
		print(a[i][j], end=' ')
	print()


(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) = map(int, input().split())
a = []
for x in range(n):
	a.append(list(map(int, input().split())))
k = int(input())
b = sorted(a, key=lambda x: x[k])
for x in b:
	for i in x:
		print(i, end=' ')
	print('')


__author__ = 'Chamath'
from operator import itemgetter

def finder():
	N = int(input().strip().split()[0])
	full_list = []
	for i in range(N):
		temp = [int(x) for x in input().split()]
		full_list.append(temp)
	k = int(input())
	full_list = sorted(full_list, key=itemgetter(k))
	for j in full_list:
		for k in j:
			print(k, end=' ')
		print()
finder()


def line_to_int():
	return list(map(int, input().split()))
temp = line_to_int()
rows = temp[0]
cols = temp[1]
data = []
for i in range(0, rows):
	data.append(line_to_int())
sort_index = line_to_int()[0]
data = sorted(data, key=lambda l: l[sort_index])
for i in range(0, rows):
	for j in range(0, cols):
		print(data[i][j], end=' ')
	if i < rows - 1:
		print()


from operator import itemgetter
nm = list(map(int, input().split()))
lst = list()
for _ in range(0, nm[0]):
	lst.append(list(map(int, input().split())))
k = int(input())
lst = sorted(lst, key=itemgetter(k))
for l in lst:
	for val in l:
		print(val, end='')
		print(' ', end='')
	print('')


from operator import itemgetter
inp = list()
(N, M) = input().split()
for _ in range(int(N)):
	inp.append([int(x) for x in input().split()])
inp = sorted(inp, key=itemgetter(int(input())))
for elem in inp:
	for val in elem:
		print(val, end=' ')
	print()


(N, M) = [int(i) for i in input().split()]
elements = []
for i in range(N):
	elements.append([int(i) for i in input().split()])
K = int(input())
for element in sorted(elements, key=lambda x: x[K]):
	for item in element:
		print(item, end=' ')
	print('')


(rows, cols) = [int(t) for t in input().split()]
matrix = [None] * rows
for r in range(rows):
	matrix[r] = [int(t) for t in input().split()]
col = int(input())
matrix.sort(key=lambda r: r[col])
for line in matrix:
	for col in line:
		print(col, end=' ')
	print()


from operator import itemgetter

def finder():
	N = int(input().strip().split()[0])
	full_list = []
	for i in range(N):
		temp = [int(x) for x in input().split()]
		full_list.append(temp)
	k = int(input())
	full_list = sorted(full_list, key=itemgetter(k))
	for j in full_list:
		for k in j:
			print(k, end=' ')
		print()
finder()


(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)))


import itertools
first = input()
arr1 = first.split()
n = int(arr1[0])
fulList = []
indexList = []
for i in range(0, n):
	ent = input()
	parts = ent.split()
	fulList.append(parts)
	indexList.append(i)
k = int(input())
relElement = []
for i in range(0, len(fulList)):
	subList = fulList[i]
	relElement.append(int(subList[k]))
list_enumerate = itertools.count()
indexList.sort(reverse=False, key=lambda k: relElement[next(list_enumerate)])
relElement.sort()
for i in range(0, len(indexList)):
	for each in fulList[indexList[i]]:
		print(each, end=' ')
	print('')


from operator import itemgetter as ig
(N, M) = [int(i) for i in input().split()]
matrix = [[int(i) for i in input().split()] for j in range(N)]
K = int(input())
newmatrix = sorted(matrix, key=ig(K))
for line in newmatrix:
	for i in line:
		print(i, end=' ')
	print()


(n, m) = input().split()
n = int(n)
m = int(m)
arr = []
for i in range(0, n):
	arr.append([int(x) for x in input().split()])
k = int(input())
for sub in sorted(arr, key=lambda x: x[k]):
	for el in sub:
		print('{el} '.format(el=el), end='')
	print('')


i = 0

class Data(object):

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

	def __lt__(self, other):
		return int(self.a[i]) < int(other.a[i])

	def __gt__(self, other):
		return other.__lt__(self)

	def __eq__(self, other):
		return int(self.a[i]) == int(other.a[i])

	def __ne__(self, other):
		return not self.__eq__(other)
s = input()
nm = s.split(' ')
allData = []
for x in range(0, int(nm[0])):
	s = input()
	d = s.split(' ')
	allData.append(Data(d))
s = input()
i = int(s)
allData = sorted(allData)
for d in allData:
	s = ''
	for y in range(0, int(nm[1])):
		s = s + d.a[y] + ' '
	print(s)


import sys
import copy
tmp = input()
N = int(tmp.split()[0])
M = int(tmp.split()[1])
table = []
for i in range(N):
	tmp = input()
	table.append(tmp.split())
ind = int(input())
ans = copy.deepcopy(table)
minind = -1
order = []
for i in range(N):
	min = 10000
	for j in range(N):
		if min > int(table[j][ind]):
			min = int(table[j][ind])
			minind = j
	order.append(minind)
	table[minind][ind] = 10000
for i in order:
	for j in range(M):
		if j == M + 1:
			sys.stdout.write(ans[i][j])
		else:
			sys.stdout.write(str(ans[i][j]) + ' ')
	sys.stdout.write('\n')


__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))))


from operator import itemgetter
import sys
(n, m) = map(int, sys.stdin.readline().split())
table = []
for i in range(0, n):
	tmp = input().split(' ')
	tmp = list((int(x) for x in tmp))
	table.append(tmp)
k = int(input())
lst = sorted(table, key=itemgetter(k))
for i in range(0, n):
	for j in range(0, m):
		print(lst[i][j], end=' ')
	print()


x = input()
x = x.split()
data = []
for i in range(int(x[0])):
	y = input().split()
	data.append([])
	for j in range(int(x[1])):
		data[i].append(int(y[j]))

def bubbleSort(alist, row):
	for passnum in range(len(alist) - 1, 0, -1):
		for i in range(passnum):
			if alist[i][row] > alist[i + 1][row]:
				temp = alist[i]
				alist[i] = alist[i + 1]
				alist[i + 1] = temp
bubbleSort(data, int(input()))
for i in range(int(x[0])):
	for j in range(int(x[1])):
		print(data[i][j], end=' ')
	print()


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(']', ''))


import sys
(n, m) = list(map(int, sys.stdin.readline().split()))
a = [list(map(int, sys.stdin.readline().split())) for i in range(n)]
k = int(sys.stdin.readline())
a.sort(key=lambda x: x[k])
for x in a:
	for y in x:
		sys.stdout.write('{} '.format(y))
	sys.stdout.write('\n')


p = input().split()
n = int(p[0])
m = int(p[1])
matrix = []
for i in range(0, n):
	row = input().split()
	for j in range(0, m):
		row[j] = int(row[j])
	matrix.append(row)
k = int(input())
matrix = sorted(matrix, key=lambda row: row[k])
for i in range(0, n):
	for j in range(0, m):
		print(str(matrix[i][j]) + ' ', end='')
	print()


(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)]))


(a, b) = input().split()
(a, b) = (int(a), int(b))
nlist = []
for i in range(a):
	l = list(map(int, input().split()))
	nlist.append(l)
k = int(input())
nwlist = []
nwlist.append(nlist[0])
for i in range(1, len(nlist)):
	j = 0
	while j < len(nwlist) and nlist[i][k] >= nwlist[j][k]:
		j += 1
	nwlist.insert(j, nlist[i])
for row in nwlist:
	s = str()
	for a in row:
		s += str(a) + ' '
	print(s)


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, _))))


import sys
(N, M) = map(int, sys.stdin.readline().split())
lines = []
for i in range(N):
	m_attrs = map(int, sys.stdin.readline().split())
	m_attrs = tuple(m_attrs)
	lines.append(m_attrs)
K = int(sys.stdin.readline().strip())
result = sorted(lines, key=lambda x: x[K])
for line in result:
	print('%s ' * len(line) % line)


(m, n) = map(int, input().split())
l = [[] for i in range(m)]
for i in range(m):
	l[i] = list(map(int, input().split()))
k = int(input())
l1 = sorted(range(m), key=lambda x: (l[x][k], m))
for i in l1:
	for j in l[i]:
		print(j, end=' ')
	print()


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)


b = input().split()
c2 = []
for i in range(int(b[0])):
	c2.append(input().split())
x = int(input())
c2.sort(key=lambda row: int(row[x]))
for i in range(int(b[0])):
	for j in range(int(b[1])):
		print(c2[i][j], end=' ')
	print('')


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]))


(numLines, attrs) = map(int, input().split())
lines = []
for i in range(0, numLines):
	line = input().split()
	lines.append(line)
atrSort = int(input())
lines.sort(key=lambda atr: int(atr[atrSort]))

def printStrings(lines):
	str = ''
	for line in lines:
		for item in line:
			str += item + ' '
		str += '\n'
	return str
print(printStrings(lines))


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)))


s = input()
s = s.split(' ')
n = int(s[0])
m = int(s[1])
a = []
for i in range(n):
	s = input()
	s = [int(j) for j in s.split(' ')]
	a.append(s)
k = int(input())
b = sorted(a, key=lambda x: x[k])
for i in b:
	for j in i:
		print(j, end=' ')
	print()


vars = [int(i) for i in input().split(' ')]
n = vars[0]
m = vars[1]
lines = []
for i in range(n):
	temp = [int(i) for i in input().split(' ')]
	lines.append(temp)
k = eval(input())
lines = sorted(lines, key=lambda line: line[k])
for line in lines:
	for word in line:
		print(word, end=' ')
	print('')


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]))


k = 0

def help1(A):
	return A[k]
(n, m) = map(int, input().split())
A = [[] for x in range(n)]
for i in range(n):
	A[i] = list(map(int, input().split(' ')))
k = int(input())
A.sort(key=lambda x: x[k])
for x in range(n):
	s = ''
	for j in range(m):
		s += str(A[x][j]) + ' '
	print(s)


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()


from operator import itemgetter
(N, M) = map(int, input().split())
table = []
for n in range(N):
	table.append(list(map(int, input().split())))
K = int(input())
table = sorted(table, key=itemgetter(K))
for row in table:
	for col in row:
		print(col, end=' ')
	print()


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]))


(n, m) = map(int, input().split())
ar = []
for i in range(n):
	ar.append(list(map(int, input().split())))
k = int(input())
ar = sorted(ar, key=lambda x: x[k])
for i in ar:
	[print(x, end=' ') for x in i]
	print('')


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)


def main():
	(N, M) = map(int, input().split())
	records = []
	for i in range(N):
		record = [int(x) for x in input().split()]
		records.append(record)
	K = int(input())
	records = sorted(records, key=lambda x: x[K])
	for record in records:
		for x in record:
			print(x, end=' ')
		print()
main()


def sort_table(table, k):
	return sorted(table, key=lambda item: item[k])

def read_row():
	return list(map(int, input().split(' ')))

def print_row(r):
	for i in r:
		print(i, end=' ')
	print()
params = list(map(int, input().split(' ')))
table = []
for i in range(params[0]):
	table.append(read_row())
k = int(input())
sorted_table = sort_table(table, k)
for i in sorted_table:
	print_row(i)


(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)