Misha hacked the Codeforces site. Then he decided to let all the users change their handles. A user can now change his handle any number of times. But each new handle must not be equal to any handle that is already used or that was used at some point.

Misha has a list of handle change requests. After completing the requests he wants to understand the relation between the original and the new handles of the users. Help him to do that.


-----Input-----

The first line contains integer q (1 ≤ q ≤ 1000), the number of handle change requests.

Next q lines contain the descriptions of the requests, one per line.

Each query consists of two non-empty strings old and new, separated by a space. The strings consist of lowercase and uppercase Latin letters and digits. Strings old and new are distinct. The lengths of the strings do not exceed 20.

The requests are given chronologically. In other words, by the moment of a query there is a single person with handle old, and handle new is not used and has not been used by anyone.


-----Output-----

In the first line output the integer n — the number of users that changed their handles at least once.

In the next n lines print the mapping between the old and the new handles of the users. Each of them must contain two strings, old and new, separated by a space, meaning that before the user had handle old, and after all the requests are completed, his handle is new. You may output lines in any order.

Each user who changes the handle must occur exactly once in this description.


-----Examples-----
Input
5
Misha ILoveCodeforces
Vasya Petrov
Petrov VasyaPetrov123
ILoveCodeforces MikeMirzayanov
Petya Ivanov

Output
3
Petya Ivanov
Misha MikeMirzayanov
Vasya VasyaPetrov123

Difficulty: EASY
Tags: String algorithms

Data structures

Spanning trees
Skill types: Data structures

Starter code:

Solutions:
(u, v) = ({}, {})
for _ in range(int(input())):
	(old, new) = input().split()
	if old not in v:
		u[old] = new
		v[new] = old
	else:
		a = v[old]
		u[a] = new
		v[new] = a
print(len(u))
for i in u:
	print(i, u[i])


n = input()
z = 0
tuplas = list()
for i in range(int(n)):
	x = input()
	x = x.split(' ')
	t = [x[0], x[1]]
	a = 0
	if i == 0:
		tuplas.append(t)
		continue
	for tup in tuplas:
		if x[0] == tup[1]:
			tup[1] = x[1]
			a = 1
			break
	if a == 0:
		tuplas.append(t)
print(len(tuplas))
for x in tuplas:
	print(x[0] + ' ' + x[1])


n = int(input())
users = {}
for i in range(n):
	(a, b) = list(map(str, input().split()))
	flag = 0
	if len(users) > 0:
		for (name, newName) in users.items():
			if newName == a:
				flag = 1
				break
		if flag == 1:
			users[name] = b
		else:
			users[a] = b
	else:
		users[a] = b
print(len(users))
for (name, newName) in users.items():
	arr = []
	arr.append(name)
	arr.append(newName)
	print(*arr)


size = int(input())
lista_completa = []
lista_completa.append([1])
add = 0
for i in range(0, size):
	(old, new) = input().split(' ')
	for e in lista_completa:
		if old == e[-1]:
			e.append(new)
			add = 1
		else:
			continue
	if add == 0:
		lista_completa.append([old, new])
	add = 0
print(len(lista_completa) - 1)
lista_completa = lista_completa[1:]
for e in lista_completa:
	print(e[0] + ' ' + e[-1])


import copy
import sys
from collections import defaultdict, Counter

def input():
	return sys.stdin.readline()

def mapi():
	return map(int, input().split())

def maps():
	return map(str, input().split())

def print(arg, *argv, end=None):
	sys.stdout.write(str(arg))
	for i in argv:
		sys.stdout.write(' ' + str(i))
	sys.stdout.write(end) if end else sys.stdout.write('\n')

def GCD(x, y):
	return GCD(y, x % y) if y else x

def modPow(x, y, p):
	(res, x) = (1, x % p)
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		(y, x) = (y >> 1, x * x % p)
	return res

def modInv(s, mod):
	return modPow(s, mod - 2, mod)

def solve():
	t = 1
	while t:
		t -= 1
		n = int(input())
		tmp = {}
		for i in range(n):
			(a, b) = maps()
			tmp[a] = b
		res = []
		dn = {}
		for (key, val) in tmp.items():
			if key not in dn:
				x = val
				while x in tmp:
					dn[x] = 1
					x = tmp[x]
				dn[key] = 1
				res.append([key, x])
		print(len(res))
		for i in res:
			print(' '.join(i))
solve()


import sys
sys.setrecursionlimit(1500)
from collections import OrderedDict
d = OrderedDict()
visited = set()
for item in range(int(input())):
	(old, new) = input().split()
	d[old] = new

def recurse(name):
	if name in d.keys():
		visited.add(name)
		return recurse(d[name])
	return name
ans = []
for item in d:
	if item not in visited:
		ans.append((item, recurse(item)))
print(len(ans))
for (old, new) in ans:
	print(old, new)


n = int(input())
lista = []
for i in range(n):
	lista.append(input().split())
output_list = []
cambios = []
for i in range(len(lista)):
	if lista[i][0] not in output_list and lista[i][1] not in output_list:
		output_list.append(lista[i][0])
		output_list.append(lista[i][1])
	elif lista[i][0] in output_list and lista[i][1] not in output_list:
		for j in range(len(output_list)):
			if output_list[j] == lista[i][0]:
				output_list.pop(j)
				output_list.insert(j, lista[i][1])
