#include<stdio.h>
#include<string.h>
int main()
{
	int n, m;
	int arr[ 205 ], arr1[ 205 ];
	char carr[ 205 ], carr1[ 205 ];
	int i, j, k, d, c;
	while( 1 )
	{
		scanf( "%d", &n );
		if( n == 0 )
			break;
		m = n;
		memset( arr, 0, sizeof( int ) *205 );
		memset( arr1, 0, sizeof( int ) * 205 );

		i = 0;
		while( n > 0 )
		{
			scanf( "%d", &arr[ i ++ ] );
			n--;
		}

		for( i = 0; i < m; i++ )
		{
			d = arr[ i ];
			j = 2;
			while( arr[ d - 1 ] != i + 1 )
			{
				d = arr[ d - 1 ];
				j++;
			}
			arr1[ i ] = j;
		}

		while( scanf( "%d", &c ) && c != 0 )
		{
			getchar();
			memset( carr, 0, sizeof( char ) * 205 );
			gets( carr );
			memset( carr1, 0, sizeof( char ) * 205 );
			for( i = strlen( carr ); i < m; i++)
			{
				carr[ i ] = ' ';
			}
			for( i = 0; i < m; i++ )
			{
				j = c % arr1[ i ];
				if( j == 0 )
					carr1[ i ] = carr[ i ];
				else
				{
					d = i + 1;
					while( j > 0 )
					{
						d = arr[ d - 1 ];
						j--;
					}
					carr1[ d -1 ] = carr[ i ];
				}
			}
			printf( "%s\n", carr1 );
		}
		printf( "\n" );
	}
	return 0;
}