#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6;
int t, n, k, dp[maxn], ans[5];
int main() {
cin >> t;
while (t--) {
cin >> n >> k;
dp[1] = 0;
for (int i = 2; i <= n; i++) dp[i] = (dp[i-1]+k)%i;
ans[1] = dp[n]+1;
dp[2] = !(k%2);
for (int i = 3; i <= n; i++) dp[i] = (dp[i-1]+k)%i;
ans[2] = dp[n]+1;
bool vis[3] = {0};
vis[(k%2+k)%3] = 1, vis[((!(k%2))+k)%3] = 1;
for (int i = 0; i < 3; i++) if (!vis[i]) dp[3] = i;
for (int i = 4; i <= n; i++) dp[i] = (dp[i-1]+k)%i;
ans[3] = dp[n]+1;
printf("%d %d %d\n", ans[3], ans[2], ans[1]);
}
return 0;
}