Polycarp is currently developing a project in Vaja language and using a popular dependency management system called Vamen. From Vamen's point of view both Vaja project and libraries are treated projects for simplicity.

A project in Vaja has its own uniqie non-empty name consisting of lowercase latin letters with length not exceeding 10 and version — positive integer from 1 to 10^6. Each project (keep in mind that it is determined by both its name and version) might depend on other projects. For sure, there are no cyclic dependencies.

You're given a list of project descriptions. The first of the given projects is the one being developed by Polycarp at this moment. Help Polycarp determine all projects that his project depends on (directly or via a certain chain). 

It's possible that Polycarp's project depends on two different versions of some project. In this case collision resolving is applied, i.e. for each such project the system chooses the version that minimizes the distance from it to Polycarp's project. If there are several options, the newer (with the maximum version) is preferred. This version is considered actual; other versions and their dependencies are ignored.

More formal, choose such a set of projects of minimum possible size that the following conditions hold:   Polycarp's project is chosen;  Polycarp's project depends (directly or indirectly) on all other projects in the set;  no two projects share the name;  for each project x that some other project in the set depends on we have either x or some y with other version and shorter chain to Polycarp's project chosen. In case of ties the newer one is chosen. 

Output all Polycarp's project's dependencies (Polycarp's project itself should't be printed) in lexicographical order.


-----Input-----

The first line contains an only integer n (1 ≤ n ≤ 1 000) — the number of projects in Vaja.

The following lines contain the project descriptions. Each project is described by a line consisting of its name and version separated by space. The next line gives the number of direct dependencies (from 0 to n - 1) and the dependencies themselves (one in a line) in arbitrary order. Each dependency is specified by its name and version. The projects are also given in arbitrary order, but the first of them is always Polycarp's. Project descriptions are separated by one empty line. Refer to samples for better understanding.

It's guaranteed that there are no cyclic dependencies. 


-----Output-----

Output all Polycarp's project's dependencies in lexicographical order.


-----Examples-----
Input
4
a 3
2
b 1
c 1
 
b 2
0
 
b 1
1
b 2
 
c 1
1
b 2

Output
2
b 1
c 1

Input
9
codehorses 5
3
webfrmk 6
mashadb 1
mashadb 2
 
commons 2
0
 
mashadb 3
0
 
webfrmk 6
2
mashadb 3
commons 2
 
extra 4
1
extra 3
 
extra 3
0
 
extra 1
0
 
mashadb 1
1
extra 3
 
mashadb 2
1
extra 1

Output
4
commons 2
extra 1
mashadb 2
webfrmk 6

Input
3
abc 1
2
abc 3
cba 2

abc 3
0

cba 2
0

Output
1
cba 2



-----Note-----

The first sample is given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project «a» (version 3) depends on are painted black. [Image] 

The second sample is again given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project «codehorses» (version 5) depends on are paint it black. Note that «extra 1» is chosen instead of «extra 3» since «mashadb 1» and all of its dependencies are ignored due to «mashadb 2». [Image]

Source: codeforces | Difficulty: MEDIUM


Solutions:
n = int(input())
names_set = set()
s = {}
names = []
arr = [[] for j in range(n)]
g = 0
for i in range(n):
	el = input()
	names_set.add(el.split()[0])
	ind = 0
	if s.get(el) is None:
		s[el] = g
		names.append(el)
		g += 1
	k = int(input())
	for j in range(k):
		el2 = input()
		if s.get(el2) is None:
			s[el2] = g
			names.append(el2)
			g += 1
		arr[s.get(el)].append(s.get(el2))
	if i != n - 1:
		r = input()
res = []
q = []
q.append(0)
sp = {}
while len(q):
	el = q[0]
	del q[0]
	(name, vers) = names[el].split()
	if name in names_set:
		try:
			sp[name] = max(int(vers), sp[name])
		except:
			sp[name] = int(vers)
	if not len(q):
		for i in sp:
			names_set.remove(i)
			new_el = []
			new_el.append(i)
			new_el.append(sp[i])
			res.append(new_el[:])
			ind = s[str(new_el[0]) + ' ' + str(new_el[1])]
			for j in range(len(arr[ind])):
				p = arr[ind][j]
				q.append(p)
		sp = {}
