#include //#include using namespace std; // using namespace atcoder; // using mint = modint1000000007; // const int mod = 1000000007; // using mint = modint998244353; // const int mod = 998244353; // const int INF = 1e9; // const long long LINF = 1e18; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i, l, r) for (int i = (l); i < (r); ++i) #define rrep(i, n) for (int i = (n)-1; i >= 0; --i) #define rrep2(i, l, r) for (int i = (r)-1; i >= (l); --i) #define all(x) (x).begin(), (x).end() #define allR(x) (x).rbegin(), (x).rend() #define P pair template inline bool chmax(A &a, const B &b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A &a, const B &b) { if (a > b) { a = b; return true; } return false; } #ifndef KWM_T_MATH_DIOPHANTINE_HPP #define KWM_T_MATH_DIOPHANTINE_HPP #include #include namespace kwm_t::math { /** * @brief 拡張ユークリッドの互除法 * * ax + by = gcd(a,b) を満たす (x,y) を返す */ long long ext_gcd(long long a, long long b, long long& x, long long& y) { if (b == 0) { x = 1; y = 0; return a; } long long x1, y1; long long g = ext_gcd(b, a % b, x1, y1); x = y1; y = x1 - (a / b) * y1; return g; } /** * @brief 線形ディオファントス方程式 ax + by = c * * return: * {x0, y0} 1つの解(存在しない場合 {-1,-1}) * * 注意: * ・一般解は x = x0 + t*(b/g), y = y0 - t*(a/g) * ・x >= 0 最小化などは別処理 */ std::pair linear_diophantine(long long a, long long b, long long c) { long long x, y; long long g = ext_gcd(a, b, x, y); if (c % g != 0) return { -1, -1 }; long long mul = c / g; x *= mul; y *= mul; return { x, y }; } /** * @brief ax ≡ c (mod b) の解(存在すれば1つ) * * return: * x ≡ solution (mod b/g) * なければ -1 * verified * https://atcoder.jp/contests/abc423/submissions/74801048 */ long long mod_linear_equation(long long a, long long b, long long c) { long long x, y; long long g = ext_gcd(a, b, x, y); if (c % g != 0) return -1; long long mod = b / g; long long res = (x * (c / g)) % mod; if (res < 0) res += mod; return res; } } // namespace kwm_t::math #endif void solve() { int n, p; cin >> n >> p; vectora(n); rep(i, n) { int x; cin >> x; x--; a[x] = i; } vectorb(n, -1); vector>keep(n + 1); vectorvis(n); rep(i, n)if (!vis[i]) { int v = i; vectorvs; while (!vis[v]) { vis[v] = true; vs.push_back(v); v = a[v]; } int sz = vs.size(); if (sz % 2) { int x = kwm_t::math::mod_linear_equation(2 * p, sz, 1); rep(i, sz) b[vs[i]] = vs[(i + x) % sz]; } else if (keep[sz].empty()) { keep[sz].swap(vs); } else { vectorvs2; vs2.swap(keep[sz]); int x = kwm_t::math::mod_linear_equation(p, sz, 1); rep(i, sz) { b[vs[i]] = vs2[i]; b[vs2[i]] = vs[(i + x) % sz]; } } } // 後処理 rep(i, n + 1) { if (keep[i].empty())continue; auto vs = keep[i]; int sz = vs.size(); int x = kwm_t::math::mod_linear_equation(2 * p, sz - 1, 1); rep(i, sz - 1) b[vs[i]] = vs[(i + x) % (sz - 1)]; // ハブったやつの行き先を決めないといけない b[vs.back()] = vs[(sz - 2 + x) % (sz - 1)]; } for (auto e : b)cout << e + 1 << " "; cout << endl; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int t; cin >> t; while (t--)solve(); return 0; }