Read problems statements in Mandarin Chinese  and Russian. 

Cascading Style Sheets, or CSS is a style sheet language, most popularly known for styling web pages. Each object on a web page (like a picture, a paragraph, a table) can have various properties (like height, width, size, colour), and these properties can be set using CSS. Each of these objects has a unique identifier called id, using which CSS can identify the object and assign value to its property.
Due to hierarchical nature of the web page, often same property of an object gets assigned different values in different CSS statements. To decide what the final value will be, each statement has a priority assigned to it. The statement with the highest priority will be the one that sets the value of the property. If there are more than one statements with same priority, then the one which was executed last amongst those will set the value.
Given multiple CSS statements in the order they were executed, answer a few queries about the final values of certain objects

------ Input ------ 
First line contains two space-separated integers N and M, the number of CSS statements and the number of queries respectively.
Following N lines will each have 4 values id, attr, val, priority, which mean that a CSS statement tries to assign value val to the attribute named attr of the object id, and that the priority of this statement is equal to priority.
Each of the next M lines will contain two values id and attr, for which you have to print value of the attribute attr of object id. You can assume that there is a statement that assigns value to this attribute attr of object id.

------ Output ------ 
Print exactly M lines, i^{th} line contains the answer to the i^{th} query as described above.

------ Constraints ------ 

1 ≤ value, priority ≤ 10^{6}, for all subtasks.
For 20 points : 1 ≤ id, N, M, attr ≤ 10^{3}For 30 points : 1 ≤ N, M ≤ 10^{3}, 1 ≤ id, attr ≤ 10^{6}For 50 points : 1 ≤ N, M ≤ 10^{5}, 1 ≤ id, attr ≤ 10^{6}

----- Sample Input 1 ------ 
3 1
22 1 200 2
22 1 300 2
22 1 100 1
22 1
----- Sample Output 1 ------ 
300

Difficulty: MEDIUM_HARD

Solutions:
(n, m) = [int(i) for i in input().split()]
d = {}
for i in range(n):
	(idr, attr, val, priority) = [int(i) for i in input().split()]
	if (idr, attr) not in d:
		d[idr, attr] = [val, priority]
	if d[idr, attr][1] <= priority:
		d[idr, attr] = [val, priority]
for i in range(m):
	(x, y) = [int(i) for i in input().split()]
	print(d[x, y][0])


(n, m) = [int(i) for i in input().split()]
dictt = {}
for i in range(n):
	(id, attr, val, priority) = [int(i) for i in input().split()]
	if (id, attr) not in dictt:
		dictt[id, attr] = [val, priority]
	if dictt[id, attr][1] <= priority:
		dictt[id, attr] = [val, priority]
for i in range(m):
	(x, y) = [int(i) for i in input().split()]
	print(dictt[x, y][0])


(n, m) = [int(x) for x in input().split()]
dict = {}
for i in range(n):
	(id, attr, value, priority) = [int(x) for x in input().split()]
	lst = [priority, value]
	if id not in dict:
		dict[id] = {attr: lst}
	elif attr not in dict[id]:
		dict[id][attr] = lst
	elif priority >= dict[id][attr][0]:
		dict[id][attr] = lst
for i in range(m):
	(id, attr) = [int(x) for x in input().split()]
	print(dict[id][attr][1])


(n, m) = map(int, input().split())
dic = {}
for i in range(n):
	(nid, atr, val, pri) = map(int, input().split())
	key = str(nid) + str(atr)
	if key not in dic:
		dic[key] = {}
		dic[key][pri] = []
		dic[key][pri].append(val)
	elif pri in dic[key]:
		dic[key][pri].append(val)
	else:
		dic[key][pri] = []
		dic[key][pri].append(val)
for i in range(m):
	(nid, atr) = map(int, input().split())
	key = str(nid) + str(atr)
	print(dic[key][max(dic[key])][-1])


(n, m) = map(int, input().split(' '))
dict1 = dict()
for i in range(n):
	(id, attribute, value, priority) = map(int, input().split(' '))
	if (id, attribute) not in dict1:
		dict1[id, attribute] = (value, priority)
	elif priority >= dict1[id, attribute][1]:
		dict1[id, attribute] = (value, priority)
for i in range(m):
	(id2, attribute2) = map(int, input().split(' '))
	print(dict1[id2, attribute2][0])


d = {}
(N, M) = map(int, input().split())
for i in range(N):
	(id_, attr, val, prio) = map(int, input().split())
	(c_val, c_prio) = d.get((id_, attr), (0, 0))
	if prio >= c_prio:
		d[id_, attr] = (val, prio)
for j in range(M):
	(id_, attr) = map(int, input().split())
	(val, prio) = d.get((id_, attr))
	print(val)


