#include <bits/stdc++.h>
using namespace std;
class Solution {
 public:
  vector<int> solve(int n, long long K) {
    const long long MAX = 1LL << 61;
    vector<long long> factorials(n + 1, 1);
    for (int i = 2; i <= n; ++i) {
      if (factorials[i - 1] <= MAX / i) {
        factorials[i] = factorials[i - 1] * i;
      } else {
        factorials[i] = MAX;
      }
    }
    auto count = [&](int n) { return n < 2 ? 1 : factorials[n - 2]; };
    vector<long long> dp(n + 1, 0);
    dp[0] = 1;
    for (int i = 1; i <= n; ++i) {
      for (int l = 1; l <= i; ++l) {
        if (count(l) <= MAX / dp[i - l]) {
          dp[i] += dp[i - l] * count(l);
        }
        if (dp[i] >= MAX) {
          dp[i] = MAX;
          break;
        }
      }
    }
    --K;
    if (K >= dp[n]) {
      return {};
    }
    vector<bool> used(n, false);
    vector<int> res(n, 0);
    iota(res.begin(), res.end(), 0);
    auto count2 = [&](int l, int r) {
      vector<int> id(n, -1);
      int cnt = 0;
      for (int i = l; i <= r; ++i) {
        if (res[i] == i) {
          ++cnt;
        }
      }
      for (int i = l; i <= r; ++i) {
        if (id[i] != -1) {
          continue;
        }
        int cur = i;
        while (true) {
          if (id[cur] == i) {
            return 0LL;
          }
          id[cur] = i;
          int nxt = res[cur];
          if (nxt == cur) {
            break;
          }
          cur = nxt;
        }
      }
      return cnt - 1 < 0 ? 1 : factorials[cnt - 1];
    };
    for (int i = 0; i < n; ++i) {
      for (int j = i; j < n; ++j) {
        if (!used[j]) {
          long long cnt = MAX;
          long long P = dp[n - j - 1];
          if (count(j - i + 1) <= MAX / P) {
            cnt = count(j - i + 1) * P;
          }
          if (K < cnt) {
            res[i] = j;
            used[j] = true;
            for (int l = i + 1, m = j - i - 1; l <= j; ++l, --m) {
              for (int d = i; d <= j; ++d) {
                if (!used[d] && d != l) {
                  long long cnt2 = MAX;
                  res[l] = d;
                  long long cnt3 = count2(i, j);
                  if (cnt3 <= MAX / P) {
                    cnt2 = cnt3 * P;
                  }
                  if (K < cnt2) {
                    used[d] = true;
                    break;
                  } else {
                    K -= cnt2;
                  }
                }
              }
            }
            i = j;
            break;
          } else {
            K -= cnt;
          }
        }
      }
    }
    return res;
  }
};
int main(int argc, char** argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  Solution sol;
  int t;
  cin >> t;
  while (t-- > 0) {
    int n;
    long long k;
    cin >> n >> k;
    auto res = sol.solve(n, k);
    if (res.size() != n) {
      cout << -1 << "\n";
    } else {
      for (auto r : res) {
        cout << r + 1 << " ";
      }
      cout << "\n";
    }
  }
  return 0;
}
