eolymp
bolt
Спробуйте наш новий інтерфейс для відправки розв'язків
Задачі

Redirect

Redirect

It is well known that Web search deals with electronic documents available at certain addresses (links) on the net. However, the address of a document is not necessarily unique, and a single document may be accessed through multiple links. Let us consider a special case of this situation -- link redirection. When the client requests a page at address \textbf{A}, the server responds with a special code providing the address of a new page \textbf{B}, and the client navigates to this address. This process is known as redirection. Page \textbf{B} may as well redirect the client to a new page \textbf{C} and so on. Thus, the same document is made available at addresses \textbf{A}, \textbf{B} and \textbf{C}. Redirection data can come from various sources, e. g. from a web crawler that takes link \textbf{A} during scheduled crawling and goes through the whole chain of redirects. Another source may be Twitter API: tweets always contain so-called shortened links (tinylinks), and links to the original documents can be obtained from the extended data in tweets. If you have multiple sources, conflicts may arise: suppose, first we obtained data from Twitter (\textbf{A->B}), then the crawler takes link \textbf{A} leading in \textbf{D}. Alternatively, the service may have been overloaded, and at first the crawler received an error attempting to retrieve document A, though later the service became operational, and the crawler reached the target document at the second attempt. Also, the link may become invalid if the document is outdated. If we want to compute some statistics for the document, say, the number of links to this document, we need a way to bring all the references to some "common denominator". The most practical "denominator" is the final link in the redirect chain. \textbf{U} is said to be the final link in the redirect chain if there is no redirect \textbf{U->V} leading to link \textbf{V}. The final link in the redirect chain will be also called the true address of the document. You are given information about \textbf{N} redirects, each being a pair \textbf{i v_i>} (\textbf{1} ≤ \textbf{i} ≤ \textbf{N},), where \textbf{u_i} is the original link, and \textbf{v_i} is the link this redirect leads to. The redirect may come in the form \textbf{i NULL>} meaning that link \textbf{u}_\{i \}is invalid. The links are given in the order of entry, i. e., the redirect occurring later in the list contains more up-to-date information. Write a program to process the available information about redirects and to determine the true address of the document for each of the M links. When solving the problem, the following conflicts may arise: \begin{itemize} \item if the chain of redirects for some link \textbf{U} is looped we suppose that the true address of this link is \textbf{NULL}; \item if several redirection options exist for some link \textbf{U} we consider the latest redirect as the correct one; \item if there is no information about redirects for some link \textbf{U} we suppose that the true address of this link is \textbf{U}. \end{itemize} \InputFile The first line of the input file contains integer \textbf{N}. Each of the following \textbf{N} lines contains two links \textbf{u_\{i \}v_i} separated by one or more space characters. Line number \textbf{N+2} contains integer \textbf{M}. The following \textbf{M} lines contain links \textbf{q_i}, for which the true addresses are to be found. \textbf{1} ≤ \textbf{N}, \textbf{M} ≤ \textbf{100000} \textbf{0} < \textbf{|v_i|}, \textbf{|u_i|}, \textbf{|q_i|} ≤ \textbf{10}. (The length of each \textbf{v_i}, \textbf{u_i}, \textbf{q_i} will not exceed \textbf{10} characters.) Links may contain uppercase and lowercase letters, numbers, and the following \textbf{15} characters listed inside the quotation marks: "\textbf{&?!#;().\%:+-_~/}". \OutputFile The output file should contain \textbf{M} lines, each being the true address of link \textbf{q_i}.
Ліміт часу 3 секунди
Ліміт використання пам'яті 64 MiB
Вхідні дані #1
2
youtu.be/J NULL
t.co/iqrw youtu.be/J
1
t.co/iqrw

Вихідні дані #1
NULL
Джерело 2013-2014 ACM Central Region of Russia Quarterfinal, Rybinsk 2013/10/17