#include using namespace std; using int64 = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { long long N, K; cin >> N >> K; long long k0 = K - 1; // 0-index long long per_a = 72LL * N; // 9 * (8N) long long a_idx = k0 / per_a; // 0..8 int a = (int)a_idx + 1; k0 %= per_a; long long per_ab = 8LL * N; long long b_idx = k0 / per_ab; // 0..8 long long r = k0 % per_ab; // rank inside (a,b) // map b_idx -> b in sorted digits excluding a int b = -1; { int cnt = 0; for (int d = 0; d <= 9; d++) { if (d == a) continue; if (cnt == b_idx) { b = d; break; } cnt++; } } // build C_low and C_high (digits excluding a,b) vector low, high; for (int d = 0; d <= 9; d++) { if (d == a || d == b) continue; if (d < b) low.push_back(d); else if (d > b) high.push_back(d); } long long L = (long long)low.size(); long long H = (long long)high.size(); // = 8 - L long long t; int c; if (L > 0 && r < L * N) { t = r / L; // 0..N-1 long long idxc = r % L; c = low[(size_t)idxc]; } else { long long r2 = r - L * N; // if L==0, this is r // H must be > 0 because L is 0..8 and total is 8 t = (N - 1) - (r2 / H); // descending long long idxc = r2 % H; c = high[(size_t)idxc]; } long long len = t + 3; // |U| = t + 3 cout << len << " " << a << " " << b << " " << c << "\n"; } return 0; }