res = res[1:]
res.sort()
print(len(res))
for i in res:
	print(i[0], i[1])


def scan_project():
	(name, version_str) = input().split()
	return (name, int(version_str))
n = int(input())
(projects, depends) = ([], {})
for i in range(n):
	if i > 0:
		input()
	project = scan_project()
	projects.append(project)
	depends[project] = [scan_project() for j in range(int(input()))]
(root_name, root_version) = projects[0]
level_depends = [projects[0]]
all_depends = {root_name: root_version}
while level_depends:
	level_depends = list(set(((name, version) for project in level_depends for (name, version) in depends[project] if name not in all_depends)))
	for (name, version) in sorted(level_depends):
		all_depends[name] = version
	level_depends = [(name, version) for (name, version) in level_depends if all_depends[name] == version]
all_depends.pop(root_name)
print(len(all_depends))
for name in sorted(all_depends):
	print(name, all_depends[name])


from collections import defaultdict

def rp():
	s = input().split()
	return (s[0], int(s[1]))
ps = {}
n = int(input())
for i in range(n):
	p = rp()
	d = []
	for _ in range(int(input())):
		d += [rp()]
	ps[p] = d
	if i != n - 1:
		input()
root = list(ps.keys())[0]
q = [(root, 0)]
u = {root[0]: (root[1], 0)}
for (i, l) in q:
	isp = i
	if isp[0] in u and isp[1] != u[isp[0]][0]:
		continue
	for p in ps[i]:
		psp = p
		if psp[0] not in u or (u[psp[0]][1] == l + 1 and u[psp[0]][0] < psp[1]):
			u[psp[0]] = (psp[1], l + 1)
			q.append((psp, l + 1))
del u[root[0]]
print(len(u))
for i in sorted(u):
	print(i, u[i][0])


levels = []
connect = dict()
n = int(input())
k = input().split()
levels.append({k[0]: int(k[1])})
connect[k[0] + ' ' + str(k[1])] = []
d = int(input())
for i in range(d):
	connect[k[0] + ' ' + str(k[1])].append(input())
for i in range(n - 1):
	s = input()
	s = input()
	connect[s] = []
	k = int(input())
	for j in range(k):
		connect[s].append(input())
lev = 0
while len(levels[len(levels) - 2]):
	levels.append(dict())
	for projname in levels[lev]:
		listing = connect[projname + ' ' + str(levels[lev][projname])]
		for i in listing:
			name = i.split()
			gov = True
			for j in range(lev + 1):
				if name[0] in levels[j]:
					gov = False
					break
			if gov:
				if name[0] not in levels[lev + 1]:
					levels[lev + 1][name[0]] = int(name[1])
				else:
					found = levels[lev + 1][name[0]]
					levels[lev + 1][name[0]] = max(int(name[1]), found)
	lev += 1
res = {}
for i in range(1, len(levels) - 2):
	res.update(levels[i])
keys = list(res.keys())
keys.sort()
print(len(keys))
for i in keys:
	print(i + ' ' + str(res[i]))


from collections import defaultdict
n = int(input())
d = defaultdict(dict)
for i in range(n):
	x = input()
	x = x.split()
	if i == 0:
		P = x
	q = int(input())
	Con = []
	for j in range(q):
		Con.append(input().split())
	if i != n - 1:
		input()
	d[x[0]][x[1]] = Con
ver = [P]
ans = {}
while ver:
	next_ans = {}
	for v in ver:
		C = d[v[0]][v[1]]
		for n_v in C:
			if n_v[0] not in ans and n_v[0] != P[0]:
				if n_v[0] in next_ans:
					if int(n_v[1]) > int(next_ans[n_v[0]]):
						next_ans[n_v[0]] = n_v[1]
				else:
					next_ans[n_v[0]] = n_v[1]
	ans.update(next_ans)
	ver = list(next_ans.items())
l = list(ans.items())
print(len(l))
l.sort()
for (k, v) in l:
	print(k, v)