(n, m) = map(int, input().split())
d = {}
for i in range(n):
	(id, at, val, pr) = map(int, input().split())
	if id not in d:
		d[id] = {}
	if at not in d[id]:
		d[id][at] = [val, pr]
	elif d[id][at][1] <= pr:
		d[id][at] = [val, pr]
for j in range(m):
	(id, at) = map(int, input().split())
	print(d[id][at][0])


(n, m) = map(int, input().split())
css = dict()
for _ in range(n):
	(id, attr, val, priority) = map(int, input().split())
	key = (id, attr)
	if key not in css.keys():
		css[key] = [val, priority]
	else:
		x = css[key][1]
		if priority >= x:
			css[key] = [val, priority]
for _ in range(m):
	(id, attr) = map(int, input().split())
	key = (id, attr)
	print(css[key][0])


(N, Q) = map(int, input().split())
D = dict()
for _ in range(N):
	(idd, attr, val, prior) = map(int, input().split())
	key = (idd, attr)
	if key not in D.keys():
		D[key] = [val, prior]
	else:
		p = D[key][1]
		if prior >= p:
			D[key] = [val, prior]
for _ in range(Q):
	(idd, attr) = map(int, input().split())
	key = (idd, attr)
	print(D[key][0])


(n, m) = [int(c) for c in input().split()]
ids = [{} for i in range(10 ** 6)]
for i in range(n):
	(id, attr, val, pr) = [int(c) for c in input().split()]
	dic = ids[id]
	(cval, cpr) = dic.get(attr, (0, 0))
	if pr >= cpr:
		cval = val
		cpr = pr
	dic[attr] = (cval, cpr)
	ids[id] = dic
for i in range(m):
	(id, attr) = [int(c) for c in input().split()]
	print(ids[id][attr][0])


(n, m) = list(map(int, input().split()))
dt = {}
for i in range(n):
	(id, attr, val, prio) = list(map(int, input().split()))
	if (id, attr) in dt.keys():
		if dt[id, attr][1] <= prio:
			dt[id, attr] = (val, prio)
	else:
		dt[id, attr] = (val, prio)
for i in range(m):
	(id, attr) = list(map(int, input().split()))
	print(dt[id, attr][0])


try:
	(n, m) = map(int, input().split())
	caid = dict()
	for _ in range(n):
		(ids, attr, val, pr) = map(int, input().split())
		if ids not in caid:
			caid[ids] = dict()
		if attr not in caid[ids]:
			caid[ids][attr] = [0, -1]
		if pr >= caid[ids][attr][1]:
			caid[ids][attr] = [val, pr]
	for _ in range(m):
		(ids, attr) = map(int, input().split())
		print(caid[ids][attr][0])
except:
	pass


from collections import defaultdict
mp_attr = defaultdict(int)
priority_mp = defaultdict(int)
(n, m) = map(int, input().split())
for _ in range(n):
	(id, attr, val, priority) = map(int, input().split())
	cur_priority = priority_mp[id, attr]
	if priority >= cur_priority:
		priority_mp[id, attr] = priority
		mp_attr[id, attr] = val
for _ in range(m):
	(id, attr) = map(int, input().split())
	print(mp_attr[id, attr])


(N, M) = [int(i) for i in input().split(' ')]
attributes = {}
for i in range(N):
	(id, attr, val, priority) = [int(i) for i in input().split(' ')]
	if (id, attr) not in attributes or priority >= attributes[id, attr][1]:
		attributes[id, attr] = (val, priority)
for i in range(M):
	(id, attr) = [int(i) for i in input().split(' ')]
	print(attributes[id, attr][0])


(n, m) = [int(i) for i in input().split(' ')]
attributes = {}
for i in range(n):
	(id, attr, val, priority) = [int(i) for i in input().split(' ')]
	if (id, attr) not in attributes or priority >= attributes[id, attr][1]:
		attributes[id, attr] = (val, priority)
for i in range(m):
	(id, attr) = [int(i) for i in input().split(' ')]
	print(attributes[id, attr][0])


from collections import defaultdict
vals = defaultdict(lambda : {})
(n, m) = [int(x) for x in input().split()]
for i in range(n):
	(uid, atr, val, timestamp) = [int(x) for x in input().split()]
	if atr not in vals[uid]:
		vals[uid][atr] = {}
		vals[uid][atr]['val'] = val
		vals[uid][atr]['timestamp'] = timestamp
	elif vals[uid][atr]['timestamp'] <= timestamp:
		vals[uid][atr]['val'] = val
		vals[uid][atr]['timestamp'] = timestamp
for i in range(m):
	(uid, attr) = [int(x) for x in input().split()]
	print(vals[uid][attr]['val'])


