// Arup Guha
// 3/16/2017
// Solution to 2017 UCF HS Contest Problem: Every Day I'm Shuffling

import java.util.*;

public class shuffling {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int numCases = stdin.nextInt();

		// Process each case.
		for (int loop=0; loop<numCases; loop++) {

			// Get input.
			int n = stdin.nextInt();
			int k = stdin.nextInt();
			int[] perm = new int[n];
			for (int i=0; i<n; i++) perm[i] = stdin.nextInt()-1;

			// Solve it.
			int[] res = apply(perm, k);

			// Output result - can speed up with StringBuffer if you want...
			for (int i=0; i<n-1; i++)
				System.out.print((res[i]+1)+" ");
			System.out.println(res[n-1]+1);
		}
	}

	// Returns the identity function.
	public static int[] identity(int n) {
		int[] res = new int[n];
		for (int i=0; i<n; i++) res[i] = i;
		return res;
	}

	public static int[] apply(int[] f, int k) {

		if (k == 0) return identity(f.length);

		// Apply the function once, which is itself.
		if (k == 1) return Arrays.copyOf(f, f.length);

		// Compose it with itself.
		if (k == 2) {
			int[] res = new int[f.length];
			for (int i=0; i<f.length; i++)
				res[i] = f[f[i]];
			return res;
		}

		// Speed up with divide by 2 trick!
		if (k%2 == 0) {
			int[] half = apply(f, k/2);
			return apply(half, 2);
		}

		// Get all but one application of the original function.
		int[] almost = apply(f, k/2);
		almost = apply(almost, 2);

		// Go one more step.
		int[] res = new int[f.length];
		for (int i=0; i<f.length; i++)
			res[i] = almost[f[i]];

		return res;
	}
}