Bicycle racing
题号:NC23782
时间限制:C/C++/Rust/Pascal 5秒,其他语言10秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

There are n villages and n unidirectional roads in the town. Every road connects two villages and every village has only one road start from it to another village or itself. The town will hold a bicycle racing in June, the commitee want to find a racing path that passes n villages(some villages may appear more than once). There must be a road between every two adjacent villages on the path.
We know every village has its lucky letter. So a path can be represented as a string consists of n letters. Among all the possible path, they prefer to choose the one whose string is k-th smallest. Find the string for them.

输入描述:

The input contains multiple test cases and the first line provides an integer up to 50 indicating to the total numberof test cases.
For each test case, the first line contains the integer n (1 ≤ n ≤ 200000). The second line contains a string length of n, which gives the lucky letter for each village. The third line contains n integer {ai}(0<=i<n, 0<=ai<n), ai means that there is a unidirectional road from the i-th village to the ai-th village.
The last line contains a integer k(1<=k<=n).
The summation of N is smaller than 1000000.

输出描述:

For each test case, you should output the label of the case first.
Then you are to output exactly N letters which are the string of the k-th smallest path.
示例1

输入

复制
2
3
abc
1 2 0
2
5
WallE
1 2 0 4 3
4

输出

复制
Case #1: bca
Case #2: lElEl