t = 1
while t > 0:
	t -= 1
	(n, q) = map(int, input().split())
	hmap = {}
	while n > 0:
		n -= 1
		(id, atr, val, pri) = map(int, input().split())
		if id in hmap:
			if atr in hmap[id]:
				if hmap[id][atr][1] <= pri:
					hmap[id][atr][0] = val
					hmap[id][atr][1] = pri
			else:
				hmap[id][atr] = [val, pri]
		else:
			hmap[id] = {}
			hmap[id][atr] = [val, pri]
	while q > 0:
		q -= 1
		(id, atr) = map(int, input().split())
		print(hmap[id][atr][0])


def get_ans(n, m, main_dic, q_arr):
	for q in q_arr:
		max_p = 0
		tmp = []
		for l in main_dic[q[0]][q[1]]:
			if l[1] > max_p:
				max_p = l[1]
				tmp = [l[0]]
			elif l[1] == max_p:
				tmp.append(l[0])
		print(tmp[-1])
(n, m) = map(int, input().split())
main_dic = {}
for _ in range(n):
	tmp = list(map(int, input().split()))
	if tmp[0] not in main_dic:
		main_dic[tmp[0]] = {}
		main_dic[tmp[0]][tmp[1]] = []
	elif tmp[1] not in main_dic[tmp[0]]:
		main_dic[tmp[0]][tmp[1]] = []
	main_dic[tmp[0]][tmp[1]].append([tmp[2], tmp[3]])
q_arr = []
for _ in range(m):
	q_arr.append(list(map(int, input().split())))
get_ans(n, m, main_dic, q_arr)


from collections import defaultdict

def resolve(config, queries):
	cache = defaultdict(dict)
	for e in config:
		(c, a, v, p) = (e[0], e[1], e[2], e[3])
		attr_cache = cache[c]
		if a not in attr_cache or attr_cache[a][0] <= p:
			attr_cache[a] = (p, v)
	for q in queries:
		print(cache[q[0]][q[1]][1])
(n, m) = map(int, input().split())
config = []
for _ in range(n):
	config.append(list(map(int, input().split())))
queries = []
for _ in range(m):
	queries.append(list(map(int, input().split())))
resolve(config, queries)


from collections import defaultdict

def solve(css_elements, queries):
	css_elements.sort(key=lambda x: x[-1])
	css_dict = defaultdict(lambda : defaultdict(int))
	for css_element in css_elements:
		css_dict[css_element[0]][css_element[1]] = css_element[2]
	for query in queries:
		print(css_dict[query[0]][query[1]])

def main():
	(n, m) = map(int, input().split())
	css_elements = []
	queries = []
	for i in range(n):
		css_elements.append(list(map(int, input().split())))
	for i in range(m):
		queries.append(list(map(int, input().split())))
	solve(css_elements, queries)
main()


nm = input().split(' ')
(n, m) = (int(nm[0]), int(nm[1]))
ids = {}
for i in range(n):
	iavp = input().split(' ')
	(cssid, a, v, p) = (int(iavp[0]), int(iavp[1]), int(iavp[2]), int(iavp[3]))
	if ids.get(cssid) is None:
		ids[cssid] = {}
		ids[cssid][a] = (v, p)
	else:
		temp = ids[cssid]
		if temp.get(a) is None:
			temp[a] = (v, p)
		elif p >= temp[a][1]:
			temp[a] = (v, p)
for i in range(m):
	ia = input().split(' ')
	(cssid, a) = (int(ia[0]), int(ia[1]))
	print(ids[cssid][a][0])


(n, m) = map(int, input().split())
dct = {}
for i in range(n):
	(id, attr, val, p) = map(int, input().split())
	(prev_p, prev_val) = dct.get((id, attr), [0, 0])
	if p >= prev_p:
		dct[id, attr] = [p, val]
for i in range(m):
	(id, attr) = map(int, input().split())
	print(dct[id, attr][1])


from collections import defaultdict
(n, m) = map(int, input().split())
dic = defaultdict(dict)
for i in range(n):
	(idd, attr, val, prio) = map(int, input().split())
	if attr not in dic[idd]:
		dic[idd][attr] = [val, prio]
	elif prio >= dic[idd][attr][1]:
		dic[idd][attr] = [val, prio]
for i in range(m):
	(idd, attr) = map(int, input().split())
	print(dic[idd][attr][0])


data = {}
(n, m) = map(int, input().split())
for i in range(n):
	(id_, attr, val, priority) = map(int, input().split())
	key = (id_, attr)
	if key not in data.keys() or priority >= data[key][1]:
		data[key] = (val, priority)
for i in range(m):
	(id_, attr) = map(int, input().split())
	key = (id_, attr)
	print(data[key][0])