output = []
for i in range(len(output_list) // 2):
	output.append(output_list[i * 2] + ' ' + output_list[i * 2 + 1])
print(len(output))
for i in output:
	print(i)


p = {}
for i in range(int(input())):
	(old, new) = input().split()
	p[old] = new
ans = []
for i in p:
	temp = p[i]
	if temp == 'hibru':
		continue
	else:
		while temp in p:
			p[i] = p[temp]
			temp2 = p[temp]
			p[temp] = 'hibru'
			temp = temp2
		ans.append([i, p[i]])
print(len(ans))
for i in ans:
	print('{} {}'.format(i[0], i[1]))


t = int(input())
d = {}
count = 0
flag = 0
for _ in range(t):
	x = list(input().split())
	if x[0] not in d.values():
		d[x[0]] = x[1]
		count += 1
	else:
		for key in d.keys():
			if d[key] == x[0]:
				d[key] = x[1]
print(count)
for key in d.keys():
	print(key, d[key])


q = int(input())
(d, flag) = (dict(), dict())
for i in range(q):
	(a, b) = input().split()
	if d.get(a) is None:
		d[a] = a
	d[b] = d[a]
	flag[a] = 0
	flag[b] = 1
cnt = sum(flag.values())
print(cnt)
for s in d:
	if flag[s]:
		print(d[s], s)


n = int(input())
users = {}
for i in range(n):
	(old, new) = input().split()
	if old not in users:
		users[new] = old
	else:
		aux = users[old]
		users.pop(old)
		users[new] = aux
print(len(users))
for user in users:
	print(users[user], user)


(n, h) = (int(input()), {})
for i in range(n):
	(o, n) = input().split()
	h[n] = h.get(o, o)
	h.pop(o, None)
print(len(h))
for (n, o) in h.items():
	print(o, n)


numR = int(input())
dic = {}
for i in range(numR):
	(old, new) = input().split()
	if old not in dic:
		dic[new] = old
	else:
		aux = dic.pop(old)
		dic[new] = aux
print(len(dic))
for key in dic:
	print(dic[key], key)


import sys

class Conjunto:

	def __init__(self, x):
		self.representante = x
		self.elementos = [x]

	@property
	def ultimo(self):
		return self.elementos[len(self.elementos) - 1]

	def esta(self, y):
		if self.ultimo == y:
			return True
		return False

	def agregar(self, y):
		self.elementos.append(y)

def find_set(x, lista):
	for i in lista:
		if i.esta(x):
			return i
	return False

def union(conj, y):
	conj.agregar(y)

def main():
	cambios = sys.stdin.readline()
	conjuntos = []
	while True:
		linea = sys.stdin.readline()
		if not linea:
			print('{}\n'.format(len(conjuntos)))
			for i in conjuntos:
				print('{} {}\n'.format(i.representante, i.ultimo))
			break
		linea = linea.replace('\n', '')
		handles = linea.split(' ')
		old = handles[0]
		new = handles[1]
		existe = find_set(old, conjuntos)
		if not existe:
			nuevo = Conjunto(old)
			nuevo.agregar(new)
			conjuntos.append(nuevo)
		else:
			existe.agregar(new)
main()


inputList = []
n = int(input())
for i in range(n):
	tempList = list(input().split())
	inputList.append(tempList)
oldNames = []
newNames = []
for tempList in inputList:
	(oldName, newName) = (tempList[0], tempList[1])
	if oldName not in newNames:
		oldNames.append(oldName)
		newNames.append(newName)
	else:
		idx = newNames.index(oldName)
		newNames[idx] = newName
print(len(oldNames))
for (oldName, newName) in zip(oldNames, newNames):
	print(oldName, newName)


n = int(input())
name = {}
for i in range(n):
	x = input().split()
	if name.get(x[0]) == None:
		name[x[1]] = x[0]
	else:
		name[x[1]] = name.pop(x[0])
print(len(name))
for j in name:
	print(name.get(j), j)


changes = int(input())
logins = {}
for i in range(changes):
	(antigo, novo) = input().split()
	if antigo not in logins:
		logins[novo] = antigo
	else:
		aux = logins[antigo]
		logins[novo] = aux
		logins.pop(antigo)
print(len(logins))
for l in logins:
	relacao = str(logins[l]) + ' ' + str(l)
	print(relacao)


amount = int(input())
new_to_old = {}
for n in range(amount):
	(old, new) = input().split()
	if old in new_to_old:
		new_to_old[new] = new_to_old[old]
		del new_to_old[old]
	else:
		new_to_old[new] = old
print(len(new_to_old))
for (new, old) in new_to_old.items():
	print(old, new)


numreq = int(input())
startnames = []
names = []
for x in range(0, numreq):
	temp = input().split()
	if names.count(temp[0]) == 1:
		b = names.index(temp[0])
		names[b] = temp[1]
	else:
		startnames.append(temp[0])
		names.append(temp[1])
print(str(len(names)))
for y in range(0, len(names)):
	print(str(startnames[y]) + ' ' + str(names[y]))


import sys
input = sys.stdin.readline
names = {}
cnt = {}
for _ in range(int(input())):
	(old, new) = input().strip().split()
	names[old] = new
	cnt[old] = cnt.get(old, 0) + 1
	cnt[new] = cnt.get(new, 0) + 1
ans = []
for old in names:
	if cnt[old] == 1:
		new = names[old]
		while new in names:
			new = names[new]
		ans.append((old, new))
print(len(ans))
for i in ans:
	print(*i)


def main():
	q = int(input())
	names = {}
	for i in range(q):
		(old, new) = input().split(' ')
		found = False
		for (k, v) in names.items():
			if v == old:
				names[k] = new
				found = True
		if not found:
			names[old] = new
	print(len(names))
	for (k, v) in names.items():
		print(k, v)
main()


try:

	def add_cconnection(new_change, data):
		(old, new) = new_change.split(' ')
		value = data.get(old)
		if value:
			del data[old]
			data[new] = value
		else:
			data[new] = old
	num = int(input())
	i = 0
	data = {}
	while i < num:
		_input = input()
		add_cconnection(_input, data)
		i += 1
	results = list(data.items())
	results.reverse()
	print(len(results))
	for (new, old) in results:
		print(old, new)
except Exception as err:
	print(err)


__author__ = 'trunghieu11'
n = int(input())
m = dict()
for i in range(n):
	(x, y) = input().split()
	m[x] = y
	for s in m.keys():
		if m.get(s) == x:
			m.pop(x)
			m[s] = y
			break
print(len(m))
for s in m.keys():
	print(s, m[s])


q = int(input(), 10)
d = dict()
for i in range(q):
	(old, new) = input().split(' ')
	if old not in d:
		d[new] = old
	else:
		f = d[old]
		d.pop(old)
		d[new] = f
print(len(d))
for i in d:
	print(d[i], ' ', i)


val = int(input())
old = []
new = []
for i in range(val):
	x = input().split(' ')
	try:
		c = new.index(x[0])
		new[c] = x[1]
	except:
		old.append(x[0])
		new.append(x[1])
print(len(old))
for i in range(len(old)):
	print(old[i] + ' ' + new[i])


used = set()
n = int(input())
p_names = {}
for i in range(n):
	(cur_name, new_name) = input().split()
	if new_name not in used:
		used.add(new_name)
		if cur_name in used:
			used.remove(cur_name)
		if cur_name in p_names:
			p_names[new_name] = p_names[cur_name]
		else:
			p_names[new_name] = cur_name
print(len(used))
for name in used:
	print(p_names[name], name)


n = int(input())
roots = {}
mapping = {}
latest = {}
curr_mapping = 1
for i in range(n):
	(old, new) = input().split()
	if old not in mapping:
		roots[curr_mapping] = old
		mapping[old] = curr_mapping
		mapping[new] = curr_mapping
		curr_mapping = old
	else:
		mapping[new] = mapping[old]
	latest[mapping[old]] = new
print(len(roots))
for key in roots:
	print('%s %s' % (roots[key], latest[key]))


handles = {}
num_req = int(input())
for _ in range(num_req):
	(old, new) = input().split(' ')
	if new in handles.items():
		continue
	if old in handles.values():
		name = next(filter(lambda x: x[1] == old, handles.items()))[0]
		handles[name] = new
		continue
	handles[old] = new
print(len(handles))
for (k, v) in handles.items():
	print(f'{k} {v}')


n = int(input())
dic = {}
for i in range(n):
	achado = False
	(old, new) = input().split()
	for (k, v) in dic.items():
		if old == v:
			dic[k] = new
			achado = True
			break
	if not achado:
		dic[old] = new
print(len(dic))
for (k, v) in dic.items():
	print(k, v)


n = int(input())
users = []
while n > 0:
	request = input()
	old = request.split(' ')[0]
	new = request.split(' ')[1]
	i = -1
	p = None
	for u in users:
		i += 1
		if u[1] == old:
			p = i
			break
	if p == None:
		new_user = {}
		new_user[0] = old
		new_user[1] = new
		users.append(new_user)
	else:
		users[p][1] = new
	n -= 1
print(len(users))
for u in users:
	print(u[0] + ' ' + u[1])


n = int(input())
queries = []
names = []
f = []
l = []
for i in range(n):
	queries.append(input().split(' '))
for i in range(n):
	for j in range(2):
		if queries[i][j] in names:
			if j == 0:
				names.remove(queries[i][j])
				l[l.index(queries[i][j])] = queries[i][j - 1]
		else:
			names.append(queries[i][j])
			if j == 0:
				f.append(queries[i][j])
			elif queries[i][j] not in l:
				l.append(queries[i][j])
print(len(f))
for k in range(len(f)):
	print(f[k], l[k])


def handleInput():
	n = int(input())
	req_list = []
	for i in range(n):
		req = input()
		req = req.split(' ')
		req_list.append(req)
	return req_list

class User:

	def __init__(self, original_handle, current_handle):
		self.original = original_handle
		self.current = current_handle

def changing_handles(req_list):
	users = []
	nodes_current = []
	for req in req_list:
		exists = False
		for i in range(len(nodes_current)):
			if req[0] == nodes_current[i]:
				users[i].current = req[1]
				nodes_current[i] = req[1]
				exists = True
				break
		if not exists:
			new_user = User(req[0], req[1])
			users.append(new_user)
			nodes_current.append(req[1])
	print(len(users))
	for user in users:
		print(user.original + ' ' + user.current)
req_list = handleInput()
changing_handles(req_list)


_n = int(input())
_dict = {}
_new_names = {}
_seen = {}

def save_data():
	_input = input().split()
	_dict[_input[0]] = _input[1]

def update_count():
	return len(_new_names)

def change_names(name):
	new_key = _dict[name]
	if _dict.get(new_key) and (not _seen.get(new_key)):
		while _dict.get(new_key):
			_seen[new_key] = 1
			_dict[name] = _dict[new_key]
			new_key = _dict[new_key]
		_new_names[name] = _dict[name]
	elif not _seen.get(name):
		_new_names[name] = _dict[name]
for _ in range(_n):
	save_data()
list(map(lambda x: change_names(x), _dict))
print(update_count())
list(map(lambda key: print(key, _new_names[key]), _new_names))


ii = lambda : int(input())
mi = lambda : map(int, input().split())
li = lambda : list(mi())
si = lambda : input()
d = {}
for _ in range(ii()):
	q = input().split()
	try:
		d[q[1]] = d[q[0]]
		del d[q[0]]
	except KeyError:
		d[q[1]] = q[0]
print(len(d))
for (x, y) in d.items():
	print(y, x)


from collections import defaultdict

class DisjSet:

	def __init__(self, n):
		self.rank = defaultdict(lambda : 1)
		self.parent = defaultdict(lambda : None)

	def sol(self, a):
		if self.parent[a] == None:
			self.parent[a] = a

	def find(self, x):
		if self.parent[x] != x:
			self.parent[x] = self.find(self.parent[x])
		return self.parent[x]

	def Union(self, x, y):
		xset = self.find(x)
		yset = self.find(y)
		if xset == yset:
			return
		if self.rank[xset] < self.rank[yset]:
			self.parent[xset] = yset
		elif self.rank[xset] > self.rank[yset]:
			self.parent[yset] = xset
		else:
			self.parent[yset] = xset
			self.rank[xset] = self.rank[xset] + 1
n = int(input())
obj = DisjSet(n + 1)
temp = []
visited = defaultdict(lambda : None)
for p in range(n):
	(a, b) = input().split()
	temp.append([a, b])
	obj.sol(a)
	obj.sol(b)
	obj.Union(a, b)
cont = 0
ans = []
while temp:
	(a, b) = temp.pop()
	if visited[obj.find(b)] == None:
		ans.append([obj.find(b), b])
		cont += 1
		visited[obj.find(b)] = True
print(cont)
for val in ans:
	print(*val)


q = int(input())
m = []
for i in range(q):
	temp = list(input().split())
	cnt = 0
	for j in range(len(m)):
		if m[j][len(m[j]) - 1] == temp[0]:
			m[j].append(temp[1])
			cnt = 1
			break
	if not cnt:
		m.append([temp[0], temp[1]])
print(len(m))
for i in range(len(m)):
	print(m[i][0], m[i][len(m[i]) - 1])


n = int(input())
dt = {}
for i in range(n):
	a = input().split(' ')
	if a[0] in dt.keys():
		dt[a[1]] = dt[a[0]]
		del dt[a[0]]
	else:
		dt[a[1]] = a[0]
print(len(dt))
for (k, v) in dt.items():
	print(v + ' ' + k)


def main():
	usedMap = {}
	wordMap = {}
	q = int(input())
	while q > 0:
		(old, new) = (x for x in input().split())
		if old not in usedMap:
			usedMap[old] = old
			usedMap[new] = old
			wordMap[old] = new
		else:
			usedMap[new] = usedMap[old]
			wordMap[usedMap[old]] = new
		q -= 1
	count = len(wordMap)
	print(count)
	for (first, second) in wordMap.items():
		print(first, second)
main()


n = int(input())
dic = {}
for e in range(n):
	(old, new) = input().split()
	achou = False
	for (x, y) in dic.items():
		if x == old or y == old:
			dic[x] = new
			achou = True
			break
	if not achou:
		dic[old] = new
print(len(dic))
for (i, e) in dic.items():
	resp = i + ' ' + e
	print(resp)


n = int(input())
h = {}
for _ in range(n):
	(old, new) = input().split(' ')
	if not h.get(old):
		h[new] = old
	else:
		h[new] = h.pop(old)
print(len(h))
for (k, v) in h.items():
	print(v, ' ', k)


def main():
	lines = int(input())
	users = {}
	for i in range(lines):
		(old, new) = input().replace('\n', '').split(' ')
		if old in users:
			users[new] = users[old]
			users[new].append(new)
			del users[old]
		else:
			users[new] = [old, new]
	print(len(users))
	for (user, names) in users.items():
		print('{} {}'.format(names[0], names[-1]))
main()


n = int(input())
change = {}
origin = {}
for _ in range(n):
	(old, new) = input().split()
	o = origin.get(old, old)
	change[o] = new
	origin[new] = o
print(len(change.keys()))
for item in change.items():
	print(item[0], item[1])


def funcao(lista):
	mapa1 = {}
	mapa2 = {}
	for e in lista:
		if mapa1.get(e[0]) == None:
			if mapa2.get(e[0]) == None:
				mapa1[e[0]] = e[1]
				mapa2[e[1]] = e[0]
			else:
				valor = mapa2[e[0]]
				mapa1[valor] = e[1]
				mapa2.pop(e[0])
				mapa2[e[1]] = valor
	return mapa1
n = int(input())
lista = []
i = 0
while i < n:
	lista.append(input().split())
	i += 1
res = funcao(lista)
print(len(res))
for (k, v) in res.items():
	print(k + ' ' + v)


from collections import *
from sys import *

def inp():
	return map(str, input().split())

class graph:

	def __init__(self, gdict=None):
		if gdict is None:
			gdict = OrderedDict()
		self.gdict = gdict

	def edges(self):
		return self.find_edges()

	def find_edges(self):
		edges = []
		for node in self.gdict:
			for nxNode in self.gdict[node]:
				if {nxNode, node} not in edges:
					edges.append({node, nxNode})
		return edges

	def get_vertices(self):
		return list(self.gdict.keys())

	def add_vertix(self, node):
		if node not in self.gdict:
			self.gdict[node] = []

	def add_edge(self, node1, node2):
		if node1 not in self.gdict:
			self.gdict[node1] = []
		self.gdict[node1].append(node2)

	def dfsUtil(self, v, visit):
		visit[v] = 1
		if v in self.gdict:
			for i in self.gdict[v]:
				if visit[i] == 0:
					self.dfsUtil(i, visit)
		self.topsort.append(v)

	def dfs(self):
		visit = defaultdict(int)
		(self.cnt, self.topsort, self.all) = (0, [], [])
		for i in self.get_vertices():
			if visit[i] == 0:
				self.dfsUtil(i, visit)
				self.cnt += 1
				self.all.append(self.topsort)
				self.topsort = []
g = graph()
for i in range(int(input())):
	(u, v) = stdin.readline().split()
	g.add_edge(u, v)
g.dfs()
print(g.cnt)
ans = ''
for i in g.all:
	ans += str(i[-1]) + ' ' + str(i[0]) + '\n'
stdout.write(ans)


h = {}
for i in range(int(input())):
	(h1, h2) = input().split()
	if h1 in h:
		h[h2] = h[h1]
		del h[h1]
	else:
		h[h2] = h1
print(len(h))
for k in h:
	print(h[k], k)


n = int(input())
d = {}
for i in range(n):
	(a, b) = input().split()
	if a in d:
		c = d.pop(a)
		d[b] = c
	else:
		d[b] = a
print(len(d))
for i in d:
	print(d[i], i)


import sys
import math
import collections
import heapq
input = sys.stdin.readline
n = int(input())
d = {}
l = []
for i in range(n):
	(s1, s2) = (i for i in input().split())
	d[s1] = s2
for i in d:
	if d[i] != -1:
		k = d[i]
		k1 = i
		while k in d:
			d[k1] = -1
			k1 = k
			k = d[k]
		l.append([i, k])
		d[k1] = -1
c = len(l)
print(c)
for i in range(c):
	print(*l[i])


def main():
	n = int(input())
	Handels = []
	for i in range(n):
		rdl = input().split()
		if not rdl[0] in Handels:
			Handels.append(rdl[0])
			Handels.append(rdl[1])
		else:
			ind = Handels.index(rdl[0])
			Handels[ind] = rdl[1]
	i = 0
	print(int(len(Handels) / 2))
	while i < len(Handels):
		print(Handels[i], Handels[i + 1])
		i += 2
main()


n = int(input())
mapa = {}
for i in range(n):
	nomes = input().split()
	old = nomes[0]
	new = nomes[1]
	if mapa.get(old) != None:
		aux = mapa[old]
		mapa.pop(old)
		mapa[new] = aux
	else:
		mapa[new] = old
print(len(mapa.values()))
for i in mapa.keys():
	print(mapa[i], i)


users = dict()
nombres = list()
cant = input()
handles = []

class Handle:

	def __init__(self, old):
		self.old = old
		self.new = None

	def actualizar(self, new):
		self.new = new
for i in range(int(cant)):
	nombre = input().split(' ')
	if nombre[0] not in nombres:
		nombres.append(nombre[0])
		u = Handle(nombre[0])
		handles.append(u)
		if nombre[1] not in nombres:
			u.actualizar(nombre[1])
			nombres.append(nombre[1])
	elif nombre[1] not in nombres:
		nombres.append(nombre[1])
		for i in handles:
			if i.new == nombre[0]:
				i.actualizar(nombre[1])
				handles.remove(i)
				handles.append(i)
				break
print(len(handles))
a = len(handles) - 1
while a >= 0:
	print('{} {}'.format(handles[a].old, handles[a].new))
	a -= 1


q = int(input())
logs = [input().split() for _ in range(q)]
origin = []
now = []
cnt = 0
for (old, new) in logs:
	if old not in now:
		origin.append(old)
		now.append(new)
		cnt += 1
	else:
		now[now.index(old)] = new
print(cnt)
for i in range(len(now)):
	print(origin[i], now[i])


n = int(input())
inputs = {}
outputs = {}
for i in range(n):
	(old, new) = [x for x in input().split()]
	inputs[old] = new
for i in list(inputs.keys()):
	if i not in list(inputs.values()):
		new = inputs[i]
		while new in list(inputs.keys()):
			old = new
			new = inputs[old]
		outputs[i] = new
print(len(outputs))
for i in list(outputs.keys()):
	print(i, outputs[i])


n = int(input())
d = {}
for i in range(n):
	(x, y) = input().split()
	d[x] = y
	for i in d.keys():
		if d[i] == x:
			d.pop(x)
			d[i] = y
			break
print(len(d))
for i in d.keys():
	print(i, d[i])


n = int(input())
dct = {}
for _ in range(n):
	(o, n) = input().split()
	f = False
	for (k, v) in dct.items():
		if v == o:
			dct[k] = n
			f = True
			break
	if not f:
		dct[o] = n
print(len(dct))
for x in dct.items():
	print(*x)


amount = int(input())
old_to_new = {}
new_to_old = {}
for n in range(amount):
	(old, new) = input().split()
	if old not in new_to_old:
		old_to_new[old] = new
		new_to_old[new] = old
	else:
		new_to_old[new] = new_to_old[old]
		old_to_new[new_to_old[old]] = new
print(len(old_to_new))
for (old, new) in old_to_new.items():
	print(old, new)


d = {}
for i in range(int(input())):
	(n, m) = map(str, input().split())
	if n in d.values():
		for i in d.items():
			if i[1] == n:
				d[i[0]] = m
	else:
		d[n] = m
print(len(d))
for i in d:
	print(i + ' ' + d[i])


n = int(input())
a = dict()
for i in range(n):
	a.update({map(str, input().split())})
k = 0
b = dict()
for i in a:
	if i not in a.values():
		j = a[i]
		while j in a.keys():
			j = a[j]
		k += 1
		b.update({i: j})
print(k)
for i in b:
	print(i, b[i])


t = int(input())
dic = dict()
for i in range(t):
	(a, b) = map(str, input().split())
	if a in dic:
		dic[b] = dic[a]
		dic.pop(a)
	else:
		dic[b] = a
print(len(dic))
for i in dic:
	print(dic[i], i)


n = int(input())
b_a = {}
for n_ in range(n):
	(b, a) = input().split()
	replaced = False
	for k in b_a:
		if b_a[k] == b:
			b_a[k] = a
			replaced = True
	if not replaced:
		b_a[b] = a
print(len(b_a))
for k in b_a:
	print(k + ' ' + b_a[k])


dic = {}
n = int(input())
while n > 0:
	alreadyInputted = False
	change = input().split()
	for (firstName, changedName) in dic.items():
		if change[0] == changedName:
			dic[firstName] = change[1]
			alreadyInputted = True
	if not alreadyInputted:
		dic[change[0]] = change[1]
	n -= 1
print(len(dic))
for (n1, n2) in dic.items():
	print(n1 + ' ' + n2)


num = int(input())
(old, new) = ([], [])
ans = {}
for i in range(0, num):
	(t1, t2) = input().split()
	if t1 in ans.keys():
		ans[t2] = ans[t1]
		del ans[t1]
	else:
		ans[t2] = t1
print(len(ans))
for k in ans:
	print(ans[k], k)


n = int(input())
First = []
Second = []
for i in range(n):
	x = input().split(' ')
	First.append(x[0])
	Second.append(x[1])
Names = []
while First != [] or Second != []:
	FirstName = First[0]
	SecondName = Second[0]
	try:
		while True:
			idx = First.index(SecondName)
			SecondName = Second[idx]
			Second.pop(idx)
			First.pop(idx)
	except:
		pass
	Second.pop(0)
	First.pop(0)
	Names.append([FirstName, SecondName])
print(len(Names))
for (i, j) in Names:
	print(i, j)


n = int(input())
d = {}
for i in range(n):
	a = input().split()
	d[a[0]] = a[1]
a = []
for i in d.keys():
	l = i
	while l in d:
		t = d[l]
		d[l] = None
		l = t
	if l != None:
		a.append(str(i) + ' ' + str(l))
print(len(a))
for i in a:
	print(i)


d = {}
g = {}
for _ in range(int(input())):
	(a, b) = map(str, input().split())
	g[a] = b
	for i in g.keys():
		if g.get(i) == a:
			g.pop(a)
			g[i] = b
			break
print(len(g.keys()))
for (i, j) in g.items():
	print(i, j)


n = int(input())
a = []
for i in range(n):
	b = input().split()
	if b[0] in a:
		ind = a.index(b[0])
		a.insert(ind, b[1])
		a.pop(ind + 1)
	else:
		a.append(b[0])
		a.append(b[1])
print(len(a) // 2)
for i in range(len(a) // 2):
	print(*a[:2])
	a = a[2:]


par = []
count = 0

def makeSet(n):
	global par
	for i in range(n + 1):
		par.append(i)

def find(a):
	global par
	if a == par[a]:
		return a
	par[a] = find(par[a])
	return par[a]

def union(a, b):
	global par
	par[find(a)] = find(b)

def main():
	global par, count
	n = int(input())
	makeSet(10000)
	m = {}
	og = set()
	for i in range(n):
		(old, new) = input().split(' ')
		if old not in m:
			m[old] = count
			count += 1
			og.add(old)
		if new not in m:
			m[new] = count
			count += 1
		union(m[old], m[new])
	rev = {}
	for i in m:
		rev[m[i]] = i
	print(len(og))
	for o in og:
		print(o, rev[find(m[o])])
main()


def avaliaRequest(request, requests, trocados):
	if not request[1] in requests or not request[1] in trocados:
		if request[0] in trocados:
			requests[trocados[request[0]]] = request[1]
			trocados[request[1]] = trocados[request[0]]
		else:
			requests[request[0]] = request[1]
			trocados[request[1]] = request[0]
numResquests = int(input())
requests = {}
trocados = {}
for i in range(numResquests):
	request = input().split()
	avaliaRequest(request, requests, trocados)
print(len(requests))
for i in requests.keys():
	print('%s %s' % (i, requests[i]))


handles = {}
q = int(input())
for i in range(q):
	(old, new) = map(str, input().split())
	if old not in handles.values():
		handles[old] = new
	else:
		for j in handles:
			if handles[j] == old:
				handles[j] = new
print(len(handles))
for i in handles:
	print(i, handles[i])


n = int(input())
conjuntos = {}
for i in range(n):
	(old, new) = input().split(' ')
	existe = False
	for (representante, conjunto) in conjuntos.items():
		if old in conjunto:
			existe = True
			conjuntos[representante].append(new)
			break
	if not existe:
		conjuntos[old] = [old, new]
print(len(conjuntos))
for elem in conjuntos.values():
	print(elem[0], elem[-1])


import sys
n_inputs = int(sys.stdin.readline())
input_ = [line.strip().split() for line in sys.stdin.readlines()]
flatten_input = [item for collection in input_ for item in collection]
unique_handles = list(dict.fromkeys(flatten_input))
changes = []
for (old, new) in input_:
	trigger = False
	for index in range(len(changes)):
		(original, last) = changes[index]
		if last == old:
			changes[index] = [original, new]
			trigger = True
			break
	if not trigger:
		changes.append([old, new])
print(len(changes))
for (original, last) in changes:
	print(original, last)


def input_split(f):
	return list(map(f, input().split()))

def main():
	n = int(input())
	vals = []
	for i in range(n):
		vals.append(tuple(input_split(str)))
	res = {}
	for (o, n) in vals:
		if o in res:
			res[n] = res[o]
			del res[o]
		else:
			res[n] = o
	print(len(res))
	for k in res:
		print('{} {}'.format(res[k], k))

def __starting_point():
	main()
__starting_point()


def get_ints(string):
	return list(map(int, string.split()))

def get_input():
	a = get_ints(input())
	return a

def main():
	q = int(input())
	data = {}
	for _ in range(q):
		(old, new) = input().split()
		if old in data:
			data[new] = data[old]
			del data[old]
		else:
			data[new] = old
	print(len(data))
	for new in data:
		print(data[new], new)
main()


requests = int(input())
iniciais = []
finais = []
for _ in range(requests):
	(anterior, novo) = input().split()
	if not anterior in finais:
		iniciais.append(anterior)
		finais.append(novo)
	else:
		finais[finais.index(anterior)] = novo
print(len(iniciais))
for i in range(len(iniciais)):
	print(iniciais[i], finais[i])


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect, insort
from time import perf_counter
from fractions import Fraction
import copy
from copy import deepcopy
import time
starttime = time.time()
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def L():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
try:
	sys.stdin = open('input.txt', 'r')
except:
	pass

def pmat(A):
	for ele in A:
		print(*ele, end='\n')
q = L()[0]
d = {}
s = set()
for i in range(q):
	(x, y) = sl()
	d[x] = y
	s.add(x)
A = []
for ele in d:
	if ele not in s:
		continue
	s.remove(ele)
	y = d[ele]
	while y in d and y in s:
		s.remove(y)
		y = d[y]
	A.append([ele, y])
print(len(A))
for ele in A:
	print(*ele)
endtime = time.time()


n = int(input())
nombres = {}
for _ in range(n):
	a = input().split()
	nombres[a[0]] = a[1]
resultado = []
vistos = []
for ini in nombres:
	if ini in vistos:
		continue
	vistos.append(ini)
	handle = []
	handle.append(ini)
	final = nombres[ini]
	while final in nombres.keys():
		vistos.append(final)
		final = nombres[final]
	handle.append(final)
	resultado.append(handle)
print(len(resultado))
for i in resultado:
	print(' '.join(i))


n = int(input())
d = {}
for j in range(n):
	l = list(map(str, input().split()))
	if l[0] in d:
		d[l[1]] = d[l[0]]
		d.pop(l[0])
	else:
		d[l[1]] = l[0]
print(len(d))
for i in d:
	print(d[i], i)


handles_num = int(input())
users = {}
for i in range(handles_num):
	change = input()
	old = change.split(' ')[0]
	new = change.split(' ')[1]
	if old in users:
		users[new] = users[old]
		users.pop(old)
	else:
		users[new] = old
print(len(users))
for (n, o) in users.items():
	print(o + ' ' + n)


n = int(input())
dir = {}
for z in range(n):
	(old, new) = input().split()
	flag = True
	for ii in dir:
		if dir[ii] == old:
			dir[ii] = new
			flag = False
			break
	if flag:
		dir[old] = new
print(len(dir))
for ii in dir:
	print(ii, dir[ii])


n = int(input())
parent = {}
child = {}
for i in range(n):
	(x, y) = input().split()
	if child.get(x, '') != '':
		parent[child[x]] = y
		child[y] = child[x]
	else:
		parent[x] = y
		child[y] = x
print(len(parent))
for x in parent:
	print(x, parent[x])


n = int(input())
dict1 = {}
dict2 = {}
dict3 = {}
for i in range(n):
	(str1, str2) = map(str, input().split())
	dict1[str1] = str2
	dict2[str1] = 0
for i in dict1:
	key = i
	if dict2[i] != 1:
		while True:
			key = dict1[key]
			if key in dict1.keys():
				dict2[key] = 1
			else:
				break
		dict3[i] = key
print(len(dict3))
for i in dict3:
	print(i, dict3[i])


def main():
	q = int(input())
	requests = [input().split() for _ in range(q)]
	mappings = {}
	seen = set()
	old_users = set()
	out = []
	for (old, new) in requests:
		if old not in seen:
			old_users.add(old)
		seen |= {old, new}
		mappings[old] = new
	for old in old_users:
		new = old
		while new in mappings:
			new = mappings[new]
		out.append('{} {}'.format(old, new))
	print(len(out))
	print('\n'.join(out))
main()


class User:

	def __init__(self, name, id):
		self.first_name = name
		self.last_name = None
		self.id = id

	def __repr__(self):
		return '{} - {}\n'.format(self.first_name, self.last_name)
used = set()
usernames = dict()
users = dict()
N = int(input())
i = 0
n_users = 0
while i < N:
	inp = input().strip('\n').split(' ')
	old = inp[0]
	new = inp[1]
	if new in used:
		continue
	elif old not in usernames:
		new_user = User(old, n_users)
		users[n_users] = new_user
		usernames[old] = n_users
		usernames[new] = n_users
		new_user.last_name = new
		used.add(new)
		used.add(old)
		n_users += 1
	else:
		user_id = usernames[old]
		users[user_id].last_name = new
		usernames[new] = user_id
		used.add(new)
	i += 1
print(len(users))
for u in users:
	text = '{} {}'.format(users[u].first_name, users[u].last_name)
	print(text)


n = int(input())
changes = {}
for i in range(0, n):
	(old, new) = input().split()
	first = changes.get(old)
	if first:
		changes.pop(old)
		changes[new] = first
	else:
		changes[new] = old
print(len(changes))
for (new, old) in changes.items():
	print(old, new)


n = int(input())
dic = []
values = []
dic_inverse = []
for i in range(n):
	string = input().split()
	old = string[0]
	new = string[1]
	values.append(new)
	dic.append((old, new))
	dic_inverse.append((new, old))
dic = dict(dic)
dic_inverse = dict(dic_inverse)

def getKey(value):
	return dic_inverse[value]
for i in range(n - 1, -1, -1):
	value = values[i]
	if value in dic:
		key = getKey(value)
		dic[key] = dic[value]
		dic.pop(value)
print(len(dic))
for key in dic:
	print(key, ' ', dic[key])


def main():
	ii = lambda : int(input())
	d = {}
	for _ in range(ii()):
		q = input().split()
		try:
			d[q[1]] = d[q[0]]
			del d[q[0]]
		except KeyError:
			d[q[1]] = q[0]
	print(len(d))
	for x in d.keys():
		print(d[x], x)
main()


a = int(input(''))
inputs = []
for value in range(a):
	a = input('').split(' ')
	inputs.append(a)
handles = []
guFat = []
for value in inputs:
	string = value[0] + ' ' + value[1]
	if value[0] not in handles:
		handles.append(value[1])
		guFat.append(string)
	if value[0] in handles:
		handles.remove(value[0])
		handles.append(value[1])
		for k in guFat:
			impor = k.split(' ')
			if value[0] == impor[1]:
				guFat.remove(k)
				guFat.append(impor[0] + ' ' + value[1])
print(len(handles))
for value in guFat:
	print(value)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

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

def insr():
	s = input()
	return list(s[:len(s) - 1])

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

def solve(arr):
	r = []
	for (i, a) in enumerate(arr):
		if a is None:
			continue
		first = arr[i]
		arr[i] = None
		last = first
		for j in range(i + 1, n):
			if arr[j] is not None and arr[j][0] == last[1]:
				last = arr[j]
				arr[j] = None
		r.append([first[0], last[1]])
	return r
n = inp()
arr = []
for i in range(n):
	s = input()[:-1].split(' ')
	arr.append(s)
res = solve(arr)
print(len(res))
for i in range(len(res)):
	print(' '.join(res[i]))


cantidad = int(input())
combis = []
for i in range(cantidad):
	linea = input().split(' ')
	listo = False
	for j in combis:
		if linea[0] == j[0]:
			j.insert(0, linea[1])
			listo = True
	if not listo:
		combis.append([linea[1], linea[0]])
largo_final = len(combis)
print(largo_final)
for i in range(largo_final):
	print('{} {}'.format(combis[i][-1], combis[i][0]))


z = int(input())
lama = []
baru = []
for i in range(z):
	y = input().split()
	lama.append(y[0])
	baru.append(y[1])
status = True
while status:
	status = False
	arr = []
	for i in range(len(lama)):
		for j in range(len(baru) - 1, -1, -1):
			if lama[i] == baru[j]:
				arr.append(lama[j])
				arr.append(baru[i])
				lama[j] = -1
				baru[i] = -1
				lama[i] = -1
				baru[j] = -1
				break
	for i in range(len(lama)):
		if lama[i] != -1:
			arr.append(lama[i])
			arr.append(baru[i])
	for i in baru:
		if i in lama:
			status = True
			break
	if status:
		baru = []
		lama = []
		for i in range(0, len(arr), 2):
			lama.append(arr[i])
		for i in range(1, len(arr), 2):
			baru.append(arr[i])
print(len(arr) // 2)
for i in range(len(arr)):
	if (i + 1) % 2 == 0:
		print(arr[i])
	else:
		print(arr[i], end=' ')


t = int(input())
d = {}
for i in range(t):
	(old, new) = input().split()
	d[new] = d.get(old, old)
	d.pop(old, None)
print(len(d))
for (a, b) in d.items():
	print(b, a)


dic = {}
for i in range(int(input())):
	(old, new) = input().split()
	found = False
	for el in dic:
		if old in dic[el]:
			found = True
			dic[el].append(new)
	if not found:
		dic[old] = [new]
print(len(dic))
for i in dic:
	print(i, dic[i][-1])


import sys
try:
	(sys.stdin, sys.stdout) = (open('in.txt', 'r'), open('out.txt', 'w'))
except:
	pass
ii1 = lambda : int(sys.stdin.readline().strip())
is1 = lambda : sys.stdin.readline().strip()
iia = lambda : list(map(int, sys.stdin.readline().strip().split()))
isa = lambda : sys.stdin.readline().strip().split()
mod = int(1000000000.0 + 7)
from collections import *
from math import *

def fun():
	n = ii1()
	parent = [-1] * n
	dic = {}
	arr = []
	for i in range(n):
		(old, new) = isa()
		arr.append([old, new])
		dic[old] = new
	res = []
	for (old, new) in arr:
		flag = False
		if old in dic:
			flag = True
			while new in dic:
				t = new
				new = dic[t]
				del dic[t]
		if flag:
			res.append([old, new])
	print(len(res))
	for (a, b) in res:
		print(a, b)
fun()


quests = int(input())
pairs = []
for i in range(quests):
	checker = True
	(old_name, new_name) = input().split()
	for j in range(len(pairs)):
		if old_name == pairs[j][1]:
			pairs[j][1] = new_name
			checker = False
			break
	if checker:
		pairs.append([old_name, new_name])
print(str(len(pairs)) + '\n' + '\n'.join([' '.join(i) for i in pairs]))


n = int(input())
ls = []
ll = []
oo = []
counter = 0
for i in range(n):
	ls.append(input().split())
for i in range(n):
	index = i
	if ls[i][0] not in ll:
		while True:
			start = i + 1
			check = True
			for j in range(start, n):
				if ls[j][0] == ls[index][1]:
					index = j
					start = j + 1
					ll.append(ls[j][0])
					check = False
			if start >= n or check:
				oo.append([ls[i][0], ls[index][1]])
				counter += 1
				break
print(counter)
for i in range(counter):
	print(*oo[i])


n = int(input())
d = {}
for _ in range(n):
	(old, new) = input().strip().split()
	if old not in d.keys():
		for i in d:
			if d[i] == old:
				d[i] = new
				break
		else:
			d[old] = new
	else:
		d[old] = new
print(len(d))
for i in d:
	print(i, d[i])


def handle_names():
	current_names = dict()
	library = dict()
	n = int(input())
	for _ in range(n):
		(old, new) = input().split()
		if old not in library:
			current_names[old] = [new]
			library[new] = old
			library[old] = old
		else:
			library[new] = library[old]
			parent = library[old]
			current_names[parent].append(new)
	return current_names
current_names = handle_names()
print(len(current_names))
for name in current_names:
	print(name, current_names[name][-1])


def Get_new(Dic, name):
	while Dic[name] != name:
		name = Dic[name]
	return name
dic = {}
orig = []
for _ in range(int(input())):
	(old, new) = input().split()
	if old not in dic:
		dic[old] = new
		dic[new] = new
		orig.append(old)
	else:
		dic[old] = new
		dic[new] = new
print(len(orig))
for name in orig:
	if dic[name] != name:
		print(name, Get_new(dic, name))


n = int(input())
i = 0
graph = {}
while i < n:
	pair = input().strip()
	(head, tail) = pair.split(' ')
	graph[head] = tail
	i += 1

def merge(dicc, key1, key2):
	new_dict = dict(dicc)
	new_set = dicc[key2]
	del new_dict[key2]
	new_dict[key1] = new_set
	return new_dict
while True:
	merged = False
	for head in graph:
		el = graph[head]
		if el in graph:
			graph = merge(graph, head, el)
			merged = True
			break
	if not merged:
		break
print(len(graph))
for key in graph:
	el = graph[key]
	print('{} {}'.format(key, el))


def s():
	n = int(input())
	m = dict()
	r = []
	for _ in range(n):
		a = input().split(' ')
		i = len(r)
		if a[0] not in m:
			r.append(a)
			m[a[0]] = i
		else:
			i = m[a[0]]
			del m[a[0]]
			r[i][1] = a[1]
		m[r[i][1]] = i
	print(len(r))
	print(*[' '.join(i) for i in r], sep='\n')
s()


n = int(input())
inv = dict()
for i in range(n):
	(a, b) = input().split()
	if a not in inv:
		inv[b] = a
	else:
		inv[b] = inv[a]
		inv.pop(a)
print(len(inv))
for (a, b) in inv.items():
	print(b, a)


q = int(input())
users = {}
for i in range(q):
	(old, new) = map(str, input().split())
	if old in users:
		users[new] = users[old]
		users.pop(old)
	else:
		users[new] = old
print(len(users.items()))
for u in users:
	print(users[u], u)


x = int(input())
words1 = []
words2 = []
for i in range(x):
	(line1, line2) = input().split()
	if line1 not in words2:
		words1.append(line1)
		words2.append(line2)
	else:
		index = words2.index(line1)
		words2[index] = line2
s = len(words1)
print(s)
for i in range(s):
	print(words1[i], words2[i])


q = int(input())
requests = {}
for i in range(q):
	(old, new) = map(str, input().split())
	if old not in requests and old not in requests.values():
		requests[new] = old
	elif old in requests:
		requests[new] = requests.pop(old)
print(len(requests))
for i in requests:
	print(requests[i], i)


handles = {}
q = int(input())
for _ in range(q):
	(old, new) = input().split()
	old = handles.pop(old, old)
	handles.setdefault(new, old)
print(len(handles))
print('\n'.join((' '.join([old, new]) for (new, old) in handles.items())))


from sys import stdin
__author__ = 'artyom'

def next_line():
	return stdin.readline().strip()
h = {}
for i in range(int(next_line())):
	(a, b) = next_line().split()
	if a in h:
		h[b] = h[a]
		del h[a]
	else:
		h[b] = a
print(len(h))
for key in h:
	print(h[key], key, sep=' ')


fast = lambda : stdin.readline().strip()
zzz = lambda : [int(i) for i in fast().split()]
(z, zz) = (input, lambda : list(map(int, z().split())))
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from re import *
from sys import *
from math import *
from heapq import *
from queue import *
from bisect import *
from string import *
from itertools import *
from collections import *
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from collections import Counter as cc
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2 or x == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = 1
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)

def output(answer):
	stdout.write(str(answer))
n = int(z())
que = {}
for i in range(n):
	(a, b) = fast().split()
	try:
		que[a]
	except:
		que[a] = ''
	que[a] = b
ans1 = []
ans2 = []

def solve(name):
	try:
		t = que[name]
		solve(t)
		vis.append(name)
	except:
		ans2.append(name)
		vis.append(name)
vis = []
lst = list(que)
for i in lst:
	if i in vis:
		continue
	ans1.append(i)
	solve(i)
	vis.append(i)
print(len(ans1))
for i in zip(ans1, ans2):
	print(*i)


n = int(input())
d = {}
for i in range(n):
	(old, new) = input().split()
	if old in d:
		d[new] = d[old]
		d.pop(old)
	else:
		d[new] = old
print(len(d))
for i in d:
	print(d[i], i)


n = int(input())
cnt = 0
name = []
for i in range(n):
	name.append(input().split())
newdic = {}

def get_keys(d, value):
	return [k for (k, v) in d.items() if v == value]
for (old, new) in name:
	if old not in newdic:
		if old in newdic.values():
			keys = get_keys(newdic, old)
			for k in keys:
				newdic[k] = new
		else:
			newdic[old] = new
	elif old in newdic.values():
		keys = get_keys(newdic, old)
		for k in keys:
			newdic[k] = new
print(len(newdic))
for (x, y) in newdic.items():
	print(x, y)


q = int(input())
D = {}
for i in range(q):
	(x, y) = input().split()
	if x in D:
		D[y] = D[x]
		D.pop(x)
	else:
		D[y] = x
print(len(D))
for i in D:
	print(D[i], i)


from sys import stdout, stdin

def effective_changes(changes):
	old_names = dict()
	new_names = dict()
	result = []
	for change in changes:
		if change[0] not in new_names:
			old_names[change[0]] = change[1]
			new_names[change[1]] = change[0]
		else:
			old_names[new_names[change[0]]] = change[1]
			new_names[change[1]] = new_names[change[0]]
			new_names.pop(change[0], None)
	for key in old_names.keys():
		result.append([key, old_names[key]])
	return result

def print_changes(changes):
	print(len(changes))
	for change in changes:
		print(change[0] + ' ' + change[1])
	return None
n = int(stdin.readline().replace('\n', ''))
changes = []
for i in range(n):
	(name, new_name) = stdin.readline().replace('\n', '').split(' ')
	changes.append([name, new_name])
changes = effective_changes(changes)
print_changes(changes)


n = int(input())
arr = []
for i in range(n):
	s = input()
	a = s.split(' ')
	arr.append(a)
array = [['' for col in range(n)] for row in range(2)]
a1 = [[] for k in range(n)]
a1[0].append(arr[0][0])
a1[0].append(arr[0][1])
a2 = []
a2.append(arr[0][1])
p = 1
while p < n:
	a2.append(arr[p][1])
	if arr[p][0] in a2:
		c = 0
		while c < p:
			if arr[p][0] in a1[c]:
				break
			else:
				c = c + 1
		a1[c].append(arr[p][1])
		p = p + 1
	else:
		a1[p].append(arr[p][0])
		a1[p].append(arr[p][1])
		p = p + 1
count = 0
for z in range(len(a1)):
	if a1[z] != []:
		count += 1
print(count)
for z in range(len(a1)):
	if a1[z] != []:
		print(a1[z][0] + ' ' + a1[z][-1])


__author__ = 'Rakshak.R.Hegde'
changes = []
valids = []
for t in range(int(input())):
	(a, b) = input().split()
	do = True
	for i in changes:
		if b == (i[0] or i[1]):
			do = False
			break
		if i[1] == a:
			i[1] = b
			break
	else:
		if do:
			changes.append([a, b])
print(len(changes))
for change in changes:
	print(change[0], change[1])


q = int(input())

class Node:

	def __init__(self, v):
		self.val = v
		self.next = None

class LinkedList:

	def __init__(self):
		self.start = None
		self.end = None

	def is_empty(self):
		return self.start == None

	def append(self, v):
		if self.is_empty():
			self.start = Node(v)
			self.end = self.start
		else:
			self.end.next = Node(v)
			self.end = self.end.next
my_dict = []
for i in range(q):
	(old, new) = list(input().split())
	if my_dict == []:
		first_list = LinkedList()
		first_list.append(old)
		first_list.append(new)
		my_dict.append(first_list)
	else:
		flg = 0
		for i in range(len(my_dict)):
			if my_dict[i].end.val == old:
				my_dict[i].append(new)
				flg = 1
		if flg == 0:
			new_list = LinkedList()
			new_list.append(old)
			new_list.append(new)
			my_dict.append(new_list)
print(len(my_dict))
for i in range(len(my_dict)):
	print(my_dict[i].start.val, my_dict[i].end.val)


n = int(input())
A = {}
B = {}
k = 0
for i in range(n):
	s = input().split()
	if s[0] not in A:
		B[s[0]] = s[1]
		A[s[1]] = s[0]
		k += 1
	else:
		j = A[s[0]]
		A[s[1]] = j
		B[j] = s[1]
print(k)
for i in B:
	print(i, B[i])


q = int(input())
nomes = {}
for i in range(q):
	(old, new) = map(str, input().split())
	if old in nomes.values():
		for (k, j) in nomes.items():
			if j == old:
				nomes[k] = new
				break
	else:
		nomes[old] = new
print(len(nomes))
for (i, j) in nomes.items():
	print(i, j)


from math import sqrt, ceil

def mi():
	return map(int, input().split())

def li():
	return list(mi())

def ii():
	return int(input())

def si():
	return input()
rank = []

def find(x, parent):
	if x == parent[x]:
		return x
	parent[x] = find(parent[x], parent)
	return parent[x]

def union(x, y, parent):
	x1 = find(x, parent)
	y1 = find(y, parent)
	if x1 == y1:
		return
	if rank[x1] >= rank[y1]:
		parent[y] = x1
		rank[x1] += 1
	else:
		parent[x] = y1
		rank[y1] += 1

def main():
	q = ii()
	old = {}
	a = []
	for i in range(q):
		(s1, s2) = map(str, input().split())
		old[s2] = s1
		a.append(s2)
	for i in range(q):
		for j in range(q):
			if a[j] == old[a[j]]:
				continue
			if a[i] == old[a[j]]:
				(old[a[j]], old[a[i]]) = (old[a[i]], old[a[j]])
				(a[i], a[j]) = (a[j], a[i])
	b = []
	for i in old.keys():
		if i != old[i]:
			b.append(i)
	print(len(b))
	for i in b:
		print(old[i], i)
main()


import pdb
n = int(input())
p = {}
for i in range(n):
	(a, b) = input().split()
	if a not in p:
		p[b] = a
	else:
		temp = p[a]
		p[b] = temp
		del p[a]
print(len(p))
for e in p:
	print(p[e], e)


def tabe(a, b):
	i = 0
	while i < len(a):
		if b == a[i]:
			return i
		i = i + 1
	return -1
a = int(input())
l = []
ll = []
i = 0
while i < a:
	b = list(input().split())
	c = b[0]
	d = b[1]
	x = tabe(ll, c)
	if x == -1:
		l.append(c)
		ll.append(d)
	else:
		ll[x] = d
	i = i + 1
i = 0
print(len(l))
while i < len(l):
	print(l[i], ll[i])
	i = i + 1


n = int(input().strip())
names = {}
c = 0
for i in range(n):
	(old, new) = input().strip().split()
	found = False
	for name in names:
		if names[name] == old:
			names[name] = new
			found = True
			break
	if not found:
		names[old] = new
		c += 1
print(c)
for name in names:
	print(name, names[name])


from sys import stdin
input = stdin.readline
import math
import sys
from collections import defaultdict
sys.setrecursionlimit(10 ** 4)

def lowbit(n):
	return n - (n & n - 1)
n = int(input().strip())
name = dict()
change = dict()
for _ in range(n):
	(first, second) = list(input().split())
	if first not in change and first not in name:
		name[first] = second
		change[second] = first
	elif first in change:
		temp = change[first]
		name[change[first]] = second
		change[second] = temp
print(len(name))
for i in name:
	print(i, name[i])


import math, string, itertools, fractions, heapq, collections, re, array, bisect
from itertools import chain, dropwhile, permutations, combinations
from collections import defaultdict

def main2(n, old, new):
	orig = []
	visited = []
	o = []
	l = []
	for (i, j) in zip(old, new):
		if i not in visited:
			orig.append([i, j])
			visited.append(j)
		else:
			for (k, v) in enumerate(orig):
				if i == v[-1]:
					orig[k].append(j)
					visited.append(j)
					continue
	print(len(orig))
	for v in orig:
		print(v[0], ' ', v[-1])

def main(n, old, new):
	o = []
	l = []
	for (i, j) in zip(old, new):
		if i not in l:
			o.append(i)
			l.append(j)
		else:
			k = l.index(i)
			l[k] = j
	print(len(o))
	for (x, y) in zip(o, l):
		print(x, y)

def main_input(info=0):
	n = int(input())
	(l, r) = (list(range(n)), list(range(n)))
	for i in range(n):
		(l[i], r[i]) = input().split()
	main(n, l, r)

def __starting_point():
	main_input()
__starting_point()


d = {}
sources = set()
count = int(input())
for i in range(count):
	(old, new_) = input().split()
	if old not in d:
		sources.add(old)
	d[old] = new_
	d[new_] = None
buf = []
total = 0
for el in sources:
	cur = d[el]
	while True:
		if d[cur] == None:
			total += 1
			buf.append('{} {}'.format(el, cur))
			break
		else:
			cur = d[cur]
print(total)
print('\n'.join(buf))


dic = {}
s = int(input())
i = 0
while i < s:
	x = input()
	h = x.split()
	if h[0] in dic:
		dic[h[1]] = dic.pop(h[0])
	else:
		dic[h[1]] = h[0]
	i += 1
print(len(dic))
for x in dic:
	print(dic[x] + ' ' + x)


n = int(input())
a = []
for i in range(n):
	a.append(input().split())
d = {}
for i in a:
	if i[0] in d:
		d[i[1]] = d[i[0]]
		del d[i[0]]
	else:
		d[i[1]] = i[0]
print(len(d))
for i in d:
	print(d[i], i)


user_input = int(input())
o = []
n = []
for i in range(user_input):
	x = input().split(' ')
	try:
		c = n.index(x[0])
		n[c] = x[1]
	except:
		o.append(x[0])
		n.append(x[1])
print(len(o))
for i in range(len(o)):
	print(o[i] + ' ' + n[i])


n = int(input())
f = {}
ans = set()
for i in range(n):
	tmp = input().split()
	if tmp[0] not in f.values():
		ans.add(tmp[0])
	f[tmp[0]] = tmp[1]
print(len(ans))
for old in ans:
	new = old
	while new in f.keys():
		new = f[new]
	print(old, new)


viejos = []
nuevos = []
cantidad = int(input())
primer_input = input().rsplit(' ')
viejos.append(primer_input[0])
nuevos.append(primer_input[1])
for i in range(cantidad - 1):
	Esta = False
	entrada = input()
	entrada = entrada.rsplit(' ')
	old = entrada[0]
	new = entrada[1]
	for item in nuevos:
		if item == old:
			reemplazar = nuevos.index(item)
			nuevos[reemplazar] = new
			Esta = True
	if not Esta:
		viejos.append(old)
		nuevos.append(new)
print(len(viejos))
for i in range(len(viejos)):
	print(viejos[i] + ' ' + nuevos[i])


class Set:

	def __init__(self, i, element1, element2):
		self.id = i
		self.elementos = {element1, element2}
		self.cabeza = element1
		self.fin = element2

	def unir(self, other):
		if self.cabeza == other.fin:
			self.cabeza = other.cabeza
			self.elementos = self.elementos.union(other.elementos)
		elif self.fin == other.cabeza:
			self.fin = other.fin
			self.elementos = self.elementos.union(other.elementos)

	def elementos_comunes(self, other):
		if not self.elementos.isdisjoint(other.elementos):
			return True
		return False

	def __eq__(self, other):
		if self.id == other.id:
			return True
		else:
			return False
numero_cambios = int(input())
lista_sets = []
for i in range(numero_cambios):
	lista_cambio = input().split(' ')
	lista_sets.append(Set(i, lista_cambio[0], lista_cambio[1]))
entre = 0
while True:
	for i in lista_sets:
		for j in lista_sets:
			if i.id != j.id:
				if i.elementos_comunes(j):
					i.unir(j)
					lista_sets.remove(j)
					entre += 1
	if entre == 0:
		break
	else:
		entre = 0
print(len(lista_sets))
for i in lista_sets:
	print('{} {}'.format(i.cabeza, i.fin))


def main():
	q = int(input())
	d = {}
	for _ in range(q):
		(old, new) = input().split()
		oldold = d.get(old)
		if oldold:
			del d[old]
			d[new] = oldold
		else:
			d[new] = old
	l = [str(len(d))]
	for (k, v) in d.items():
		l.append(' '.join((v, k)))
	print('\n'.join(l))
main()


import copy
n = input()
E = list()

class Node:

	def __init__(self, nodo):
		self.padre = None
		self.nodo = nodo
		self.hijo = None

	def __repr__(self):
		return self.nodo
node_list = []
node_list_letter = []
for i in range(0, int(n)):
	e = input()
	e = e.split(' ')
	E.append(e)
	if e[0] not in node_list_letter:
		new_node = Node(e[0])
		node_list.append(new_node)
		node_list_letter.append(e[0])
	else:
		new_node = node_list[node_list_letter.index(e[0])]
	if e[1] not in node_list_letter:
		new_child = Node(e[1])
		node_list.append(new_child)
		node_list_letter.append(e[1])
	else:
		new_child = node_list[node_list_letter.index(e[1])]
	new_node.hijo = new_child
	new_child.padre = new_node
check = []

def buscar_padre(x):
	check.append(x)
	if x.padre:
		x = buscar_padre(x.padre)
	return x

def buscar_hijo(x):
	check.append(x)
	if x.hijo:
		x = buscar_hijo(x.hijo)
	return x
output = []
iter = 0
for i in node_list:
	iter += 1
	if i not in check:
		try:
			padre = buscar_padre(i)
		except Exception as e:
			pass
		try:
			hijo = buscar_hijo(i)
		except Exception as e:
			pass
		try:
			if [padre, hijo] not in output:
				output.append([padre, hijo])
		except Exception as e:
			pass
print(len(output))
for i in output:
	print(i[0].nodo + ' ' + i[1].nodo)


q = int(input())
a = []
b = []
for i in range(q):
	(m, n) = input().split()
	if not m in b:
		a.append(m)
		b.append(n)
	else:
		b[b.index(m)] = n
print(len(a))
for i in range(len(a)):
	print(a[i], ' ', b[i])


n = int(input())
(str1, str2, count) = ([], [], 0)
for _ in range(n):
	(x, y) = input().split()
	str1.append(x)
	str2.append(y)
for i in range(n):
	if str1[i] != 0:
		count += 1
		rstr = str2[i]
		for j in range(i + 1, n):
			if str1[j] == rstr:
				rstr = str2[j]
				(str1[j], str2[j]) = (0, 0)
		str2[i] = rstr
print(count)
for i in range(len(str1)):
	if str1[i] != 0:
		print(str1[i], str2[i])


cant = int(input())
lineas = []
for i in range(cant):
	linea = input()
	lineas.append(linea.split(' '))
finales = []
for i in lineas:
	esta = False
	for j in finales:
		if i[0] in j:
			esta = True
			j.append(i[1])
	if not esta:
		finales.append(i)
print(len(finales))
for i in finales:
	print(i[0] + ' ' + i[-1])


t = int(input())
old = []
new = []
for i in range(t):
	(o, n) = map(str, input().split())
	old.append(o)
	new.append(n)
ans = []
while 0 < len(old):
	s = new[0]
	ans.append(old[0])
	old.pop(0)
	new.pop(0)
	j = 0
	while j < len(old):
		if old[j] == s:
			s = new[j]
			old.pop(j)
			new.pop(j)
		else:
			j += 1
	ans.append(s)
print(len(ans) // 2)
i = 0
while i < len(ans) - 1:
	print(ans[i] + ' ' + ans[i + 1])
	i += 2


n = int(input())
d = dict()
for i in range(n):
	(fr, to) = map(str, input().split())
	if fr in d:
		d[to] = d[fr]
		del d[fr]
	else:
		d[to] = fr
print(len(d))
for p in d:
	print(d[p], p)


from collections import Counter
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
n = int(input())
d = {}
for i in range(n):
	(o, n) = input().split()
	if o in d:
		d[n] = d[o]
		d.pop(o)
	else:
		d[n] = o
print(len(d))
for i in d:
	print(d[i], i)


numero_pedido_troca = int(input())
mapemento_user = {}
for i in range(numero_pedido_troca):
	(antigo, novo) = input().split(' ')
	if antigo in mapemento_user:
		mapemento_user[novo] = (mapemento_user[antigo][0], novo)
		mapemento_user.pop(antigo)
	else:
		mapemento_user[novo] = (antigo, novo)
print(len(mapemento_user))
for i in mapemento_user.values():
	print('%s %s' % (i[0], i[1]))


nr_user = int(input())
handles = list()
flag_add = True
for i in range(nr_user):
	new_handle = input().split()
	for handle in handles:
		if new_handle[0] == handle[1]:
			handle[1] = new_handle[1]
			flag_add = False
	if flag_add:
		handles.append(new_handle)
	flag_add = True
print(len(handles))
for handle in handles:
	print(*handle)


I = input
P = print
d = {}
s = {}
for _ in '0' * int(I()):
	(o, n) = I().split()
	t = o in s
	d[[o, s.get(o)][t]] = n
	s[n] = [o, s.get(o)][t]
P(len(d))
for k in d:
	P(k, d[k])


misha = {}
n_consultas = int(input())
consultas = []
for k in range(n_consultas):
	consultas.append(input().split())
dic_imprimir = {}
for x in consultas:
	if x[0] in dic_imprimir:
		dic_imprimir[x[1]] = dic_imprimir[x[0]]
		del dic_imprimir[x[0]]
	else:
		dic_imprimir[x[1]] = x[0]
print(len(dic_imprimir))
for k in dic_imprimir:
	print('{0} {1}'.format(dic_imprimir[k], k))


from sys import stdin, stdout
input = stdin.readline
n = int(input())
ls = []
ll = []
oo = []
counter = 0
for i in range(n):
	ls.append(input().split())
for i in range(n):
	index = i
	if ls[i][0] not in ll:
		while True:
			start = i + 1
			check = True
			for j in range(start, n):
				if ls[j][0] == ls[index][1]:
					index = j
					start = j + 1
					ll.append(ls[j][0])
					check = False
			if start >= n or check:
				oo.append([ls[i][0], ls[index][1]])
				counter += 1
				break
print(counter)
for i in range(counter):
	print(*oo[i])


num = int(input())
d = {}
o = []
for i in range(num):
	(old, new) = input().split()
	if old not in d.values():
		o.append(old)
	d[old] = new
print(len(o))
for i in o:
	tmp = i
	while tmp in d:
		tmp = d[tmp]
	print(i, tmp)


def main():
	q = int(input())
	old = []
	new = []
	for _ in range(q):
		(a, b) = list(input().split())
		flag = 1
		for i in range(len(new)):
			if new[i] == a:
				new[i] = b
				flag = 0
				break
		if flag == 1:
			old.append(a)
			new.append(b)
	n = len(old)
	print(n)
	for i in range(n):
		print(old[i], new[i])

def __starting_point():
	main()
__starting_point()


q = int(input())
l = [[]]
t = []
for i in range(q):
	(old, new) = map(str, input().split())
	if l[-1] != []:
		l.append([])
	c = 1
	for j in range(len(l)):
		if old in l[j]:
			l[j].append(new)
			c = 0
			t.append(j)
	if c:
		l[-1].append(old)
		l[-1].append(new)
		t.append(len(l) - 1)
if l[-1] == []:
	l.remove([])
print(len(l))
z = []
for i in t[::-1]:
	if i not in z:
		z.append(i)
		print(l[i][0], l[i][-1])


(u, v) = ({}, {})
s = set()
for i in range(int(input())):
	(a, b) = input().split()
	if b in s:
		continue
	c = v.get(a, a)
	(u[c], v[b]) = (b, c)
	s.add(a)
	s.add(b)
print(len(u))
for a in u:
	print(a, u[a])


n = int(input())
a = []
b = []
for i in range(n):
	c = input().split(' ')
	if c[0] in a:
		z = a.index(c[0])
		a[z] = c[1]
	else:
		a.append(c[1])
		b.append(c[0])
print(len(a))
for i in range(len(a)):
	print(b[i] + ' ' + a[i])


t = int(input())
d = {}
for i in range(t):
	(o, n) = map(str, input().split())
	d[o] = n
	d[n] = 0
c = []
for key in d:
	if d[key] != 0:
		k = key
		while d[k] != 0:
			l = d[k]
			d[k] = 0
			k = l
		c.append([key, k])
print(len(c))
for i in c:
	print(*i)


def ke(val):
	for (key, value) in d.items():
		if val == value:
			return key
n = int(input())
d = {}
for i in range(n):
	(old, new) = input().split()
	if old in d.values():
		get = ke(old)
		d[get] = new
	else:
		d[old] = new
print(len(d))
for x in d:
	print(x, d[x])


ii = lambda : int(input())
kk = lambda : map(int, input().split())
ll = lambda : list(kk())
d = {}
for _ in range(ii()):
	(a, b) = input().split()
	if a in d:
		d[b] = d[a]
		del d[a]
	else:
		d[b] = a
print(len(d))
for k in d:
	print(d[k], k)


(n, d) = (int(input()), dict())
for i in range(n):
	(a, b) = input().split()
	tag = False
	for i in d:
		if d[i] == a:
			d[i] = b
			tag = True
			break
	if not tag:
		d[a] = b
print(len(d))
for i in d:
	print(i, d[i])


n = int(input())
names = {}
for i in range(n):
	(x, y) = map(str, input().split())
	names[x] = [y]
ans = []
for i in names:
	(x, y) = (i, names[i][0])
	if y not in names:
		continue
	elif len(names[x]) == 2:
		continue
	else:
		tmp = [x]
		j = names[x][0]
		names[x].append(True)
		while True:
			if j not in names:
				break
			names[j].append(True)
			j = names[j][0]
		tmp.append(j)
		ans.append(tmp)
for i in names:
	if len(names[i]) == 1:
		ans.append([i, names[i][0]])
print(len(ans))
for i in range(len(ans)):
	print(*ans[i])


from sys import stdin
input = lambda : stdin.readline().strip()
q = int(input())
d = {}
current = set()
for i in range(q):
	(a, b) = map(str, input().split())
	if b in current:
		pass
	else:
		flag = 0
		x = ''
		for j in d:
			if d[j] == a:
				flag = 1
				x = j
		if flag == 1:
			current.remove(a)
			current.add(b)
			d[x] = b
		else:
			d[a] = b
			current.add(b)
print(len(d))
for i in d:
	print(i, d[i])


from sys import stdin, stdout
import bisect

def ii():
	return int(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def li():
	return list(mi())

def si():
	return stdin.readline()
n = ii()
dict1 = {}
for i in range(n):
	(x, y) = input().split()
	if x in dict1:
		dict1[y] = dict1.pop(x)
	else:
		dict1[y] = x
print(len(dict1))
for i in dict1:
	print(dict1[i], i)


n = input()
handles_dict = {}
i = 0
while i < int(n):
	change = input().split(' ')
	old = change[0]
	new = change[1]
	found = False
	j = 0
	while j < len(handles_dict):
		if old in handles_dict[j]:
			handles_dict[j].append(new)
			found = True
		j += 1
	if not found:
		handles_dict[len(handles_dict)] = [old, new]
	i += 1
print(len(handles_dict))
i = 0
while i < len(handles_dict):
	print('{0} {1}'.format(handles_dict[i][0], handles_dict[i][-1]))
	i += 1


starting_names = []
ending_names = []
for _ in range(int(input())):
	(h1, h2) = input().split()
	if h1 not in ending_names:
		starting_names.append(h1)
		ending_names.append(h2)
	else:
		ending_names[ending_names.index(h1)] = h2
print(len(starting_names))
for x in range(len(ending_names)):
	print(starting_names[x] + ' ' + ending_names[x])


nb_user = int(input())
changes = {}
for _ in range(nb_user):
	(old, new) = [x for x in input().split()]
	if old in changes:
		changes[new] = changes[old]
		del changes[old]
	else:
		changes[new] = old
print(len(changes))
for k in changes:
	print(changes[k], k)


q = int(input())
(rev, h) = ({}, {})
for _ in range(q):
	(old, new) = input().split()
	if old in rev:
		k = rev[old]
		h[k] = new
		del rev[old]
		rev[new] = k
	else:
		h[old] = new
		rev[new] = old
print(len(h))
for (k, v) in h.items():
	print('{} {}'.format(k, v))


n = int(input())
d = {}
a = [''] * n
for i in range(n):
	a[i] = input().split()
	if a[i][0] in d.values():
		for (key, value) in d.items():
			if a[i][0] == value:
				v = key
				d.update({v: a[i][1]})
	else:
		d.update({a[i][0]: a[i][1]})
print(len(d))
for (key, value) in d.items():
	print(key, value)


numchanges = int(input())
queues = [['empty'] for i in range(numchanges + 1)]
lastPopulated = 0
inserted = False
for i in range(numchanges):
	(old, new) = map(str, input().split())
	for j in range(lastPopulated + 1):
		if old == queues[j][0]:
			queues[j].insert(0, new)
			inserted = True
	if not inserted:
		queues[lastPopulated].insert(0, old)
		queues[lastPopulated].insert(0, new)
		lastPopulated += 1
	else:
		inserted = False
print(lastPopulated)
for i in range(lastPopulated):
	print('%s %s' % (queues[i][-2], queues[i][0]))


import sys
data = sys.stdin.readlines()
nombres = []
relaciones = []
for i in range(int(data[0])):
	cambio = data[i + 1].strip('\n').split(' ')
	if cambio[0] in nombres:
		for relacion in relaciones:
			if cambio[0] == relacion[-1]:
				relacion.append(cambio[1])
		nombres.remove(cambio[0])
		nombres.append(cambio[1])
	else:
		relaciones.append(cambio)
		nombres.append(cambio[1])
print(len(relaciones))
for relacion in relaciones:
	print(relacion[0] + ' ' + relacion[-1])


q = int(input())
users = dict()
for i in range(q):
	(old, new) = input().split(' ')
	if old not in users:
		users[new] = old
	else:
		users[new] = users[old]
		del users[old]
print(len(users))
for (key, val) in users.items():
	print('%s %s' % (val, key))


n = int(input())
users = {}
for x in range(n):
	new = users.values()
	string = input()
	usernames = string.split()
	(a, b) = (usernames[0], usernames[1])
	if not a in new:
		users[a] = b
	else:
		for x in users:
			if users[x] == a:
				users[x] = b
				break
print(len(users))
for (x, y) in users.items():
	print('%s %s' % (x, y))


ii = lambda : int(input())
d = {}
for _ in range(ii()):
	q = input().split()
	try:
		d[q[1]] = d[q[0]]
		del d[q[0]]
	except KeyError:
		d[q[1]] = q[0]
print(len(d))
for (x, y) in d.items():
	print(y, x)


query = int(input())
mapa = {}
for i in range(query):
	tupla = list(input().split())
	if not tupla[0] in mapa:
		mapa[tupla[1]] = tupla[0]
	else:
		mapa[tupla[1]] = mapa[tupla[0]]
		del mapa[tupla[0]]
print(len(mapa))
for elemento in mapa:
	print(mapa[elemento], elemento)


n = int(input())
alt = {}
for _ in range(n):
	(antigo, novo) = input().split()
	if antigo not in alt:
		alt[novo] = antigo
	else:
		alt[novo] = alt[antigo]
		alt.pop(antigo, None)
print(len(alt))
for chave in alt:
	print('{name1} {name2}'.format(name1=alt[chave], name2=chave))


n = int(input())
p = []
for i in range(n):
	(x, y) = map(str, input().split(' '))
	found = 0
	for j in range(len(p)):
		if p[j][1] == x:
			p[j][1] = y
			found = 1
			break
	if found == 0:
		p.append([x, y])
	if p == []:
		p.append([x, y])
print(len(p))
for i in range(len(p)):
	print(p[i][0] + ' ' + p[i][1])


n = int(input())
old_h = {}
for i in range(n):
	(old, new) = input().split()
	if new not in old_h:
		if old in old_h:
			aux = old_h[old]
			del old_h[old]
			old_h[new] = aux
		else:
			old_h[new] = old
print(len(old_h.items()))
for (k, v) in old_h.items():
	print(v + ' ' + k)


x = []
y = []
n = int(input())
for i in range(n):
	(a, b) = input().split(' ')
	if a in y:
		y[y.index(a)] = b
	else:
		x.append(a)
		y.append(b)
print(len(x))
for i in range(len(x)):
	print(x[i], y[i])


n = int(input())
d = {}
for i in range(n):
	(handle, newHandle) = input().split()
	if handle not in d and handle not in d.values():
		d[handle] = newHandle
	elif handle in d:
		d[handle] = newHandle
	else:
		for j in d:
			if d[j] == handle:
				d[j] = newHandle
				break
print(len(d))
for i in d:
	print(i, d[i])


q = int(input())
old_new = {}
for i in range(q):
	(old, new) = list(map(str, input().split()))
	if old not in old_new:
		old_new[new] = old
	else:
		old_new[new] = old_new[old]
		old_new.pop(old)
print(len(old_new.items()))
for (x, y) in old_new.items():
	print(y, x)


n = int(input())
usuarios = {}
for i in range(n):
	dato = input().strip().split(' ')
	paso = False
	for (key, value) in usuarios.items():
		if dato[0] == value:
			usuarios[key] = dato[1]
			paso = True
	if not paso:
		usuarios[dato[0]] = dato[1]
print(len(usuarios))
for (k, v) in usuarios.items():
	print(k, v)


import sys
from collections import deque
fast_reader = sys.stdin.readline
fast_writer = sys.stdout.write

def input():
	return fast_reader().strip()

def print(*argv):
	fast_writer(' '.join((str(i) for i in argv)))
	fast_writer('\n')
for _ in range(1):
	n = int(input())
	d = {}
	p = []
	for i in range(n):
		(old, new) = input().split()
		d[old] = new
		p.append(old)
	visited = set()
	ans = 0
	lans = []
	for i in range(len(p)):
		if p[i] in visited:
			continue
		ans += 1
		old = p[i]
		q = deque()
		q.append(p[i])
		new = ''
		while len(q) != 0:
			t = q.popleft()
			if t in visited:
				continue
			elif t not in d:
				visited.add(t)
				break
			else:
				visited.add(t)
				new = d[t]
				q.append(d[t])
		lans.append([old, new])
	print(ans)
	for i in range(len(lans)):
		print(*lans[i])


from collections import defaultdict
import sys
q = int(sys.stdin.readline())
old = defaultdict(str)
new = defaultdict(str)
for i in range(q):
	(a, b) = sys.stdin.readline().split()
	if a in new.values():
		old[b] = old[a]
		new[old[a]] = b
	else:
		old[b] = a
		new[a] = b
sys.stdout.write(str(new.keys().__len__()) + '\n')
for x in new.keys():
	sys.stdout.write(x + ' ' + new[x] + '\n')


from collections import defaultdict, deque
n = input()
inputs = [input() for _ in range(int(n))]
num = 0
graph = defaultdict(list)
dic_names_first = dict()
dic_names_second = dict()
realdict = list()

def breadth_first_search(graph, root):
	(visited, queue) = (list(), deque([root]))
	while queue:
		vertex = queue.popleft()
		for neighbour in graph[vertex]:
			if neighbour not in visited:
				visited.append(neighbour)
				queue.append(neighbour)
	return visited[-1]
(visited, queue) = (list(), deque())
for handles in inputs:
	nicknames = handles.split(' ')
	graph[nicknames[0]].append(nicknames[1])
	dic_names_first[nicknames[0]] = True
	dic_names_second[nicknames[1]] = True
	if dic_names_second.get(nicknames[0], False) == False:
		realdict.append(nicknames[0])
		num += 1
print(num)
for name in realdict:
	print(name, breadth_first_search(graph, name))


l1 = []
l2 = []
for i in range(int(input())):
	(s1, s2) = input().split()
	if s1 not in l1 and s1 not in l2:
		l1.append(s1)
		l2.append(s2)
	else:
		l2[l2.index(s1)] = s2
print(len(l1))
for i in range(0, len(l1)):
	print(l1[i], l2[i])


(d, P) = ({}, [])
for _ in range(int(input())):
	(old, new) = input().split()
	if old in d:
		(P[d[old]][1], d[new]) = (new, d[old])
	else:
		d[new] = len(P)
		P.append([old, new])
print(len(P))
for p in P:
	print(*p)


from collections import Counter
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def tinput():
	return input().split()

def rinput():
	return map(int, tinput())

def rlinput():
	return list(rinput())
mod = int(1000000000.0) + 7

def root(parent, a):
	while parent[a] != a:
		parent[a] = parent[parent[a]]
		a = parent[a]
	return a

def union(parent, size, a, b):
	(root_a, root_b) = (root(parent, a), root(parent, b))
	if root_a == root_b:
		return
	if size[root_a] >= size[root_b]:
		parent[root_b] = root_a
		size[root_a] += size[root_b]
		size[root_b] = 0
	else:
		parent[root_a] = root_b
		size[root_b] += size[root_a]
		size[root_a] = 0

def solve(d, n):
	dcpy = d.copy()
	for i in dcpy:
		if i in d:
			temp = i
			while temp in d:
				temp1 = temp
				temp = d[temp]
				del d[temp1]
			d[i] = temp
	print(len(d))
	for i in d:
		print(i, d[i])
n = iinput()
d = dict()
for i in range(n):
	(a, b) = input().split()
	d[a] = b
solve(d, n)


n = int(input())
dic = {}
for _ in range(n):
	(a, b) = input().split()
	if a not in dic:
		dic[a] = b
ans = []
visited = set()
for key in dic:
	if key not in visited:
		temp_key = key
		while True:
			visited.add(temp_key)
			if dic[temp_key] in dic:
				temp_key = dic[temp_key]
			else:
				break
		ans.append(key + ' ' + dic[temp_key])
print(len(ans))
for out in ans:
	print(out)


def a():
	return [int(i) for i in input().split()]

def b():
	return a()[0]

def s():
	n = b()
	se = dict()
	es = dict()
	for i in range(n):
		(c, nn) = input().split()
		sn = es.get(c, c)
		se[sn] = nn
		es[nn] = sn
	print(len(se))
	se_pairs = [' '.join(item) for item in se.items()]
	print('\n'.join(se_pairs))
for t in range(1):
	s()


num = int(input())
firt_list = []
second_list = []
res_list = []
for i in range(num):
	l = input().split(' ')
	firt_list.append(l[0])
	second_list.append(l[1])
while len(firt_list) != 0:
	i += 1
	firstone = firt_list[0]
	secondone = second_list[0]
	try:
		while True:
			index = firt_list.index(secondone)
			secondone = second_list[index]
			second_list.pop(index)
			firt_list.pop(index)
	except:
		pass
	firt_list.pop(0)
	second_list.pop(0)
	res_list.append([firstone, secondone])
print(len(res_list))
for i in range(len(res_list)):
	print(*res_list[i])


a = int(input())
sa = []
for i in range(a):
	(x, y) = map(str, input().split(' '))
	sa.append([x, y, 0])
for i in sa:
	if i[2] == 0:
		for j in sa:
			if j[2] == 0 and i[1] == j[0]:
				i[1] = j[1]
				j[2] = 2
		i[2] = 1
a = [i for i in sa if i[2] == 1]
print(len(a))
for i in a:
	print(i[0], i[1])


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
import random
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def main():
	n = I()
	aa = [LS() for _ in range(n)]
	d = {}
	r = []
	for (a, b) in aa:
		if a in d:
			d[b] = d[a]
			del d[a]
		else:
			d[b] = len(r)
			r.append(a)
	rr = []
	for (k, v) in d.items():
		rr.append('{} {}'.format(r[v], k))
	return '{}\n{}'.format(len(rr), '\n'.join(rr))
print(main())


def MCH(n):
	d = dict()
	cont = 0
	for i in range(n):
		q = input().strip().split(' ')
		if not q[0] in d.values():
			d[q[0]] = q[1]
			cont += 1
		else:
			for k in d.keys():
				if d[k] == q[0]:
					d[k] = q[1]
	print(cont)
	for k in d.keys():
		print(k + ' ' + d[k])
n = int(input())
MCH(n)


d = {}
for i in range(int(input())):
	(name, nick) = map(str, input().split())
	if name in d.values():
		for (k, v) in d.items():
			if v == name:
				d[k] = nick
	else:
		d[name] = nick
print(len(d))
for (i, j) in d.items():
	print(i, j)


def search_key(handles, old, new):
	for (key, value) in handles.items():
		if value == old:
			handles[key] = new
			return True
	return False

def toString(handles):
	first = True
	total = 0
	res = ''
	for (key, value) in handles.items():
		total += 1
		if first:
			res += key + ' ' + value
			first = False
		else:
			res += '\n' + key + ' ' + value
	return '{}\n{}'.format(total, res)
queries = int(input())
handles = {}
for __ in range(queries):
	query = input().split()
	old = query[0]
	new = query[1]
	if not search_key(handles, old, new):
		handles[old] = new
print(toString(handles))


q = int(input())
handles = {}
count = 0
for i in range(q):
	(old, new) = input().split()
	if old in handles:
		original = handles[old]
		del handles[old]
		handles[new] = original
	else:
		handles[new] = old
		count += 1
print(count)
for (new, original) in handles.items():
	print(original, new)


q = int(input())
D = {}
count = 0
for i in range(q):
	Flag = 0
	(q1, q2) = input().split()
	if len(D) == 0:
		D[q1] = q2
		count += 1
	else:
		for (key, value) in D.items():
			if value == q1:
				D.update({key: q2})
				Flag = 1
		if Flag == 0:
			D.update({q1: q2})
			count += 1
i = 0
print(count)
for (key, value) in D.items():
	if i != 0:
		print('\n', end='')
	print(key, value, end='')
	i += 1


n = int(input())
handle = []
change = []
while n:
	b = input().split()
	boolchange = False
	for i in range(len(change)):
		if b[0] == change[i]:
			boolchange = True
			change[i] = b[1]
			break
	if boolchange == False:
		handle.append(b[0])
		change.append(b[1])
	n -= 1
print(len(handle))
for i in range(len(handle)):
	print(handle[i], change[i])


from functools import reduce
from operator import *
from math import *
from sys import *
setrecursionlimit(10 ** 7)
RI = lambda : list(map(int, input().split()))
RS = lambda : input().rstrip().split()
n = RI()[0]
ans = {}
for i in range(n):
	(old, new) = RS()
	if old in ans:
		ans[new] = ans[old]
		del ans[old]
	else:
		ans[new] = old
print(len(ans))
for i in ans:
	print(ans[i], i)


n = int(input())
dic = {}
for i in range(n):
	(x, y) = input().split()
	if x in dic:
		dic[y] = dic[x]
		del dic[x]
	else:
		dic[y] = x
print(len(dic))
for i in dic:
	print(dic[i], i)


class Coder(object):

	def __init__(self, old, new):
		self.old = old
		self.new = new

	def rename(self, name):
		self.new = name
n = int(input())
s = set()
for _ in range(n):
	(old, new) = input().split()
	noCoder = True
	for c in s:
		if c.new == old:
			c.rename(new)
			noCoder = False
	if noCoder:
		s.add(Coder(old, new))
print(len(s))
for c in s:
	print(c.old, c.new)


n = int(input())
d1 = {}
for i in range(n):
	(old, new) = input().split()
	if old in d1.values():
		for key in d1:
			if d1[key] == old:
				d1[key] = new
	else:
		d1[old] = new
print(len(d1))
for (k, v) in d1.items():
	print(k, v)


e = {}
d = {}
for i in range(int(input())):
	(a, b) = input().split()
	if a in e:
		d[e.get(a)] = b
		e[b] = e.get(a)
	else:
		d[a] = b
		e[b] = a
print(len(d))
for i in d:
	print(i, d[i])


from collections import defaultdict

def main():
	q = int(input())
	users = defaultdict(list)
	for _ in range(q):
		(old, new) = input().split()
		users[new].append(old)
		if old in users.keys():
			users[new] += users[old]
			del users[old]
	print(len(users))
	for (new, old) in users.items():
		print(old[-1], new)
main()


n = int(input())
a = []
b = []
resp = []
for i in range(0, n):
	ent = list(map(str, input().split()))[:2]
	if ent[0] in b:
		b[b.index(ent[0])] = ent[1]
	else:
		a.append(ent[0])
		b.append(ent[1])
print(len(a))
for j in range(0, len(a)):
	print(a[j], end=' ')
	print(b[j])


import sys
sys.setrecursionlimit(1000000)
(mod, MOD) = (1000000007, 998244353)
from collections import deque, Counter, OrderedDict, defaultdict

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(str, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()

def dfs(curr):
	visited[curr] = True
	if curr not in mydict:
		return curr
	return dfs(mydict[curr])
n = int(input())
mydict = dict()
mat = []
visited = dict()
for i in range(n):
	(a, b) = get_ints()
	mydict[a] = b
	mat.append([a, b])
	visited[a] = False
ans = []
for i in mat:
	if not visited[i[0]]:
		ans.append([i[0], dfs(i[0])])
print(len(ans))
for i in ans:
	print(*i)


def add_cconnection(new_change, data):
	(old, new) = new_change.split(' ')
	value = data.get(old)
	if value:
		del data[old]
		data[new] = value
	else:
		data[new] = old
num = int(input())
i = 0
data = {}
while i < num:
	_input = input()
	add_cconnection(_input, data)
	i += 1
results = list(data.items())
results.reverse()
print(len(results))
a = 0
for (new, old) in results:
	print(old, new)


linhas = int(input())
dic = {}
stack = []
for i in range(linhas):
	(old, new) = input().split()
	if old in set(list(dic.values())):
		index = list(dic.values()).index(old)
		dic[list(dic.keys())[index]] = new
		stack.append(list(dic.keys())[index])
	else:
		dic[old] = new
		stack.append(old)
print(len(dic))
conj = set([])
for i in range(len(dic)):
	tmp = stack.pop()
	while tmp in conj:
		tmp = stack.pop()
	print(tmp + ' ' + dic[tmp])
	conj.add(tmp)


q = int(input())
oldnames = []
newnames = []
for i in range(q):
	(old, new) = input().split()
	if old not in newnames:
		oldnames.append(old)
		newnames.append(new)
	else:
		change = newnames.index(old)
		newnames[change] = new
changes = len(oldnames)
print(changes)
for j in range(changes):
	print(oldnames[j], newnames[j])


N = int(input())
names = [input().split() for i in range(N)]
c_to_old = dict()
for n in names:
	if n[0] not in c_to_old:
		c_to_old[n[1]] = n[0]
	else:
		old = c_to_old[n[0]]
		del c_to_old[n[0]]
		c_to_old[n[1]] = old
print(len(c_to_old))
thing = sorted(c_to_old.items(), key=lambda x: x[1])
for (c, o) in thing:
	print(o, c)


t = int(input())
domain = set()
dsu = dict()
for _ in range(t):
	(old, new) = input().split()
	if new not in domain:
		if old not in domain:
			dsu[new] = old
		else:
			dsu[new] = dsu[old]
			del dsu[old]
		domain.add(old)
		domain.add(new)
print(len(dsu))
for i in dsu:
	print(dsu[i], i)


re = []
name = []
for i in range(int(input())):
	o = list(input().split())
	if i == 0:
		if o[1] not in re:
			name.append(o)
		re.append(o[0])
		re.append(o[1])
	elif o[1] not in re:
		u = 0
		for k in range(len(name)):
			if o[0] in name[k]:
				name[k][1] = o[1]
				u += 1
		if u == 0:
			name.append(o)
		re.append(o[0])
		re.append(o[1])
print(len(name))
for i in name:
	print(*i)


num = int(input())
old = []
new = []
for i in range(num):
	y = input().split(' ')
	try:
		x = new.index(y[0])
		new[x] = y[1]
	except:
		old.append(y[0])
		new.append(y[1])
print(len(old))
for i in range(len(old)):
	print(old[i] + ' ' + new[i])


n = int(input())
names = {}
orig = {}
for _ in range(n):
	(a, b) = input().split()
	if a not in orig:
		orig[a] = a
	orig[b] = orig[a]
	names[orig[b]] = b
print(len(names))
for (x, y) in names.items():
	print(x, y)


n = int(input())
f = {}
h = {}
for i in range(0, n):
	(old, new) = input().split()
	if old in h.values():
		for (p, q) in h.items():
			if q == old:
				h[p] = new
				if old in f.values():
					for (x, y) in f.items():
						if y == old:
							f.pop(x, old)
							break
				break
	else:
		h[old] = new
	f[old] = new
print(len(h))
for (x, y) in h.items():
	print(x + ' ' + y)


a = {}
ans = {}
kol = 0
n = int(input())
for i in range(n):
	k = input().split()
	a[k[1]] = k[0]
for i in a.keys():
	if not i in a.values():
		d = i
		try:
			while a[i] in a.keys():
				i = a[a[i]]
			if i in a.keys():
				i = a[i]
		except:
			...
		if d == i:
			i = a[i]
		ans[i] = d
		kol += 1
print(kol)
for i in ans.items():
	print(i[0], i[1])


def is_someone(old, d):
	for k in d:
		if d[k] == old:
			return k
	return False
n = int(input())
d = {}
for i in range(n):
	(old, new) = input().split()
	user = is_someone(old, d)
	if user:
		d[user] = new
	else:
		d[old] = new
print(len(d))
for k in d:
	print(k, d[k])


def root(a):
	while parent[a] != a:
		parent[a] = parent[parent[a]]
		a = parent[a]
	return a

def union(a, b):
	a = root(a)
	b = root(b)
	parent[a] = b
from collections import defaultdict
n = int(input())
parent = {}
user = []
for _ in range(n):
	(old, new) = input().split()
	user.append(old)
	parent[old] = old
	parent[new] = new
	union(old, new)
taken = defaultdict(bool)
ans = {}
c = 0
for u in user:
	if not taken[root(u)]:
		ans[u] = root(u)
		taken[root(u)] = True
		c += 1
print(c)
for m in ans:
	print(m, ans[m])


import sys
handles = sys.stdin.readlines()
handles = [handle.rstrip('\n').split(' ') for handle in handles[1:]]
users = {}
originals = []
for (old, new) in handles:
	if old not in users.keys():
		users[old] = new
		originals.append(old)
		users[new] = old
	else:
		previous = old
		old = users[old]
		del users[previous]
		users[new] = old
		users[old] = new
sys.stdout.write('{}\n'.format(len(originals)))
for handle in originals:
	sys.stdout.write('{} {}\n'.format(handle, users[handle]))


n = int(input())
handles = {}
for i in range(n):
	(old, new) = input().split()
	handles[new] = handles.get(old, old)
	handles.pop(old, None)
print(len(handles))
for (new, old) in handles.items():
	print(old, new)


from collections import Counter, defaultdict
from math import factorial
n = int(input())
d = defaultdict(str)
for i in range(n):
	(old, new) = [x for x in input().split()]
	if old in d.keys():
		d[new] = d[old]
		del d[old]
	else:
		d[new] = old
print(len(d))
for el in d:
	print(d[el], el)


n = int(input())
nombres = {}
for i in range(n):
	a = input()
	if a.split(' ')[0] not in nombres.keys():
		nombres[a.split(' ')[0]] = [a.split(' ')[1], 0]
nombres_visitados = []
imprimir = []
i = 0
while len(list(nombres.keys())) > 0:
	nombre_base = list(nombres.keys())[i]
	nombre_actual = list(nombres.keys())[i]
	if nombres[nombre_actual][1] == 0:
		while True:
			nombres_visitados.append(nombre_actual)
			nombres[nombre_actual][1] = 1
			nombres_futuro = nombres[nombre_actual][0]
			if nombres[nombre_actual][0] not in nombres.keys():
				imprimir.append(nombre_base + ' ' + nombres[nombre_actual][0])
				nombres.pop(nombre_actual)
				break
			else:
				nombres.pop(nombre_actual)
			nombre_actual = nombres_futuro
print(len(imprimir))
for i in imprimir:
	print(i)


import sys
sys.setrecursionlimit(10000)
q = int(input())
x = list()
for i in range(q):
	x.append(input().split())

def sequences(x, g=len(x) - 1):
	for i in range(g, 0, -1):
		for j in range(i - 1, -1, -1):
			if not set(x[i]).isdisjoint(x[j]):
				x[i] = (x[j][0], x[i][1])
				del x[j]
				return sequences(x, i - 1)
	return x
y = sequences(x)
print(len(y))
for i in y:
	print(' '.join(i))


dic = {}
n = int(input())
for i in range(n):
	full_name = input().split()
	if full_name[0] in list(dic.keys()):
		dic[full_name[1]] = dic[full_name[0]]
		del dic[full_name[0]]
	else:
		dic[full_name[1]] = full_name[0]
print(len(dic))
for (k, v) in dic.items():
	print('%s %s' % (v, k))


n = int(input())
hashmap = {}
for _ in range(n):
	(a, b) = list(map(lambda x: x, input().split()))
	if a in hashmap:
		temp = hashmap[a]
		del hashmap[a]
		hashmap[b] = temp
	else:
		hashmap[b] = a
print(len(hashmap))
for a in hashmap:
	print(hashmap[a], a)


import sys
from itertools import groupby
sys.setrecursionlimit(1000000000)
lines = []
for Line in sys.stdin:
	lines.append(Line.rstrip('\n'))
n = int(lines[0])
del lines[0]
d = dict()
for i in range(n):
	(a, b) = lines[i].split()
	d[a] = b
visited = []

def dfs(v):
	global visited
	if v in d:
		visited += [v]
		return dfs(d[v])
	else:
		return v
a = dict()
c = dict()
for (k, v) in d.items():
	a[k] = dfs(v)
for (k, v) in a.items():
	if k not in visited:
		c[k] = v
print(len(c))
for (k, v) in c.items():
	print(k, v)


q = int(input())
di = dict()
d = dict()
for i in range(q):
	(old, new) = input().split()
	if old not in d:
		if old in d.values():
			for (k, v) in d.items():
				if v == old:
					d[k] = new
					break
		else:
			d[old] = new
print(len(d))
for (k, v) in d.items():
	print(k, v)


n = int(input())
c = {}
d = []
l = []
for i in range(n):
	(a, b) = input().split()
	l.append(b)
	if a not in l:
		d.append(a)
	c[a] = b
print(len(d))
for i in d:
	x = c[i]
	while x in c:
		x = c[x]
	print(i, x)


import sys

def input():
	return sys.stdin.readline()[:-1]
q = int(input())
sonnnn = {}
fatherr = {}
for _ in range(q):
	(old, new) = [x for x in input().split()]
	if old not in sonnnn and old not in fatherr:
		sonnnn[old] = new
		fatherr[new] = old
	else:
		sonnnn[fatherr[old]] = new
		fatherr[new] = fatherr[old]
print(len(sonnnn))
for father in sonnnn:
	print(father, sonnnn[father])


n = int(input())
d = dict()
old = ''
new = ''
for i in range(n):
	(old, new) = input().split()
	if d.get(old) == None:
		d[new] = old
	else:
		d[new] = d[old]
		d.pop(old)
lent = len(d)
print(lent)
for a in d:
	print(d[a], a)


class QuickFindUF:

	def __init__(self):
		self.parent = {}

	def connected(self, node1, node2):
		return self.root(node1) == self.root(node2)

	def root(self, node):
		if not node in self.parent.keys():
			self.parent[node] = node
			return node
		while self.parent[node] != node:
			self.parent[node] = self.parent[self.parent[node]]
			node = self.parent[node]
		return node

	def union(self, childNode, parentNode):
		rootChildNode = self.root(childNode)
		rootParentNode = self.root(parentNode)
		self.parent[rootChildNode] = rootParentNode
unionFind = QuickFindUF()
handles = {}
requestCount = int(input())
for i in range(requestCount):
	(oldHandle, newHandle) = input().split(' ')
	if oldHandle not in handles.keys():
		handles[oldHandle] = True
	handles[newHandle] = False
	unionFind.union(oldHandle, newHandle)
initialHandlesWithChange = list(filter(lambda x: handles[x], handles))
print(len(initialHandlesWithChange))
for handle in initialHandlesWithChange:
	print(handle, unionFind.root(handle))


def misha():
	times = int(input())
	a = []
	b = []
	for time in range(times):
		p = input().split()
		if p[0] in b:
			b[b.index(p[0])] = p[1]
		else:
			a.append(p[0])
			b.append(p[1])
	print(len(a))
	for i in range(len(a)):
		print(a[i], b[i])
	return
misha()


n = int(input())
handle = []
lines = []
for i in range(0, n):
	lines.append(input())
for line in lines:
	(old, new) = line.split(' ')
	for h in handle:
		if h[-1] == old:
			h.append(new)
			old = None
	if old:
		handle.append([old, new])
print(len(handle))
for h in handle:
	print(h[0] + ' ' + h[-1])


n = int(input())
dic = {}
c = 0
for i in range(n):
	(o, n) = str(input()).split(' ')
	x = dic.get(o)
	if x is not None and x != o:
		oo = dic.pop(o)
		dic[n] = oo
	else:
		dic[n] = o
		c += 1
print(c)
l = list(dic)
for i in range(len(l), 0, -1):
	print(dic[l[i - 1]] + ' ' + l[i - 1])


import sys
n = int(sys.stdin.readline())
tracker = {}
count = 0
for i in range(n):
	pair = sys.stdin.readline().split('\n')[0].split(' ')
	if pair[0] in tracker:
		tracker[pair[1]] = tracker.pop(pair[0])
	else:
		count += 1
		tracker[pair[1]] = pair[0]
print(count)
for key in list(tracker.keys()):
	print(tracker[key] + ' ' + key)


numero = int(input())
requests = []
for x in range(numero):
	requests.append(input().split(' '))

class nombre:

	def __init__(self, actual, hijo, parent):
		self.actual = actual
		self.hijo = hijo
		self.parent = parent

def buscar_segun_string(lista_objetos, string):
	for obj in lista_objetos:
		if obj.actual == string:
			return obj
dic = {}
grafo = {}
lista_nombres = list()
lista_pares = list()
pares = []
for nombre in lista_nombres:
	for nombre1 in lista_nombres:
		if nombre != nombre1 and nombre1.actual == nombre.actual:
			lista_nombres.remove(nombre1)
lista_si_existe = [None for x in range(2 * numero)]
contador = 0
for req in requests:
	existe = False
	for i in range(2 * numero):
		if lista_si_existe[i] == None:
			continue
		elif lista_si_existe[i] != None and req[0] == lista_si_existe[i].actual:
			aux1 = nombre(req[1], None, None)
			lista_si_existe[i].hijo = aux1
			aux1.parent = lista_si_existe[i]
			lista_si_existe[contador] = aux1
			existe = True
			contador += 1
			break
	if not existe:
		aux1 = nombre(req[0], None, None)
		aux2 = nombre(req[1], None, None)
		aux1.hijo = aux2
		aux2.parent = aux1
		lista_si_existe[contador] = aux1
		lista_si_existe[contador + 1] = aux2
		contador += 2
respuesta = []
for nombre in lista_si_existe:
	if nombre != None and nombre.parent == None:
		copia = nombre
		while nombre.hijo != None:
			nombre = nombre.hijo
		respuesta.append([copia.actual, nombre.actual])
print(len(respuesta))
for item in respuesta:
	print('{} {}'.format(item[0], item[1]))


from collections import defaultdict, Counter
from math import sqrt, log10, log2, log, gcd, floor, factorial
from bisect import bisect_left, bisect_right
from itertools import combinations, combinations_with_replacement
import sys, io, os
input = sys.stdin.readline
mod = 10 ** 9 + 7
inf = float('inf')

def get_list():
	return [int(i) for i in input().split()]
yn = lambda a: print('YES' if a else 'NO')
ceil = lambda a, b: (a + b - 1) // b
t = 1
for i in range(t):
	n = int(input())
	dict = defaultdict(str)
	first = set()
	for i in range(n):
		(a, b) = input().strip().split(' ')
		dict[a] = b
	for i in dict:
		first.add(i)
	for i in dict.values():
		first.discard(i)
	l = []
	for i in first:
		j = i
		while dict[i] != '':
			i = dict[i]
		l.append([j, i])
	print(len(l))
	for i in l:
		print(' '.join(i))


from collections import defaultdict
q = int(input())
old = defaultdict(str)
new = defaultdict(str)
for i in range(q):
	(a, b) = input().split()
	if a in new.values():
		old[b] = old[a]
		new[old[a]] = b
	else:
		old[b] = a
		new[a] = b
print(new.keys().__len__())
for x in new.keys():
	print(x, new[x])


n = []
for _ in range(int(input())):
	(a, b) = input().split()
	for x in range(len(n)):
		if n[x][1] == a:
			n[x][1] = b
			break
	else:
		n.append([a, b])
print(len(n))
for i in n:
	print(' '.join(i))


n = int(input())
old = []
new = []
for i in range(n):
	l = input().split()
	mark = -1
	for i in range(len(new)):
		if new[i] == l[0]:
			mark = i
			break
	if mark == -1:
		old.append(l[0])
		new.append(l[1])
	else:
		new[i] = l[1]
print(len(new))
for i in range(len(new)):
	print(old[i], new[i])


n = int(input())
k = 0
initial = dict()
inp = [input().split() for i in range(n)]
for (old, new) in inp:
	if old not in initial:
		initial[new] = old
		initial[old] = old
	else:
		initial[new] = initial[old]
curr = dict()
for (_, new) in inp:
	curr[initial[new]] = new
print(len(curr.keys()))
for it in curr.items():
	print(*it)


n = int(input())
parents = {}
curr = {}
for i in range(n):
	(old, new) = input().split()
	if old in parents:
		parents[new] = parents[old]
	else:
		parents[new] = old
	curr[parents[new]] = new
print(len(curr))
for v in curr:
	print('%s %s' % (v, curr[v]))


q = int(input())
n = [input() for x in range(q)]
a = [n[0].split()]
for x in range(1, q):
	f = False
	s = n[x].split()
	for y in a:
		if s[0] == y[-1]:
			y.append(s[1])
			f = True
			break
	if not f:
		a.append(s)
print(len(a))
for x in a:
	print(x[0], x[-1])


n = int(input())
d1 = dict()
d2 = dict()
for i in range(n):
	(a, b) = input().split()
	if d2.get(a) is None:
		d1[a] = b
		d2[b] = a
	else:
		d1[d2[a]] = b
		d2[b] = d2[a]
print(len(d1))
for i in d1:
	print(i, d1.get(i))


n = int(input())
d = {}
for _ in range(n):
	(old, new) = input().split()
	if old not in d:
		d[new] = old
	else:
		d[new] = d[old]
		d.pop(old)
print(len(d))
for (k, v) in d.items():
	print(v, k, sep=' ')


n = int(input())
d = {}
for i in range(n):
	(old, new) = input().split()
	already = True
	for i in d:
		if old == d[i]:
			d[i] = new
			already = False
	if already:
		d[old] = new
print(len(d))
for i in d.keys():
	print(str(i) + ' ' + str(d[i]))


n = int(input())
m = {}
for i in range(n):
	(a, b) = map(str, input().split())
	try:
		kk = m[a]
		m.__delitem__(a)
		m[b] = kk
	except KeyError:
		m[b] = a
print(len(m))
for k in m:
	print(m[k], k)


t = int(input())
d = {}
while t > 0:
	(n, n1) = map(str, input().split())
	if n not in d:
		d[n1] = n
	else:
		k = d[n]
		d[n1] = k
		del d[n]
	t -= 1
print(len(d))
for i in d:
	print(d[i], i)


def main():
	q = int(input())
	users = []
	newusers = []
	for i in range(q):
		line = input().rstrip().split()
		old = line[0]
		new = line[1]
		if old not in newusers:
			users.append(old)
			newusers.append(new)
		else:
			index0 = newusers.index(old)
			newusers[index0] = new
	print(str(len(users)))
	for j in range(len(users)):
		print(users[j] + ' ' + newusers[j])
main()


n = int(input())
old_handles = {}
new_handles = {}
for i in range(n):
	(old, new) = input().split()
	if old in new_handles:
		old_handle = new_handles[old]
		old_handles[old_handle] = new
		new_handles[new] = old_handle
	else:
		old_handles[old] = new
		new_handles[new] = old
print(len(old_handles))
for i in old_handles:
	print(i, old_handles[i])


import sys
from collections import OrderedDict
input = lambda : sys.stdin.readline().strip()
li = lambda : list(map(int, input().split()))
I = lambda : int(input())
t = I()
m = OrderedDict()
for z in range(t):
	(o, n) = input().split()
	m[o] = [n, 0]
ans = OrderedDict()
for i in m.keys():
	if m[i][1] != 1:
		cur = m[i]
		while cur[1] == 0 and m.get(cur[0]) != None:
			cur[1] = 1
			cur = m.get(cur[0])
			if m.get(cur[0]) == None:
				cur[1] = 1
		m[i][1] = 1
		ans[i] = cur[0]
print(len(ans))
for i in ans:
	print(i, ans[i])


n = int(input())
old = []
new = []
for i in range(n):
	(old_name, new_name) = input().split()
	old.append(old_name)
	new.append(new_name)
names = dict()
used = []
original = []
for i in range(len(old)):
	if new[i] in used:
		continue
	if old[i] not in used:
		original.append(old[i])
	names[old[i]] = new[i]
	used.append(old[i])
	used.append(new[i])
print(len(original))
for original in original:
	old = original
	new = names[old]
	while new in names:
		old = new
		new = names[old]
	print(original, new)


def get_key(d, value):
	for (k, v) in d.items():
		if v == value:
			return k
m = [int(x) for x in input().split()][0]
d = dict()
for _ in range(m):
	x = input().split()
	if x[0] in d.values():
		d[get_key(d, x[0])] = x[1]
	else:
		d[x[0]] = x[1]
print(len(d))
for x in d.keys():
	print(x, d[x], sep=' ')


n = int(input())
users = []
counter = 0

class User:

	def __init__(self, old_name, new_name):
		self.old_name = old_name
		self.actual_name = new_name
for i in range(n):
	r = input().split()
	for u in users:
		if u.actual_name == r[0]:
			u.actual_name = r[1]
			break
	else:
		users.append(User(r[0], r[1]))
		counter += 1
print(counter)
for u in users:
	print('{} {}'.format(u.old_name, u.actual_name))


I = lambda : input().split()
n = int(input())
d = {}
for i in range(n):
	(x, y) = I()
	if x not in d and x not in d.values():
		d[x] = y
	elif x in d:
		d[x] = y
	else:
		for i in d:
			if d[i] == x:
				d[i] = y
				break
print(len(d))
for i in d:
	print(i, d[i])


from sys import stdin
input = lambda : stdin.readline().strip()
from math import ceil, sqrt, factorial
INT_MIN = float('-infinity')
INT_MAX = float('infinity')
q = int(input())
d = {}
current = set()
for i in range(q):
	(a, b) = map(str, input().split())
	if b in current:
		pass
	else:
		flag = 0
		x = ''
		for j in d:
			if d[j] == a:
				flag = 1
				x = j
		if flag == 1:
			current.remove(a)
			current.add(b)
			d[x] = b
		else:
			d[a] = b
			current.add(b)
print(len(d))
for i in d:
	print(i, d[i])


lines = int(input())
initial = []
username = {}
for i in range(lines):
	(old, new) = input().strip().split(' ')
	if old not in username.keys() and old not in username.values():
		initial.append(old)
	username[old] = new
print(len(initial))
for user in initial:
	aux = user
	while aux in username.keys():
		aux = username[aux]
	print('{} {}'.format(user, aux))


dicionario = {}
num_de_requests = int(input())
for novo in range(num_de_requests):
	(antigo, novo) = input().split(' ')
	if antigo not in dicionario:
		dicionario[novo] = antigo
	else:
		aux = dicionario.pop(antigo)
		dicionario[novo] = aux
print(len(dicionario))
for (novo, antigo) in dicionario.items():
	print(antigo, novo)


n = int(input())
d = {}
for i in range(n):
	(old, new) = map(str, input().split())
	f = 0
	for (key, item) in d.items():
		if item == old:
			d[key] = new
			f = 1
			break
	if f == 1:
		continue
	d[old] = new
print(len(d))
for (i, j) in d.items():
	print(i, j)


n = int(input())
handlers = {}
handlersAux = {}
qtd = 0
for i in range(n):
	(old, new) = [s for s in input().split(' ')]
	if old in handlersAux:
		handlers[handlersAux[old]] = new
		handlersAux[new] = handlersAux[old]
	else:
		handlers[old] = new
		handlersAux[new] = old
print(len(handlers))
for (old, new) in handlers.items():
	print(old, new)


n = int(input())
new = {}
for s in range(0, n):
	(user, req) = input().split()
	bl = False
	for (k, v) in new.items():
		if v == user:
			new[k] = req
			bl = True
			break
	if bl == False:
		new[user] = req
print(len(new))
for (k, v) in new.items():
	print(k + ' ' + v)


num_req = int(input())
requests = []
for j in range(num_req):
	req = input()
	req = list(req.split(' '))
	requests.append(req)
lista_old = []
for req in requests:
	validator = False
	for j in lista_old:
		if req[0] in j[1]:
			j[1].append(req[1])
			validator = True
			break
	if not validator:
		nodo = (req[0], list())
		nodo[1].append(req[1])
		lista_old.append(nodo)
output = []
for i in lista_old:
	string = ''
	string += str(i[0]) + ' ' + str(i[1][-1])
	output.append(string)
print(str(len(lista_old)))
for j in output:
	print(j)


n = int(input())
names = {}
for i in range(n):
	a = input().split(' ')
	listo = False
	for i in names:
		if a[0] == names[i]:
			names[i] = a[1]
			listo = True
			break
	if not listo:
		names[a[0]] = a[1]
print(len(names))
for i in names:
	print(i, names[i])


def dfs(d, u, visted):
	visted[u] = True
	while True:
		if u not in d:
			break
		u = d[u]
		visted[u] = True
	return u
n = int(input())
l = {}
visted = {}
for i in range(n):
	(a, b) = input().split()
	l[a] = b
	visted[a] = False
k = 0
e = []
for i in l:
	if visted[i] == False:
		k += 1
		a = dfs(l, i, visted)
		e.append((i, a))
print(k)
for i in range(k):
	print(*e[i])


lineas = int(input())
listas = list()
for i in range(0, lineas):
	line = input()
	line = line.split()
	line.append('0')
	listas.append(line)
listas_output = list()
for l in listas:
	if l[2] == 1:
		continue
	name = [l[0], l[1]]
	for k in listas:
		if k[0] in name and k != l:
			k[2] = 1
			name.append(k[1])
	l[2] = 1
	listas_output.append(name)
print(len(listas_output))
for j in listas_output:
	if len(j) == 2:
		print(j[0], j[-1])
for z in listas_output:
	if len(z) > 2:
		print(z[0], z[-1])


n = int(input())
(m, rev) = ({}, {})
for i in range(n):
	(a, b) = input().split()
	if a not in rev:
		m[a] = b
		rev[b] = a
	else:
		m[rev[a]] = b
		rev[b] = rev[a]
print(len(m))
for key in m:
	print(key, m[key])


ii = lambda : int(input())
d = {}
for _ in range(ii()):
	q = input().split()
	if q[0] in d:
		d[q[1]] = d[q[0]]
		d.pop(q[0])
	else:
		d[q[1]] = q[0]
print(len(d))
for (x, y) in d.items():
	print(y, x)


n = int(input())
d = {}
for i in range(n):
	(a, b) = input().split()
	bo = True
	for j in d:
		if a == d[j]:
			d[j] = b
			bo = False
	if bo:
		d[a] = b
print(len(d))
for i in d:
	print(i, d[i])


num_lines = input()
name_mapping = []
for i in range(int(num_lines)):
	line = input()
	(former, latter) = line.split(' ')
	check = 0
	for j in name_mapping:
		if j[-1] == former:
			j.append(latter)
			check = 1
	if check == 0:
		name_mapping.append([former, latter])
print(len(name_mapping))
for k in name_mapping:
	print(k[0], ' ', k[-1])


t = int(input())
a = []
for i in range(t):
	b = [x for x in input().split()]
	a.append(b)
b = []
c = []
for i in range(t):
	b.append(a[i][0])
	c.append(a[i][1])
cnt = 0
d = []
for x in b:
	if x not in c:
		cnt += 1
		d.append(x)
print(cnt)
h = []
for y in d:
	e = b.index(y)
	g = []
	p = c[e]
	while c[e] in b:
		f = b.index(c[e])
		e = f
		p = c[e]
	g.append(y)
	g.append(p)
	h.append(g)
for i in range(cnt):
	print(h[i][0] + ' ' + h[i][1])


queries = []
for q in range(int(input())):
	(old, new) = input().split()
	done = False
	for elem in queries:
		if old == elem[-1]:
			elem.append(new)
			done = True
			break
	if not done:
		queries.append([old, new])
print(len(queries))
for elem in queries:
	print(elem[0], elem[-1])


ii = lambda : int(input())
d = {}
for _ in range(ii()):
	q = input().split()
	if q[0] in d:
		d[q[1]] = d[q[0]]
		del d[q[0]]
	else:
		d[q[1]] = q[0]
print(len(d))
for x in d.keys():
	print(d[x], x)


n = input()
lista_nuevos = []
lista_viejos = []
for i in range(int(n)):
	elem = input().split(' ')
	if elem[0] not in lista_viejos and elem[0] not in lista_nuevos:
		lista_viejos.append(elem[0])
		lista_nuevos.append(elem[1])
	elif elem[0] in lista_nuevos:
		indice = lista_nuevos.index(elem[0])
		lista_nuevos.pop(indice)
		viejo = lista_viejos[indice]
		lista_viejos.remove(viejo)
		lista_viejos.append(viejo)
		lista_nuevos.append(elem[1])
print(len(lista_nuevos))
j = len(lista_viejos) - 1
while j >= 0:
	s = ''
	s += lista_viejos[j] + ' '
	s += lista_nuevos[j]
	print(s)
	j -= 1


n = int(input())
aux = {}
for i in range(n):
	entrada = input().split()
	if len(aux) == 0:
		aux[entrada[1]] = entrada[0]
	elif entrada[0] in aux:
		aux[entrada[1]] = aux.pop(entrada[0])
	else:
		aux[entrada[1]] = entrada[0]
print(len(aux))
for s in aux:
	print(aux[s], s)


n = int(input())
map = {}
for i in range(0, n):
	input_string = input()
	input_string = input_string.split(' ')
	if input_string[0] in map:
		temp = map[input_string[0]]
		map.pop(input_string[0])
		map[input_string[1]] = temp
	else:
		map[input_string[1]] = input_string[0]
print(len(map))
for key in map.keys():
	value = map[key]
	print(f'{value} {key}')


n = int(input())
res = []
update = False
for i in range(n):
	(old, new) = input().split()
	update = False
	for i in res:
		if i[1] == old:
			i[1] = new
			update = True
			break
	if update == False:
		res.append([old, new])
print(len(res))
for i in res:
	print(*i)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
sys.setrecursionlimit(int(pow(10, 6)))
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
d = {}
for _ in range(l()[0]):
	pos = 0
	(a, b) = ssp()
	for ks in d:
		if d[ks] == a:
			d[ks] = b
			pos = 1
	if not pos:
		d[a] = b
print(len(d.keys()))
for ks in d:
	print(ks, d[ks])


n = {}
for _ in range(int(input())):
	(a, b) = map(str, input().split())
	if a in n:
		n[b] = n[a]
		del n[a]
	else:
		n[b] = a
print(len(list(n)))
for x in list(n):
	print(n[x], x)


from sys import stdin, stdout
values = []
n = 0
for line in stdin.readlines():
	if n == 0:
		n = int(line)
		continue
	else:
		values.append(line.strip().split(' '))
diccionario = {}
for value in values:
	if value[0] not in diccionario.values():
		diccionario[value[0]] = value[1]
	else:
		for (key, value_1) in diccionario.items():
			if value_1 == value[0]:
				diccionario[key] = value[1]
output_list = [str(value[0]) + ' ' + str(value[1]) for value in list(diccionario.items())]
stdout.write(str(len(output_list)) + '\n')
stdout.writelines(['%s\n' % value for value in output_list])


q = int(input())
dic = {}
for i in range(q):
	(a, b) = input().split()
	used = False
	for i in list(dic.keys()):
		if a == dic[i]:
			used = True
			dic[i] = b
	if not used:
		dic[a] = b
print(len(dic))
for i in list(dic.keys()):
	print(i, dic[i])


cambios = int(input())
lista_de_cambios = []
for xxx in range(cambios):
	aux = tuple(input().strip().split(' '))
	lista_de_cambios.append(aux)
finales = {}
for cambio in lista_de_cambios:
	for llave in finales.keys():
		if finales[llave] == cambio[0]:
			finales[llave] = cambio[1]
			break
	else:
		finales[cambio[0]] = cambio[1]
print(len(finales))
for llave in finales.keys():
	print(llave, finales[llave])


from sys import stdin, stdout
nombres_originales = {}
lista_queries = []
q = int(stdin.readline().rstrip())
for i in range(q):
	linea = stdin.readline()
	linea = linea.split('\n')[0]
	lista = linea.split(' ')
	lista_queries.append((lista[0], lista[1]))
for tupla in lista_queries:
	if tupla[0] in nombres_originales.values():
		for (nombre_original, nombre_viejo) in nombres_originales.items():
			if tupla[0] == nombre_viejo:
				nombres_originales[nombre_original] = tupla[1]
				break
	else:
		nombres_originales[tupla[0]] = tupla[1]
stdout.write(str(len(nombres_originales)) + '\n')
for (nombre_original, nombre_viejo) in nombres_originales.items():
	stdout.write(nombre_original + ' ' + nombre_viejo + '\n')


n = int(input())
new = {}
oldold = {}
for i in range(n):
	(u, v) = input().split()
	if not u in oldold:
		oldold[u] = u
	oldold[v] = oldold[u]
	new[oldold[u]] = v
print(len(new))
for u in new:
	print(u, new[u])


n = int(input())
p = []
q = []
c = 1
for i in range(n):
	(a, b) = map(str, input().split())
	if a not in q:
		p.append([a, b])
		q.append(a)
		q.append(b)
	else:
		x = q.index(p[int(q.index(a) / 2 - 0.5)][1])
		p[int(q.index(a) / 2 - 0.5)][1] = b
		q[x] = b
print(len(p))
for i in range(len(p)):
	print(p[i][0], p[i][1])


n = int(input())
names = {}
changes = 0
for i in range(n):
	(current, changed) = map(str, input().split())
	if current not in names:
		names[changed] = current
		changes += 1
	else:
		aux = names[current]
		names.pop(current)
		names[changed] = aux
print(changes)
for i in names:
	print(names[i], i)


d = dict()
n = int(input())
for i in range(n):
	(x, y) = input().split()
	if x in d:
		d[y] = d[x]
		d.pop(x)
	else:
		d[y] = x
print(len(d))
for i in d:
	print(d[i], i)


from sys import stdin, stdout

def solve(renames):
	handles = {}
	for names in renames:
		oldname = names[0]
		newname = names[1]
		if oldname not in handles:
			handles[newname] = oldname
		else:
			oldername = handles.pop(oldname)
			handles[newname] = oldername
	return handles

def print_handles(handles):
	names = handles.items()
	stdout.write(str(len(names)))
	stdout.write('\n')
	for (newname, oldname) in names:
		stdout.write('{} {}'.format(oldname, newname))
		stdout.write('\n')
renames = []
n = int(stdin.readline())
for i in range(n):
	lines = stdin.readline().rstrip('\r\n').split()
	renames.append((lines[0], lines[1]))
solution = solve(renames)
print_handles(solution)


from collections import defaultdict as dd
from sys import setrecursionlimit
setrecursionlimit(11000)
q = int(input())
vis = dd(int)
names = dict()
ans = []

def findNew(name):
	if names.get(name, -1) == -1:
		return name
	vis[name] = 1
	return findNew(names.get(name))
for _ in range(q):
	(s, t) = input().split()
	names[s] = t
for name in names:
	if vis[name] != 1:
		ans.append((name, findNew(name)))
print(len(ans))
for i in ans:
	print(i[0], i[1])


old = []
new = []
for i in range(int(input())):
	(o, n) = input().split()
	if o in new:
		new[new.index(o)] = n
	else:
		old.append(o)
		new.append(n)
print(len(old))
for i in range(len(old)):
	print(old[i], new[i])


n = int(input())
a = dict()
d = []
for i in range(n):
	s = list(input().split())
	if a.get(s[0]) == None:
		a[s[1]] = s[0]
		d.append(s[0])
	else:
		r = a.pop(s[0])
		a[s[1]] = r
print(len(d))
for i in range(len(d)):
	t = a.popitem()
	print(t[1], t[0])


n = eval(input())
s = {}
cnt = 0

def update():
	(a, b) = input().split(' ')
	for g in s.keys():
		if s[g] == a:
			s[g] = b
			return 1
	s[a] = b
	global cnt
	cnt += 1
for i in range(n):
	update()
print(cnt)
for (a, b) in s.items():
	print(a + ' ' + b)


x = int(input())
dic = {}
for c in range(x):
	(a, b) = input().split()
	if a in dic.values():
		for (key, value) in dic.items():
			if value == a:
				dic[key] = b
	else:
		dic[a] = b
print(len(dic))
for (key, value) in dic.items():
	print(str(key) + ' ' + str(value))


a = dict()
b = dict()
n = int(input())
ans = 0
for i in range(n):
	word = input().split()
	x = word[0]
	y = word[1]
	if a.get(x) == None and b.get(x) == None:
		a[x] = y
		b[y] = x
	elif b.get(x) != None:
		b[y] = b[x]
		a[b[y]] = y
m = len(a)
print(m)
for i in range(m):
	ans = a.popitem()
	print(ans[0], end=' ')
	print(ans[1])


n = int(input())
d = {}
changes = 0
for i in range(n):
	(old_handle, new_handle) = input().split()
	if d.__contains__(old_handle):
		d[new_handle] = d[old_handle]
		d.__delitem__(old_handle)
	else:
		d[new_handle] = old_handle
		changes += 1
print(changes)
for new_handle in d.keys():
	print(d[new_handle], new_handle)


q = int(input())
old = dict()
new = dict()
for i in range(q):
	name = input().split()
	if new.get(name[0]) == None:
		old[name[0]] = name[1]
		new[name[1]] = name[0]
	else:
		old[new[name[0]]] = name[1]
		new[name[1]] = new[name[0]]
print(len(old))
for i in old:
	print(i, old[i])


n = int(input())
d = {}
for i in range(n):
	(old, new) = input().split()
	if old in d:
		d[new] = d[old]
		del d[old]
	else:
		d[new] = old
print(len(d))
for x in d:
	print(d[x], x)


def solve():
	q = int(input())
	l = []
	for i in range(q):
		(old, new) = input().split()
		for namelist in l:
			if namelist[-1] == old:
				namelist.append(new)
				break
		else:
			l.append([old, new])
	print(len(l))
	for namelist in l:
		print(namelist[0], namelist[-1])
solve()


import sys
data = sys.stdin.readlines()
archivo = [line.split() for line in data]
cambios = int(archivo[0][0])
dic = {}
contador = 0
lista = []
for i in range(cambios):
	dic[archivo[i + 1][0]] = archivo[i + 1][1]
for (key, value) in dic.items():
	viejo = key
	nuevo = value
	eliminar = nuevo
	while True:
		if nuevo in dic.keys():
			eliminar = nuevo
			nuevo = dic.get(nuevo)
			dic[eliminar] = 0
		else:
			if eliminar != 0 and nuevo != 0:
				cambio = viejo + ' ' + nuevo
				lista.append(cambio)
			break
print(len(lista))
for i in lista:
	print(i)


db = {}
names = int(input())
for i in range(names):
	users = input().split()
	if users[0] in db:
		for key in db:
			if db[key] == users[0]:
				db[key] = users[1]
				break
		db[users[1]] = 'old'
	else:
		db[users[0]] = users[1]
		db[users[1]] = 'old'
how_many = 0
for name in db:
	if db[name] != 'old':
		how_many += 1
print(how_many)
for name in db:
	if db[name] != 'old':
		print(name, ' ', db[name])


times = int(input())
old_to_new = {}
new_to_old = {}
for _ in range(times):
	(old, new) = input().split(' ')
	if old in new_to_old:
		old_to_new[new_to_old[old]] = new
		new_to_old[new] = new_to_old[old]
	else:
		old_to_new[old] = new
		new_to_old[new] = old
print(len(old_to_new))
for k in old_to_new:
	print(f'{k} {old_to_new[k]}')


l = []
for _ in range(int(input())):
	l.append(list(input().split()))
c = 0
for i in range(len(l)):
	for j in range(len(l)):
		if i != j:
			if l[i][0] == l[j][1] and l[i][0] != '@':
				l[i][0] = l[j][0]
				(l[j][0], l[j][1]) = ('@', '@')
				c += 1
print(len(l) - c)
for i in l:
	if i[0] == '@':
		pass
	else:
		print(*i)


q = int(input())
ht = {}
num = 0
for i in range(q):
	(old, new) = input().split()
	for i in ht.keys():
		if old == ht[i]:
			ht[i] = new
			break
	else:
		num = num + 1
		ht[old] = new
print(num)
for j in ht.keys():
	print(j, ht[j])


class Person:

	def __init__(self, first_name, actual_name):
		self.actual_name = actual_name
		self.first_name = first_name

	def change_name(self, name):
		self.actual_name = name
person_list = []
n_handles = int(input())
for x in range(n_handles):
	handle = input().split(' ')
	add = True
	for person in person_list:
		if handle[0] == person.actual_name:
			add = False
			person.change_name(handle[1])
	if add:
		person_list.append(Person(handle[0], handle[1]))
print(len(person_list))
for person in person_list:
	print(person.first_name + ' ' + person.actual_name)


q = int(input())
d = {}
for _ in range(q):
	(o, n) = list(map(str, input().strip().split()))
	if o not in d:
		d[o] = n
		d[n] = [o]
	else:
		z = d[o][0]
		d[z] = n
		del d[o]
		d[n] = [z]
print(len(d) // 2)
for i in d:
	if type(d[i]) == str:
		print(i, d[i])


import math

def fact(n):
	ans = 1
	for i in range(2, n + 1):
		ans *= i
	return ans

def comb(n, c):
	return fact(n) // (fact(n - c) * c)
_ = int(input())
arrs = []
for i in range(_):
	(a, b) = map(str, input().split())
	fnd = 0
	for p in arrs:
		if a == p[-1]:
			p.append(b)
			fnd = 1
			break
	if not fnd:
		arrs.append([a, b])
print(len(arrs))
for i in arrs:
	print(i[0], i[-1])


def ff(x, a, s):
	if x[a] == a:
		s.add(a)
		return a
	s.add(x[a])
	return ff(x, x[a], s)

def find(x, a):
	if x[a] == a:
		return x[a]
	x[a] = find(x, x[a])
	return x[a]

def union(x, a, b):
	xx = find(x, a)
	yy = find(x, b)
	if xx != yy:
		x[xx] = yy
d = {}
for _ in range(int(input())):
	(x, y) = map(str, input().split())
	d[x] = y
x = {}
y = {}
s = set()
for (a, b) in d.items():
	s.add(a)
	s.add(b)
for i in s:
	x[i] = i
	y[i] = True
for (a, b) in d.items():
	union(x, a, b)
ans = {}
ss = set()
for (a, b) in d.items():
	if a not in ss:
		ss.add(a)
		ans[a] = ff(x, a, ss)
print(len(ans))
for (a, b) in ans.items():
	print('{} {}'.format(a, b))


user_input = int(input())
oldie = []
newbie = []
for i in range(user_input):
	x = input().split(' ')
	try:
		c = newbie.index(x[0])
		newbie[c] = x[1]
	except:
		oldie.append(x[0])
		newbie.append(x[1])
print(len(oldie))
for i in range(len(oldie)):
	print(oldie[i] + ' ' + newbie[i])


n = int(input())
names = {}
while n:
	n -= 1
	(old, new) = input().split()
	if old in names:
		names[new] = names[old]
		del names[old]
	else:
		names[new] = old
print(len(names))
for (key, value) in names.items():
	print(value, key)


q = int(input())
d = {}
for i in range(q):
	(s, t) = list(map(str, input().split()))
	if s not in d:
		d[t] = s
	else:
		d[t] = d[s]
		d.pop(s)
print(len(d.items()))
for (x, y) in d.items():
	print(y, x)


a = int(input())
old = []
new = []
d = {}
for i in range(a):
	p = input().split()
	old.append(p[0])
	new.append(p[1])
dis = []
for i in range(a):
	if old[i] not in new:
		dis.append(old[i])
print(len(dis))
for i in range(len(dis)):
	p = dis[i]
	q = dis[i]
	for j in range(a):
		if p == old[j]:
			p = new[j]
	s = q + ' ' + p
	print(s)


n = int(input())
d = {}
stat = {}
for i in range(n):
	(a, b) = [x for x in input().split()]
	d[a] = b
	stat[a] = True
	stat[b] = False
ans = []
for i in d.keys():
	if stat[i] == True:
		p = i
		while stat[p] == True:
			stat[p] = False
			p = d[p]
		ans.append([i, p])
print(len(ans))
for i in range(len(ans)):
	print(ans[i][0], ans[i][1])


import sys

def main(in_file):
	n = in_file.readline()
	persons = []
	for line in in_file:
		(old, new) = line.split()
		flag = True
		for names in persons:
			if old in names:
				flag = False
				names.append(new)
		if flag:
			new_person = [old, new]
			persons.append(new_person)
	print(len(persons))
	for person in persons:
		print('{} {}'.format(person[0], person[-1]))
main(sys.stdin)


from sys import stdin
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin

def ii():
	return int(stdin.readline())

def fi():
	return float(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def fmi():
	return map(float, stdin.readline().split())

def li():
	return list(mi())

def lsi():
	x = list(stdin.readline())
	x.pop()
	return x

def si():
	return stdin.readline()
s = set()
d = dict()
a = []
for _ in range(ii()):
	(x, y) = si().split()
	s.add(x)
	d[x] = y
	a.append(x)
ans = dict()
for i in a:
	if i in s:
		s.remove(i)
		p = d[i]
		while p in s:
			s.remove(p)
			p = d[p]
		ans[i] = p
print(len(ans))
for i in ans:
	print(i, ans[i])


x = []
y = []
for i in range(int(input())):
	(s, t) = input().split()
	if s not in x and s not in y:
		x.append(s)
		y.append(t)
	else:
		y[y.index(s)] = t
print(len(x))
for i in range(0, len(x)):
	print(x[i], y[i])


d = {}
for i in range(int(input())):
	(x, y) = input().split()
	if x not in d:
		d[y] = x
	else:
		d[y] = d.pop(x)
print(len(d))
for (i, j) in d.items():
	print(j, i)


h = {}
q = int(input())
for _ in range(q):
	(o, n) = input().split()
	o = h.pop(o, o)
	h.setdefault(n, o)
print(len(h))
for (n, o) in h.items():
	print(o, n)


MyDict = {}
for i in range(int(input())):
	X = list(input().split())
	if X[0] not in MyDict.values():
		MyDict[X[0]] = X[1]
	else:
		for key in MyDict.keys():
			if MyDict[key] == X[0]:
				MyDict[key] = X[1]
				break
print(len(MyDict))
print(*[Key + ' ' + MyDict[Key] for Key in MyDict.keys()], sep='\n')


n = int(input())
old = []
new = []
for i in range(n):
	x = input().split(' ')
	try:
		z = new.index(x[0])
		new[z] = x[1]
	except:
		old.append(x[0])
		new.append(x[1])
print(len(old))
for i in range(len(old)):
	print(old[i] + ' ' + new[i])


q = int(input())
d = {}
for i in range(q):
	(old, new) = input().split()
	d[new] = d.get(old, old)
	d.pop(old, None)
print(len(d))
for (i, j) in d.items():
	print(j, i)


IL = lambda : list(map(int, input().split()))
IS = lambda : input().split()
I = lambda : int(input())
S = lambda : input()
q = I()
mapp = {}
for i in range(q):
	(nbefore, nafter) = IS()
	if nbefore not in mapp:
		mapp[nafter] = nbefore
	else:
		mapp[nafter] = mapp[nbefore]
		mapp.pop(nbefore)
print(len(mapp))
for item in mapp.items():
	print(item[1], item[0])


from sys import stdin, stdout
import math, sys, heapq
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
from os import path
import random
import bisect as bi

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

	def I():
		return int(stdin.readline())

	def In():
		return map(int, stdin.readline().split())

def dict(a):
	d = {}
	for x in a:
		if d.get(x, -1) != -1:
			d[x] += 1
		else:
			d[x] = 1
	return d

def find_gt(a, x):
	i = bi.bisect_left(a, x)
	if i != len(a):
		return i
	else:
		return -1

def main():
	try:
		d = {}
		n = I()
		for x in range(n):
			(a, b) = input().split()
			if d.get(a, -1) == -1:
				d[b] = a
			else:
				temp = d[a]
				d.pop(a)
				d[b] = temp
		print(len(d))
		for x in d:
			print(d[x], x)
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(1):
	main()


from collections import defaultdict
cantidad = int(input())
dic_o = {}
dic_nuevo = {}

class User:

	def __init__(self, nombre):
		self.nombre = nombre
		self.padre = nombre
contador = 0
lista_nombre = []
for i in range(cantidad):
	lista = [str(x) for x in input().split(' ')]
	nombre = lista[0]
	new_name = lista[1]
	if nombre in dic_nuevo.keys():
		dic_nuevo[new_name] = dic_nuevo[nombre]
		dic_o[dic_nuevo[nombre]] = new_name
	else:
		lista_nombre.append(nombre)
		contador += 1
		dic_o[nombre] = new_name
		dic_nuevo[new_name] = nombre
print(contador)
for j in range(contador):
	txt = lista_nombre[j] + ' ' + dic_o[lista_nombre[j]]
	print(txt)


q = int(input())
handles = []
status = True
for i in range(q):
	status = True
	res = input().split(' ')
	if len(handles) == 0:
		handles.append(res)
		continue
	for j in handles:
		if j[1] == res[0]:
			j[1] = res[1]
			status = False
			break
	if status:
		handles.append(res)
print(str(len(handles)))
for k in handles:
	print(k[0] + ' ' + k[1])


x = []
y = []
old = []
notun = []
res = '000000000000000000'
q = int(input(''))
for i in range(q):
	s = input('')
	[s1, s2] = s.split()
	x.append(s1)
	y.append(s2)
for i in range(q):
	if x[i] == res:
		continue
	neww = y[i]
	for j in range(i + 1, q):
		if neww == x[j]:
			neww = y[j]
			x[j] = res
	old.append(x[i])
	notun.append(neww)
print(len(old))
for i in range(len(old)):
	print(old[i], notun[i])


n = int(input())
handles = []
for i in range(n):
	handle = input().split()
	if len(handles) == 0:
		handles.append(handle)
	else:
		for i in range(len(handles)):
			if handle[0] == handles[i][1]:
				handles[i][1] = handle[1]
				break
			elif i == len(handles) - 1:
				handles.append(handle)
print(len(handles))
for i in handles:
	print(i[0], i[1])


q = int(input())
mapping = dict()
for _ in range(q):
	(old, new) = input().split()
	if old in mapping:
		mapping[new] = mapping[old]
		del mapping[old]
	else:
		mapping[new] = old
print(len(mapping))
for (key, value) in mapping.items():
	print(value, key)


lineCnt = int(input())
users = {}
for i in range(0, lineCnt):
	(oldName, newName) = input().split()
	userFound = ''
	for name in users:
		nameList = users[name]
		if oldName in nameList:
			userFound = name
			break
	if len(userFound) > 0:
		users[userFound].append(newName)
	else:
		users[oldName] = []
		users[oldName].append(newName)
print(len(users))
for name in users:
	newName = users[name][-1]
	print(name + ' ' + newName)


numero_lineas = int(input().strip())
querys = []
historial = {}
for linea in range(numero_lineas):
	querys.append(input().strip().split())
for query in querys:
	if query[0] not in historial:
		if query[0] in historial.values():
			for (k, v) in historial.items():
				if query[0] == v:
					historial[k] = query[1]
					break
		else:
			historial[query[0]] = query[1]
print(len(historial))
for (k, v) in historial.items():
	print(k, v)


def changes(ul):
	ol = [u[0] for u in ul]
	nl = [u[1] for u in ul]
	rl = []
	for i in range(len(ul)):
		if ol[i] is None:
			continue
		j = i
		while nl[j] in ol:
			j = ol.index(nl[j])
			ol[j] = None
		rl.append('%s %s' % (ol[i], nl[j]))
	return [len(rl)] + rl
ul = [input().split() for _ in range(int(input()))]
[print(r) for r in changes(ul)]


requests_number = int(input())
original_names = dict()
incumbent_names = dict()
for i in range(requests_number):
	(old, new) = input().rstrip().split(' ')
	if old not in incumbent_names:
		original_names[old] = new
		incumbent_names[new] = old
	else:
		incumbent_names[new] = incumbent_names[old]
		original_names[incumbent_names[old]] = new
		del incumbent_names[old]
print(len(original_names))
for i in original_names:
	print('{} {}'.format(i, original_names[i]))


q = int(input())
di = {}
count = 0
for i in range(q):
	x = input().split()
	(nome2, nome1) = x
	if nome2 not in di.keys():
		count += 1
		di[nome1] = nome2
	else:
		di[nome1] = di[nome2]
		di[nome2] = ' '
print(count)
for (k, v) in di.items():
	if v != ' ':
		print(v, k)


dict = {}
n = int(input())
for i in range(n):
	(old, new) = input().split()
	listOfKeys = [key for (key, value) in dict.items() if value == old]
	if len(listOfKeys) == 0:
		dict[old] = new
	else:
		dict[listOfKeys[0]] = new
print(len(dict))
for (old, new) in dict.items():
	print(old, ' ', new)


n = int(input())
d = dict()
for i in range(n):
	a = list(map(str, input().split()))
	if a[0] not in d:
		d[a[1]] = a[0]
	else:
		d[a[1]] = d[a[0]]
	if a[0] in d:
		del d[a[0]]
print(len(d))
for i in d:
	print(d[i], i)


n = int(input())
d = {}
h = {}
for i in range(n):
	(u, v) = input().split()
	if not u in h:
		h[u] = u
	d[h[u]] = v
	h[v] = h[u]
print(len(d))
for u in d:
	print(u, d[u])


n = int(input())
d = {}
l = []
for j in range(0, n):
	x = [y for y in input().split(' ')]
	if x[0] not in d:
		d[x[0]] = x[1]
		d[x[1]] = x[0]
		l.append(x[0])
	else:
		d[x[1]] = d[x[0]]
		d[d[x[0]]] = x[1]
print(len(l))
for j in l:
	print(j, d[j])


n = int(input())
mapa = {}
for i in range(n):
	(a, n) = input().split()
	if a in mapa:
		mapa.update({n: mapa.pop(a)})
	else:
		mapa.update({n: a})
keys = list(mapa.keys())
print(len(keys))
resultado = []
for i in keys:
	resultado.append(mapa.get(i) + ' ' + i)
print('\n'.join(resultado))


def solve(n, seq):
	used = []
	ans = {}
	temp = {}
	for (old, new) in seq:
		if old not in used:
			if new not in used:
				ans[old] = new
				used.append(new)
				temp[new] = old
			else:
				pass
			used.append(old)
		else:
			og = temp[old]
			if new not in used:
				ans[og] = new
				temp[new] = og
				temp.pop(old)
				used.append(new)
	q = ans.items()
	print(len(q))
	for (i, j) in q:
		print(i, j)
test = int(input())
seq = []
for x in range(test):
	n = input().split()
	seq.append(n)
solve(test, seq)


q = int(input())
current = []
begin = []
for i in range(q):
	(old, new) = input().split()
	for ind in range(len(current)):
		if current[ind] == old:
			current[ind] = new
			break
	else:
		begin.append(old)
		current.append(new)
print(len(begin))
for el in zip(begin, current):
	print(el[0], el[1])


from sys import stderr

def readint():
	return int(input())

def readfloat():
	return float(input())

def readarray(n, f=input):
	return [f() for i in range(n)]

def readlinearray(f=int):
	return list(map(f, input().split()))

def euclide(a, b):
	if a < 0:
		(g, x, y) = euclide(-a, b)
		return (g, -x, y)
	if b < 0:
		(g, x, y) = euclide(a, -b)
		return (g, x, -y)

def gen_primes(max):
	primes = [1] * (max + 1)
	for i in range(2, max + 1):
		if primes[i]:
			for j in range(i + i, max + 1, i):
				primes[j] = 0
	primes[0] = 0
	return [x for x in range(max + 1) if primes[x]]

def is_prime(N):
	if N % 2 == 0:
		return N == 2
	i = 3
	while i * i <= N:
		if N % i == 0:
			return False
		i += 2
	return True

def answer():
	return ''

def gcj():
	case_number = readint()
	for case in range(case_number):
		print('Case #%s: %s' % (case + 1, str(answer)))
n = readint()
start_name = {}
for i in range(n):
	(a, b) = input().split()
	if a not in start_name:
		start_name[b] = a
	else:
		start_name[b] = start_name[a]
		del start_name[a]
print(len(start_name))
for (k, v) in start_name.items():
	print(v, k)


import sys
import math
import itertools
import collections

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=1):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	newnumber = 0
	while number > 0:
		newnumber += number % base
		number //= base
	return newnumber

def cdiv(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

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

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def wr(arr):
	return '\n'.join(map(str, arr))

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)
n = ii()
d = {}
for i in range(n):
	s = input().split()
	if s[0] in d.values():
		for item in d:
			if d[item] == s[0]:
				d[item] = s[1]
	else:
		d[s[0]] = s[1]
print(len(d))
for item in d:
	print(item, d[item])


x = int(input())
dic = {}
s = []
for i in range(x):
	mid = input()
	s.clear()
	s = [a for a in mid.split()]
	if s[1] in dic:
		ss = dic[s[1]]
		dic.pop(s[1])
		dic[s[0]] = ss
	else:
		flag = 0
		for (key, value) in dic.items():
			if value == s[0]:
				dic[key] = s[1]
				flag = 1
		if flag == 0:
			dic[s[0]] = s[1]
print(len(dic))
for (key, value) in dic.items():
	print(key, end=' ')
	print(value)


oldie = {}
newie = {}
for I in range(int(input())):
	(old, new) = map(str, input().split())
	if old in newie:
		temp = newie[old]
		newie[new] = temp
		oldie[temp] = new
	else:
		oldie[old] = new
		newie[new] = old
print(len(oldie))
for (key, value) in oldie.items():
	print(key, value)


orig = []
new = []
for i in range(int(input())):
	inp = list(map(str, input().split()))
	if inp[0] in new:
		new[new.index(inp[0])] = inp[1]
	else:
		orig.append(inp[0])
		new.append(inp[1])
print(len(orig))
for i in range(len(orig)):
	print(orig[i], new[i])


d = {}
n = int(input())
for i in range(n):
	(old, new) = input().split(' ')
	d[old] = new

def findvalue(name, d):
	if name in d.keys():
		return findvalue(d[name], d)
	else:
		return name
r = {}
for (key, value) in d.items():
	if not key in r.keys() and (not value in r.values()):
		v = findvalue(value, d)
		if v not in r.values():
			r[key] = v
print(len(r))
for (key, value) in r.items():
	print('{} {}'.format(key, value))


q = int(input())
d = {}
for i in range(q):
	w = input().split()
	(a, b) = (w[0], w[1])
	if a in d.values():
		for j in d.keys():
			if d[j] == a:
				d[j] = b
				break
	else:
		d[a] = b
print(len(d))
for i in d:
	print(i, d[i])


try:
	t = int(input())
	l = []
	l1 = []
	for i in range(t):
		(x, y) = map(str, input().split())
		if l.count(x) == 0 and l1.count(x) == 0:
			l.append(x)
		if l1.count(x) == 1:
			z = l1.index(x)
			l1[z] = y
		else:
			l1.append(y)
	print(len(l))
	for i in range(len(l)):
		print(l[i], l1[i])
except EOFError:
	pass


import sys
n_inputs = int(sys.stdin.readline())
input_ = [line.strip().split() for line in sys.stdin.readlines()]
changes = []
for (old, new) in input_:
	trigger = False
	for index in range(len(changes)):
		(original, last) = changes[index]
		if last == old:
			changes[index] = [original, new]
			trigger = True
			break
	if not trigger:
		changes.append([old, new])
print(len(changes))
for (original, last) in changes:
	print(original, last)


q = int(input())
dict1 = {}
dict2 = {}
ans = []
answer = ''
for i in range(q):
	(old, new) = map(str, input().split())
	if old not in dict2:
		ans.append(old)
	dict1[old] = new
	dict2[new] = old
print(len(ans))
for i in range(len(ans)):
	f = ans[i]
	answer = ''
	answer += f
	while True:
		if f in dict1:
			f = dict1[f]
		else:
			break
	answer += ' ' + f
	print(answer)


n = int(input())
d = {}
cnt = 0
for i in range(n):
	s = input().split()
	if s[0] in d:
		d[s[1]] = d[s[0]]
		d[s[0]] = 0
	else:
		d[s[1]] = s[0]
		cnt += 1
print(cnt)
d = [list(d.values()), list(d.keys())]
for i in range(len(d[0])):
	if d[0][i] != 0:
		print(d[0][i], d[1][i])


import sys
input = sys.stdin.readline
n = int(input())
d = dict()
d1 = dict()
for i in range(n):
	(old, new) = input().split()
	if d1.get(old, -1) == -1:
		d[old] = new
		d1[new] = old
	else:
		d[d1[old]] = new
		d1[new] = d1[old]
print(len(d))
for i in d.keys():
	print(i, d[i])


import sys
sys.stdin.readline().strip()
currents = {}
length = 0
for line in sys.stdin:
	handles = line.strip().split(' ')
	for key in currents:
		if handles[0] == currents[key]:
			currents[key] = handles[1]
			break
	else:
		currents[handles[0]] = handles[1]
		length += 1
print(length)
for key in currents:
	print(key, currents[key])


n = int(input())
matrix = []
for i in range(n):
	(old, new) = [item for item in input().split()]
	if i == 0:
		matrix.append([old, new])
	else:
		for c in range(len(matrix)):
			ver = 0
			if matrix[c][1] == old:
				matrix[c][1] = new
				ver = 1
				break
		if ver == 0:
			matrix.append([old, new])
tam = len(matrix)
print(tam)
for i in range(tam):
	print(matrix[i][0] + ' ' + matrix[i][1])


n = int(input())
t = []
for x in range(n):
	s = input().split(' ')
	there = False
	for i in range(len(t)):
		if t[i][len(t[i]) - 1] == s[0]:
			there = True
			t[i].append(s[1])
			break
	if not there:
		t.append(s)
print(len(t))
for x in range(len(t)):
	print(t[x][0], t[x][len(t[x]) - 1])


__author__ = 'pxy'
q = int(input())
c = {}
for j in range(q):
	(a, b) = (i for i in input().split())
	if a in c.values():
		c[list(c.keys())[list(c.values()).index(a)]] = b
	else:
		c[a] = b
print(len(c))
for j in c:
	print(j, c[j])


n = int(input())
d = {}
for i in range(n):
	(s, t) = input().split(' ')
	d[s] = t
	if s in d.values():
		del d[s]
		for i in d:
			if d[i] == s:
				d[i] = t
				break
print(len(d))
for i in d:
	print(i, end=' ')
	print(d[i])


cambios = []
q = int(input())
for i in range(q):
	j = input()
	cambios.append(j.split(' '))
soluciones = []
for c in cambios:
	agregado = False
	for s in soluciones:
		if s[::-1][0] == c[0]:
			s.append(c[1])
			agregado = True
	if not agregado:
		soluciones.append([c[0], c[1]])
print(len(soluciones))
for r in soluciones:
	print(r[0], r[-1])


def union(p, q):
	i = find(p)
	j = find(q)
	if i == j:
		return True
	if sz[i] < sz[j]:
		iden[i] = j
		sz[j] += sz[i]
	else:
		iden[j] = i
		sz[i] += sz[j]
	return False

def find(p):
	root = p
	while root != iden[root]:
		root = iden[root]
	while p != root:
		newp = iden[p]
		iden[p] = root
		p = newp
	return root

def dfs(v):
	global с
	s.add(v)
	try:
		dfs(d[v])
	except KeyError:
		с = v
n = int(input())
s = set()
iden = [0] * n
sz = [1] * n
d = {}
b = []
с = 0
for i in range(n):
	a = input().split()
	d[a[0]] = a[1]
for i in d.keys():
	if i not in s:
		dfs(i)
		b.append((i, с))
print(len(b))
for i in b:
	print(i[0], i[1])


from sys import stdin

def solve(names):
	answer = []
	for name in names:
		bool = True
		for answer_name in answer:
			if answer_name[0] == name[1]:
				answer_name[0] = name[0]
				bool = False
			elif answer_name[1] == name[0]:
				answer_name[1] = name[1]
				bool = False
		if bool:
			answer.append([name[0], name[1]])
	return answer
question = stdin.readlines()
while len(question) > 1:
	n_boys = int(question.pop(0))
	names = []
	for i in range(n_boys):
		name = question.pop(0).strip('\n')
		(name_1, name_2) = name.split(' ')
		names.append((name_1, name_2))
	real_names = solve(names)
	print(len(real_names))
	for name in real_names:
		print(name[0], name[1])


from collections import defaultdict, deque, Counter, OrderedDict
import threading, sys

def main():
	n = int(input())
	(d1, d2) = ({}, {})
	for i in range(n):
		(a, b) = input().split()
		if a in d2:
			d1[d2[a]] = b
			d2[b] = d2[a]
		else:
			d1[a] = b
			d2[b] = a
	print(len(d1))
	for i in d1:
		print(i, d1[i])
main()


q = int(input())
l = dict()
for i in range(q):
	(l1, l2) = input().split()
	l[l1] = l2
	l[l2] = ''
s = dict()
for i in l:
	j = l[i]
	while j != '' and l[j] != '':
		r = j
		j = l[j]
		l[r] = ''
	l[i] = j
k = 0
for i in l:
	if l[i] != '':
		k += 1
print(k)
for i in l:
	if l[i] != '':
		print(i, l[i])


n = int(input())
ol = []
ne = []
for i in range(n):
	x = input().split(' ')
	try:
		c = ne.index(x[0])
		ne[c] = x[1]
	except:
		ol.append(x[0])
		ne.append(x[1])
print(len(ol))
for i in range(len(ol)):
	print(ol[i] + ' ' + ne[i])


abacaba = []
abc = []
q = int(input())
d = {}
used = set()
for i in range(q):
	(a, b) = input().split()
	abacaba.append(a)
	abacaba.append(b)
	abc.append(a)
	used.add(b)
	d[a] = b
s = list(set(abacaba))
print(q - len(abacaba) + len(s))
for j in range(len(abc)):
	ans = d[abc[j]]
	while True:
		try:
			ans = d[ans]
		except KeyError:
			break
	if abc[j] not in used:
		print(abc[j], ans)
	else:
		used.add(abc[j])


dic = dict()
numero = int(input())
for i in range(numero):
	palabra = input()
	(palabra1, palabra2) = palabra.split()
	if palabra1 in dic:
		dic[palabra2] = dic[palabra1]
		dic.pop(palabra1)
	else:
		dic[palabra2] = palabra1
print(len(dic))
for i in dic:
	print(dic[i], i)


dp = {}
for _ in range(int(input())):
	(o, n) = input().split(' ')
	if o in dp:
		val = dp.pop(o)
		dp[n] = val
	else:
		dp[n] = o
print(len(dp))
for (i, a) in dp.items():
	print(a, i)


n = int(input())
d = {}
s = set()
for _ in range(n):
	(x, y) = map(str, input().split())
	d[x] = y
	s.add(x)
ans = []
for i in d:
	if i not in s:
		continue
	s.remove(i)
	y = d[i]
	while y in d and y in s:
		s.remove(y)
		y = d[y]
	ans.append([i, y])
print(len(ans))
for j in ans:
	print(*j)


class Node:

	def __init__(self, old):
		self.old = old
		self.last = old
n = int(input())
dict = {}
originals = []
for i in range(n):
	words = input().strip().split(' ')
	old = words[0]
	new = words[1]
	if not old in dict:
		n = Node(old)
		originals.append(n)
		dict[old] = n
	dict[old].last = new
	dict[new] = dict[old]
print(len(originals))
for i in originals:
	print(i.old, i.last)


cambios = int(input())
claves = {}
changes = {}
while cambios != 0:
	actual = input().split(' ')
	if actual[0] not in claves.values() and actual[0] not in claves.keys():
		claves[actual[0]] = actual[1]
		changes[actual[0]] = 1
	elif actual[0] in claves.keys() and changes[actual[0]] == 1:
		claves[actual[0]] = actual[1]
		changes[actual[0]] += 1
	else:
		llave = ''
		for (k, v) in claves.items():
			if v == actual[0]:
				valor = v
				llave = k
		claves[llave] = actual[1]
		changes[llave] += 1
	cambios -= 1
total = 0
for val in changes.values():
	total += 1
print(total)
for (k, v) in claves.items():
	print(k, v)


q = int(input())
key = []
val = []
for _ in range(q):
	temp = input().split(' ')
	key.append(temp[0])
	val.append(temp[1])
i = 0
while i < len(key):
	if val[i] in key:
		j = -1
		while val[i] in key:
			j = key.index(val[i])
			val[i] = val[j]
			del key[j]
			del val[j]
	i += 1
print(len(key))
k = 0
while k < len(key):
	print(key[k], val[k])
	k += 1


n = int(input())
d = {}
visited = {}
arr = []
for i in range(n):
	(a, b) = list(map(str, input().split()))
	d[a] = b
	visited[a] = False
	arr.append([a, b])
ans = {}
for i in arr:
	(a, b) = (i[0], i[1])
	if visited[a] == False:
		visited[a] = True
		key = d[a]
		while True:
			if key in d:
				if visited[key] == False:
					visited[key] = True
					key = d[key]
				else:
					break
			else:
				break
		ans[a] = key
print(len(ans))
for i in ans:
	print(i, ans[i])


E = int(input())
A = []
B = []
C = []
D = []
for i in range(E):
	F = input().split()
	A.append(F[0])
	B.append(F[1])
for i in range(len(A)):
	for j in range(len(B)):
		if A[i] == B[j]:
			break
		elif j == len(B) - 1:
			C.append(A[i])
print(len(C))
for i in range(len(C)):
	D.append(C[i])
i = 0
j = 0
while i < len(C):
	if C[i] == A[j]:
		C[i] = B[j]
		j += 1
	else:
		j += 1
	if j == len(A):
		i += 1
		j = 0
for i in range(len(C)):
	print(D[i], C[i])


q = int(input())
d = {}
used = set()
for _ in range(q):
	(old, new) = input().split()
	parent = d.get(old)
	if parent:
		del d[old]
		d[new] = parent
	else:
		d[new] = old
print(len(d))
for i in d:
	print(d[i], i)


d_n = {}
n = int(input())
for _ in range(n):
	(x, y) = input().split()
	if x in d_n:
		d_n[y] = d_n[x]
		del d_n[x]
	else:
		d_n[y] = x
print(len(d_n))
for key in d_n:
	print('%s %s' % (d_n[key], key))


import math
q = int(input())
m = {}
for _ in range(q):
	(old, new) = map(str, input().split())
	if old not in m:
		m[new] = old
	else:
		m[new] = m[old]
		del m[old]
print(len(m))
for key in m:
	print(m[key], key)


n = int(input())
d = []
for i in range(n):
	(a, b) = input().split()
	f = False
	for x in d:
		if a == x[1]:
			x[1] = b
			f = True
			break
	if not f:
		d.append([a, b])
print(len(d))
for x in d:
	print(x[0], x[1])


n = int(input())
handles = dict()
for i in range(n):
	(old, new) = input().split(' ')
	if old in handles:
		handles[old].append(new)
	else:
		found = False
		for k in handles.keys():
			if old in handles[k]:
				handles[k].append(new)
				found = True
				break
		if not found:
			handles[old] = [new]
print(len(handles))
for k in handles.keys():
	print(k, handles[k][-1])


d = {}
for _ in range(int(input())):
	(o, n) = input().split()
	f = 1
	for v in d.keys():
		if d[v] == o:
			d[v] = n
			f = 0
	if f:
		d[o] = n
print(len(d.keys()))
for v in d.keys():
	print(v, d[v])


def find(x):
	if a[x] not in a.keys():
		return a[x]
	else:
		return find(a[x])
n = int(input())
a = {}
d = {}
for i in range(n):
	(b, c) = input().split()
	a[b] = c
	d[c] = b
e = []
for i in a:
	if i not in d.keys():
		e.append([i, find(i)])
print(len(e))
for i in range(len(e)):
	print(*e[i])


maps = {}
for i in range(int(input())):
	(old, new) = input().split()
	if old in maps:
		maps[new] = maps[old]
		del maps[old]
	else:
		maps[new] = old
print(len(maps))
for (k, v) in maps.items():
	print(v + ' ' + k)


n = int(input())
d = {}
count = 0
for i in range(n):
	(old, new) = input().split()
	if old in d:
		val = d.pop(old)
		d[new] = val
	else:
		d[new] = old
		count += 1
print(count)
for i in d:
	print(d[i], i)


from collections import Counter as c
a = c()
b = int(input())
for i in range(b):
	d = input().split()
	e = True
	for j in a.keys():
		if a[j] == d[0]:
			a[j] = d[1]
			e = False
			break
	if e:
		a[d[0]] = d[1]
print(len(a))
for i in a.keys():
	print(i, a[i])


d = {}
for _ in range(int(input())):
	b = 1
	l = input().split()
	for (i, j) in d.items():
		if l[0] == j:
			d[i] = l[1]
			b = 0
			break
	if b:
		d[l[0]] = l[1]
print(len(d))
for (i, j) in d.items():
	print(i, j)


n = int(input())
nombres = {}
for i in range(n):
	a = input()
	if a.split(' ')[0] not in nombres.keys():
		nombres[a.split(' ')[0]] = [a.split(' ')[1], 0]
nombres_visitados = []
imprimir = []
for i in nombres.keys():
	nombre_actual = i
	if nombres[i][1] == 0:
		while True:
			nombres_visitados.append(nombre_actual)
			nombres_pasados = [i]
			nombres[nombre_actual][1] = 1
			if nombres[nombre_actual][0] not in nombres.keys() and nombres[nombre_actual][0] not in nombres_pasados:
				imprimir.append(i + ' ' + nombres[nombre_actual][0])
				break
			nombre_actual = nombres[nombre_actual][0]
print(len(imprimir))
for i in imprimir:
	print(i)


def get_key(d, value):
	for (k, v) in d.items():
		if v == value:
			return k
n = int(input())
d = {}
for i in range(n):
	(s1, s2) = input().split()
	if s1 in d.values():
		d[get_key(d, s1)] = s2
	else:
		d[s1] = s2
print(len(d))
for (k, v) in d.items():
	print(k, v)


n = int(input())
dic = {}
for i in range(n):
	(a, b) = input().split()
	if a in dic:
		dic[b] = dic.pop(a)
	else:
		dic[b] = a
print(len(dic))
for i in dic:
	print(dic[i], i)


n = int(input())
V = []
L = []
L1 = []
for i in range(n):
	M = input().split()
	if M[0] not in L1:
		L.append(M[0])
		V.append(M[0])
		if M[1] in V:
			L1.append(M[0])
		else:
			L1.append(M[1])
			V.append(M[1])
	elif M[1] not in V:
		V.append(M[1])
		L1[L1.index(M[0])] = M[1]
k = 0
for i in range(len(L)):
	if L[i] != L1[i]:
		k = k + 1
print(k)
for i in range(len(L)):
	if L[i] != L1[i]:
		print(L[i], L1[i])


inCount = int(input())
a = {}
for i in range(inCount):
	(curStr1, curStr2) = input().split()
	if curStr1 not in a.keys():
		a[curStr2] = curStr1
	else:
		prevValue = a[curStr1]
		a.pop(curStr1)
		a[curStr2] = prevValue
print(len(a))
for (key, value) in a.items():
	print(value, key)


q = int(input())
d = {}
for i in range(q):
	(old, new) = input().split()
	d[old] = new
	d[new] = None
arr = []
s = set()
for i in d:
	if d[i] is not None:
		subs = d[i]
		while d[subs] is not None:
			subs = d[subs]
		if i not in s and subs not in s:
			arr.append([i, subs])
			s.add(i)
			s.add(subs)
print(len(arr))
for i in arr:
	print(*i)


n = int(input())
handles = {}
for e in range(n):
	(old, new) = map(str, input().split())
	if old in handles:
		handles[new] = handles[old]
		handles.pop(old)
	else:
		handles[new] = old
print(len(handles))
for e in handles:
	print(handles[e], end=' ')
	print(e)


t = int(input())
d = {}
while t:
	flag = 0
	(k, v) = input().split()
	for (k1, v1) in d.items():
		if k == v1:
			flag = 1
			d[k1] = v
			break
	if flag == 0:
		d[k] = v
	t -= 1
print(len(d))
for (k, v) in d.items():
	print(k, v)


n = int(input())
arr = dict()
arr_ = dict()
for i in range(n):
	s = input()
	s = s.split()
	arr[s[0]] = s[1]
	arr[s[1]] = 0
	arr_[s[1]] = s[0]
ans = []
for i in arr:
	if i not in arr_:
		word = i
		while arr[word] != 0:
			word = arr[word]
		ans.append([i, word])
print(len(ans))
for i in ans:
	print(i[0], i[1])


n = int(input().strip())
d = {}
nm = {}
count = 0
for _ in range(n):
	(o, n) = [str(a_temp) for a_temp in input().strip().split()]
	if o not in d:
		d[o] = o
		count += 1
	parent = d[o]
	d[n] = parent
	nm[parent] = n
print(count)
for (k, v) in nm.items():
	print('{0} {1}'.format(k, v))


q = int(input())
parent = {}
while q:
	(a, b) = map(str, input().split())
	parent[a] = b
	q -= 1
visited = set()
ans = []
for name in parent:
	temp = name
	while temp in parent and temp not in visited:
		visited.add(temp)
		temp = parent[temp]
	if name != temp:
		ans.append((name, temp))
print(len(ans))
for (a, b) in ans:
	print(a, b)


q = int(input())
d = {}
for i in range(q):
	(old, new) = input().split()
	t = d.get(old, old)
	d[new] = t
	if t != old:
		del d[old]
print(len(d))
for x in d:
	print(d[x], x)


n = int(input())
result = dict()
for i in range(n):
	tmp = input().split()
	k = tmp[0]
	v = tmp[1]
	if k in result.values():
		tmp = list(result.values()).index(k)
		result[list(result.keys())[tmp]] = v
	else:
		result[k] = v
print(len(result))
for i in result:
	print(i + ' ' + result[i])


n = int(input())
o = []
l = []
for i in range(n):
	(s1, s2) = input().split()
	if s1 in l:
		ind = l.index(s1)
		l[ind] = s2
	else:
		o.append(s1)
		l.append(s2)
print(len(o))
for i in range(len(o)):
	print(o[i], end=' ')
	print(l[i])


n = int(input())
l = [[] for i in range(n)]
st = set()
for i in range(n):
	(ini, cur) = [x for x in input().split()]
	if ini in st:
		for i in range(n):
			if l[i][-1] == ini:
				l[i].append(cur)
				st.add(cur)
				break
	else:
		for i in range(n):
			if len(l[i]) > 0:
				continue
			l[i].append(ini)
			l[i].append(cur)
			st.add(ini)
			st.add(cur)
			break
ans = []
for i in range(len(l)):
	if len(l[i]) > 0:
		ans.append([l[i][0], l[i][-1]])
	else:
		break
print(len(ans))
for i in ans:
	print(*i)