N = int(input())
Slide = dict()
SlideList = [list()] * N
ItogInformation = set()
GoodNameTags = set()
(NewName, NewVer) = input().split()
NewVer = int(NewVer)
GoodNameTags.add(NewName)
Slide[0] = NewName + ' ' + str(NewVer)
Slide[NewName + ' ' + str(NewVer)] = 0
NewNum = int(input())
SlideList[0] = [0] * (NewNum + 2)
SlideList[0][1] = NewNum
for i in range(1, NewNum + 1):
	(Name, Ver) = input().split()
	SlideList[0][i + 1] = Name + ' ' + Ver
for j in range(1, N):
	input()
	(NewName, NewVer) = input().split()
	NewVer = int(NewVer)
	Slide[j] = NewName + ' ' + str(NewVer)
	Slide[NewName + ' ' + str(NewVer)] = j
	NewNum = int(input())
	SlideList[j] = [0] * (NewNum + 2)
	SlideList[j][1] = NewNum
	for i in range(1, NewNum + 1):
		(Name, Ver) = input().split()
		SlideList[j][i + 1] = Name + ' ' + Ver
ListOfNeed = set()
ListOfRead = set()
ListOfRead.add(Slide[0])
while len(ListOfRead) != 0:
	ListOfNeed = ListOfRead.copy()
	ListOfRead = set()
	found = dict()
	for i in ListOfNeed:
		for j in range(SlideList[Slide[i]][1]):
			(Name, Ver) = SlideList[Slide[i]][j + 2].split()
			Ver = int(Ver)
			key = found.pop(Name, -1)
			if key == -1 or key < Ver:
				found[Name] = Ver
			else:
				found[Name] = key
	for i in list(found.keys()):
		if not i in GoodNameTags:
			GoodNameTags.add(i)
			ListOfRead.add(i + ' ' + str(found[i]))
			ItogInformation.add(i + ' ' + str(found[i]))
Otvet = list(ItogInformation)
Otvet.sort()
print(len(Otvet))
for i in range(len(Otvet)):
	print(Otvet[i])


def bfs(polycarp, all_projects, existed_projects):
	queue0 = {polycarp[0]: polycarp[1]}
	queue1 = {}
	while queue0:
		for p in list(queue0.items()):
			existed_projects[p[0]] = p[1]
		for pp in list(queue0.items()):
			for p in all_projects[pp]:
				if p[0] not in existed_projects:
					if p[0] not in queue1:
						queue1[p[0]] = p[1]
					else:
						queue1[p[0]] = max(queue1[p[0]], p[1])
				else:
					continue
		(queue0, queue1) = (queue1, {})

def main():
	n = int(input())
	all_projects = {}
	for _ in range(n):
		(name, version) = input().split()
		version = int(version)
		if _ == 0:
			polycarp = (name, version)
		nn = int(input())
		all_projects[name, version] = []
		for _2 in range(nn):
			(name2, version2) = input().split()
			version2 = int(version2)
			all_projects[name, version].append((name2, version2))
		if _ != n - 1:
			input()
	existed_projects = {}
	bfs(polycarp, all_projects, existed_projects)
	print(len(existed_projects) - 1)
	for name in sorted(existed_projects.keys()):
		if name != polycarp[0]:
			print(name, existed_projects[name])

def __starting_point():
	main()
__starting_point()


n = int(input())
d = {}
for x in range(n):
	(l, r) = list(map(str, input().split()))
	r = int(r)
	if x == 0:
		L = l
		R = r
	if not (l, r) in d:
		d[l, r] = []
	m = int(input())
	for y in range(m):
		(l1, r1) = list(map(str, input().split()))
		d[l, r].append((l1, int(r1)))
	if x != n - 1:
		s = input()