(n, m) = map(int, input().split())
d1 = {}
for i in range(n):
	(a, b, c, d) = map(int, input().split())
	if (a, b) not in d1:
		d1[a, b] = [c, d]
	elif d1[a, b][1] <= d:
		d1[a, b] = [c, d]
for __ in range(m):
	(x, y) = map(int, input().split())
	print(d1[x, y][0])


def main():
	(nb_stmt, nb_queries) = [int(x) for x in input().split()]
	attrmap = dict()
	prioritymap = dict()
	for i in range(nb_stmt):
		line = input().split()
		key = line[0] + '#' + line[1]
		priority = int(line[3])
		if key not in prioritymap or prioritymap[key] <= priority:
			attrmap[key] = int(line[2])
			prioritymap[key] = priority
	for j in range(nb_queries):
		line = input().split()
		key = line[0] + '#' + line[1]
		print(attrmap[key])
main()


def io(func=str, n=1):

	def nfp():
		ff = str
		try:
			it = iter(func)

			def nf(val):
				nonlocal ff
				ff = next(it, ff)
				return ff(val)
			return type(func)((nf(va) for va in input().split()))
		except TypeError:
			return func(input())
	if type(n) is list:
		return [_(func, x) for x in n]
	else:
		t = lambda : nfp()
		if n == 1:
			return t()
		elif n == 0:
			return [t() for i in range(int(input()))]
		else:
			return [t() for i in range(n)]

def main():
	(N, M) = io((int, int))
	css = {}
	for i in range(N):
		(id, attr, v1, p1) = io((int, int, int, int))
		(v2, p2) = css.get((id, attr), (0, 0))
		if p2 <= p1:
			css[id, attr] = (v1, p1)
	for i in range(M):
		(id, attr) = io((int, int))
		yield css[id, attr][0]
print('\n'.join([str(res) for res in main()]))


(n, m) = (int(x) for x in input().strip().split())
entry = dict()
for i in range(n):
	(_id, attr, val, pty) = (int(x) for x in input().strip().split())
	temp = entry.get(tuple((_id, attr)), tuple())
	if not temp:
		entry[_id, attr] = (pty, val)
	elif temp[0] <= pty:
		entry[_id, attr] = (pty, val)
for i in range(m):
	(_id, attr) = (int(x) for x in input().strip().split())
	print(entry[_id, attr][1])


(t, queries) = map(int, input().split())
css = dict()
for _ in range(t):
	(id, attr, val, priority) = map(int, input().split())
	if (id, attr) in css and priority >= css[id, attr][1]:
		css[id, attr] = (val, priority)
	elif not (id, attr) in css:
		css[id, attr] = (val, priority)
for _ in range(queries):
	(id, attr) = map(int, input().split())
	print(css[id, attr][0])


(n, m) = map(int, input().split())
stmt = {}
for i in range(n):
	(id, attr, val, priority) = map(int, input().split())
	if (id, attr) in stmt:
		if stmt[id, attr][1] <= priority:
			stmt[id, attr] = [val, priority]
	else:
		stmt[id, attr] = [val, priority]
for i in range(m):
	(id, attr) = map(int, input().split())
	print(stmt[id, attr][0])


(n, m) = [int(x) for x in input().split()]
elems = {}
for i in range(0, n):
	(id, attr, val, priority) = [int(x) for x in input().split()]
	if id not in elems:
		elems[id] = {}
	element = elems[id]
	if attr not in element or element[attr][1] <= priority:
		element[attr] = [val, priority]
for i in range(0, m):
	(id, attr) = [int(x) for x in input().split()]
	print(str(elems[id][attr][0]))


I = [int(x) for x in input().split()]
n = I[0]
m = I[1]
CssObject = {}
CssPri = {}
while n > 0:
	n -= 1
	I2 = [int(x) for x in input().split()]
	id = I2[0]
	attr = I2[1]
	val = I2[2]
	pri = I2[3]
	if id in CssObject.keys():
		if attr in CssObject[id].keys():
			if CssPri[id][attr] <= pri:
				CssPri[id][attr] = pri
				CssObject[id][attr] = val
		else:
			CssObject[id][attr] = val
			CssPri[id][attr] = pri
	else:
		CssObject[id] = {}
		CssPri[id] = {}
		CssObject[id][attr] = val
		CssPri[id][attr] = pri
while m > 0:
	m -= 1
	I3 = [int(x) for x in input().split()]
	print(CssObject[I3[0]][I3[1]])


(N, M) = input().split(' ')
N = int(N)
M = int(M)
hm = {}
for i in range(N):
	st = input()
	(id, att, v, pr) = st.split(' ')
	if (id, att) in hm:
		if int(hm[id, att][1]) <= int(pr):
			hm[id, att] = (v, pr)
	else:
		hm[id, att] = (v, pr)
for i in range(M):
	(id, att) = input().split(' ')
	print(hm[id, att][0])