cswork / OAOpen practice workspace

MicrosoftOA #53

Order Records by Matching Start and End

Estimated EasyJudge-ready

Topics (AI-labelled)Hashing

Records (start,end,payload) form exactly one chain, in which the end of each record equals the start of the next. The input is shuffled; concatenate the payloads in chain order. Spaces in the content must be kept exactly as they are.

This site uses a standard input/output protocol.

Input

The first line contains N, and each record then takes two lines: start end, and the full payload. 1≤N≤200000; the source does not restrict the strings, so this site uses keys of 1..12 ASCII alphanumeric characters and payloads of 0..10 visible ASCII characters or spaces. All records are guaranteed to form exactly one chain with no cycles and no branches.

Output

Output the full concatenated content followed by a terminating newline; if the content is empty, output an empty line.

Example 1

Input:

3
aaa bbb
2
bbb ccc
1
ccc ddd
3

Output:

213

Example 2

Input:

1
p q
X

Output:

X

Example 3

Input:

2
b c
b 
a b
 a

Output:

 ab 

Example notes

Example 1: the chain aaa→bbb→ccc→ddd concatenates to 213. Example 2: the only record outputs X. Example 3: first a→b, then b→c, concatenating to " ab " with a leading and a trailing space.

Editorial

Loading solution…