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.
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.
'''
# Submitted By M7moud Ala3rj
Don't Copy This Code, CopyRight . [email protected] © 2022-2023 :)
'''
# Problem Name = "Misha and Changing Handles"
# Class: B

import sys
sys.setrecursionlimit(2147483647)
input = sys.stdin.readline
def print(*args, end='\n', sep=' ') -> None:
    sys.stdout.write(sep.join(map(str, args)) + end)

def dfs(node):
    global graph, seen
    if node in graph.keys():
        child = graph[node]
        if child not in seen:
            seen.add(child)
            return dfs(child)

    return node


def Solve():
    global graph, seen
    graph = {}
    for q in range(int(input())):
        old, new = input().split() ; graph[old] = new
    
    seen = set()
    for i in graph.keys():
        if i not in seen:
            print(i, dfs(i))




if __name__ == "__main__":
    for t in range(int(input())):
        Solve()