lst = []
array = [L]
temp1 = [(L, R)]
temp2 = []
k1 = 0
k2 = 1
while k1 < len(temp1) or k2 < len(temp2):
	if k2 > len(temp2):
		while k1 < len(temp1):
			for (a, b) in d[temp1[k1]]:
				temp2.append((a, -b))
			k1 += 1
		temp2.sort()
		temp = []
		for (a, b) in temp2:
			if not a in array:
				array.append(a)
				lst.append((a, -b))
				temp.append((a, -b))
		temp2 = temp[:]
		temp1 = []
		k1 = 1
		k2 = 0
	elif k1 > len(temp1):
		while k2 < len(temp2):
			for (a, b) in d[temp2[k2]]:
				temp1.append((a, -b))
			k2 += 1
		temp1.sort()
		temp = []
		for (a, b) in temp1:
			if not a in array:
				array.append(a)
				lst.append((a, -b))
				temp.append((a, -b))
		temp1 = temp[:]
		temp2 = []
		k2 = 1
		k1 = 0
lst.sort()
print(len(lst))
for (a, b) in lst:
	print(a, b)


def readline():
	return input()

def read_project():
	project = readline().split(' ')
	project[1] = int(project[1])
	dep_num = int(readline())
	deps = {}
	for _ in range(0, dep_num):
		(proj, version) = readline().split(' ')
		version = int(version)
		if proj in deps:
			deps[proj] = max(deps[proj], version)
		else:
			deps[proj] = version
	return (tuple(project), deps)

def make_like_buck():
	projects_num = int(readline())
	projects = {}
	polikarp_proj = None
	for ind in range(0, projects_num):
		(proj, deps) = read_project()
		if ind == 0:
			polikarp_proj = proj
		projects[proj] = deps
		if ind != projects_num - 1:
			readline()
	RESULT_DEPS = {polikarp_proj[0]: polikarp_proj[1]}
	curr_proj = [polikarp_proj]
	while len(curr_proj) > 0:
		curr_deps = {}
		for proj in curr_proj:
			new_deps = projects[proj]
			for proj in new_deps:
				if proj in RESULT_DEPS:
					continue
				if proj in curr_deps:
					curr_deps[proj] = max(curr_deps[proj], new_deps[proj])
				else:
					curr_deps[proj] = new_deps[proj]
		RESULT_DEPS = {**RESULT_DEPS, **curr_deps}
		curr_proj = list(curr_deps.items())
	RESULT_DEPS.pop(polikarp_proj[0])
	print(len(RESULT_DEPS))
	items = ['%s %s' % x for x in sorted(list(RESULT_DEPS.items()))]
	print('\n'.join(items))
make_like_buck()


def read_project_name():
	(name, version) = input().split()
	return (name, int(version))

def read_project():
	project = read_project_name()
	deps = list()
	dependencies = int(input())
	while dependencies:
		dependencies -= 1
		deps.append(read_project_name())
	return (project, tuple(deps))

def main():
	dependencies = dict()
	n = int(input()) - 1
	(main_project, deps) = read_project()
	dependencies[main_project] = deps
	queue = (main_project,)
	while n:
		n -= 1
		input()
		(project, deps) = read_project()
		dependencies[project] = deps
	selected = dict()
	while queue:
		new_selected = dict()
		for (name, version) in queue:
			if name not in selected:
				old = new_selected.get(name, -1)
				new_selected[name] = max(old, version)
		queue = list()
		for project in new_selected.items():
			queue.extend(dependencies[project])
		selected.update(new_selected)
	del selected[main_project[0]]
	print(len(selected))
	for (project, ver) in sorted(selected.items()):
		print(project, ver)
main()


n = int(input())
projects = {}
root = None
for i in range(n):
	(name, v) = input().split()
	v = int(v)
	projects[name, v] = []
	m = int(input())
	for j in range(m):
		(nn, vv) = input().split()
		vv = int(vv)
		projects[name, v].append((nn, vv))
	if i < n - 1:
		input()
	if not i:
		root = (name, v)
q = set([root])
ans = {}
while q:
	temp = {}
	for u in q:
		for v in projects[u]:
			if v[0] not in ans and v[0] != root[0]:
				if v[0] not in temp:
					temp[v[0]] = v[1]
				else:
					temp[v[0]] = max(temp[v[0]], v[1])
	ans.update(temp.items())
	q = set(temp.items())
print(len(ans))
print('\n'.join(list(map(lambda x: ' '.join((str(c) for c in x)), sorted(list(ans.items()), key=lambda item: item[0])))))


def read_pack():
	(name, ver) = input().strip().split()
	return (name, int(ver))
