Задачі
VAN DINSKY
VAN DINSKY
You must help would-be painter Vincent van Dinsky mix colors for his paintings. His master gave him a book of color mixing rules plus a color palette, and told him to produce the set of colors that are required for a painting, by experimenting until he finds the minimum number of color mixes that is required for each color in the painting.
\textit{\textbf{Notes}}:
\begin{itemize}
\item all colors are named using the characters \textbf{a-z 0-9} (lowercase)
\item a color mixing rule is made by three colors written on the same line, meaning "\textbf{color1 mixed with color2 gives color3}" -- e.g. "\textbf{yellow cyan green}"
\item mr. van Dinsky is not willing to do random color mixing experiments -- if he doesn’t find in the book what is the result of mixing yellow with green, he will never attempt to do this operation. He is also very bad at logic inference -- e.g. if the book says that \textbf{yellow+cyan=green}, \textbf{yellow+magenta=red} and \textbf{red+cyan=black}, he will not assume that \textbf{green+magenta=black} (unless the book explicitly tells what is the result of mixing \textbf{green & magenta}). On the other hand -- he is fully aware that it doesn’t matter what color you start with -- mixing \textbf{yellow & green} is the same as mixing \textbf{green & yellow}.
\end{itemize}
\InputFile
For an input that contains, in order
\begin{itemize}
\item all the color mixing rules in the book
\item an empty line
\item one or several data sets (painting tasks), made of two lines each:
\begin{itemize}
\item all the colors available initially on the palette
\item all the colors required for the painting
\end{itemize}
\end{itemize}
\OutputFile
You should produce an output that contains one line for each painting (i.e. each data set); on that line, for each color in the painting, the minimum number of color mixes that will produce the desired color (starting from the colors available initially on the palette; the result is '\textbf{-1}' if it’s impossible to obtain the desired color).
You can assume that the input is correct, there are less than \textbf{50000} color mixing rules in the rule book and less than 1000 colors.
Вхідні дані #1
cyan yellow green cyan magenta blue yellow magenta red red green black cyan yellow magenta pink cyan black pink red brown cyan yellow red blue black
Вихідні дані #1
0 3 0 1 -1 -1 2