n = int(input())
deps = dict()
for i in range(n):
	pack = read_pack()
	if not i:
		root = pack
	dep_n = int(input())
	deps[pack] = [read_pack() for _ in range(dep_n)]
	if i != n - 1:
		input()
queue = [(root, 0)]
taken = {root[0]: (root[1], 0)}
for (pack, level) in queue:
	if pack[0] in taken and pack[1] != taken[pack[0]][0]:
		continue
	for dep in deps[pack]:
		if dep[0] not in taken or (taken[dep[0]][1] == level + 1 and taken[dep[0]][0] < dep[1]):
			taken[dep[0]] = (dep[1], level + 1)
			queue.append((dep, level + 1))
del taken[root[0]]
print(len(taken))
for d in sorted(taken):
	print(d, taken[d][0])


from queue import Queue

def main():
	n = int(input())
	d = {}
	res = {}
	start = None
	for i in range(n):
		(project, version) = input().split()
		version = int(version)
		if i == 0:
			start = (project, version)
		k = int(input())
		if project not in d:
			d[project] = {}
		if version not in d[project]:
			d[project][version] = []
		for j in range(k):
			(p, v) = input().split()
			v = int(v)
			d[project][version].append((p, v))
		if i != n - 1:
			input()
	q = Queue()
	q.put(start)
	k = 1
	while not q.empty():
		append = {}
		for i in range(k):
			(s_p, s_v) = q.get()
			for (p, v) in d[s_p][s_v]:
				if p == start[0]:
					continue
				if p in res:
					continue
				if p not in append or append[p] < v:
					append[p] = v
		k = len(append)
		for p in append:
			res[p] = append[p]
			q.put((p, append[p]))
	ans = []
	for p in res:
		v = res[p]
		ans.append((p, v))
	ans = sorted(ans, key=lambda z: z[0])
	print(len(ans))
	for (p, v) in ans:
		print(p, v)
main()


from queue import Queue

def BFS(v):
	nonlocal ans
	stack = Queue()
	stack.put(v)
	used = {p[0]: False for p in list(graph.keys())}
	used[v[0]] = True
	while True:
		set_ = dict()
		st = Queue()
		while not stack.empty():
			v = stack.get()
			for u in graph[v]:
				n_ = u[0]
				if not used[n_]:
					try:
						set_[n_] = max(set_[n_], u[1])
					except KeyError:
						set_[n_] = u[1]
		for el in list(set_.items()):
			ans.append(el)
			st.put(el)
			used[el[0]] = True
		if st.empty():
			break
		while not st.empty():
			stack.put(st.get())
n = int(input())
graph = dict()
for i in range(n):
	(name, ver) = input().split()
	k = int(input())
	temp = set()
	for j in range(k):
		(name_, ver_) = input().split()
		temp.add((name_, int(ver_)))
	tup = (name, int(ver))
	graph[tup] = temp
	if i == 0:
		PP = tup
	if i + 1 != n:
		input()
ans = list()
BFS(PP)
print(len(ans))
print('\n'.join([' '.join(map(str, el)) for el in sorted(ans, key=lambda x: x[0])]))


from sys import stdin, stdout

def read_pack():
	(name, ver) = stdin.readline().split()
	return (name, int(ver))
n = int(input())
deps = dict()
for i in range(n):
	pack = read_pack()
	if not i:
		root = pack
	dep_n = int(input())
	deps[pack] = [read_pack() for _ in range(dep_n)]
	if i != n - 1:
		input()
queue = [(root, 0)]
taken = {root[0]: (root[1], 0)}
for (pack, level) in queue:
	for dep in deps[pack]:
		if dep[0] not in taken:
			taken[dep[0]] = (dep[1], level + 1)
			queue.append((dep, level + 1))
		elif taken[dep[0]][1] == level + 1 and taken[dep[0]][0] < dep[1]:
			index = queue.index(((dep[0], taken[dep[0]][0]), level + 1))
			taken[dep[0]] = (dep[1], level + 1)
			queue[index] = (dep, level + 1)
del taken[root[0]]
print(len(taken))
for d in sorted(taken):
	print(d, taken[d][0])