#include <bits/stdc++.h>
using namespace std;
int ri() {
  int x;
  if (scanf(" %d", &x) == EOF) return -1;
  return x;
}
long long int rl() {
  long long int x;
  if (cin >> x) return x;
  return -1;
}
string rs() {
  string s;
  if (cin >> s) return s;
  return "";
}
const long long int inf = 2e18;
struct UnionFind {
  const int n;
  vector<int> parent;
  UnionFind(int n) : n(n), parent(n, -1) {
    for (int i = 0; i < n; i++) parent[i] = i;
  }
  void merge(int u, int v) {
    int U = findSet(u);
    int V = findSet(v);
    parent[U] = V;
  }
  int findSet(int u) {
    if (parent[u] == u) return u;
    return parent[u] = findSet(parent[u]);
  }
  void print() {
    return;
    for (int i = 0; i < n; ++i) {
      printf("%d ==> %d\n", i, parent[i]);
    }
  }
};
struct SingleCase {
  vector<long long int> goodPerms;
  vector<long long int> fact;
  long long int getGoodPerms(int n) {
    if (n == 0) return 1;
    long long int& ret = goodPerms[n];
    if (ret != -1) return ret;
    ret = 0;
    for (int i = 1; i <= n; i++) {
      long long int a = mul(numDerangements(i), getGoodPerms(n - i));
      ret = add(ret, a);
    }
    return ret;
  }
  vector<int> getDerangement(int n, long long int k) {
    if (n == 1) return vector<int>(1, 0);
    if (n == 2) {
      vector<int> v(2);
      v[0] = 1;
      v[1] = 0;
      return v;
    }
    UnionFind uf(n);
    vector<int> taken(n, 0);
    vector<int> v(n, -1);
    v[0] = n - 1;
    taken[n - 1] = 1;
    uf.merge(0, n - 1);
    uf.print();
    for (int idx = 1; idx < n; idx++) {
      bool found = false;
      for (int i = 0; i < n - 1; i++) {
        long long int cur = numDerangements(n - idx);
        if (taken[i]) continue;
        if (i == idx) continue;
        if (idx + 1 != n && uf.findSet(i) == idx) continue;
        if (cur > k) {
          v[idx] = i;
          taken[i] = 1;
          uf.merge(idx, i);
          found = true;
          break;
        } else {
          k -= cur;
        }
      }
      assert(found);
      uf.print();
    }
    return v;
  }
  long long int numDerangements(int n) {
    if (n <= 2) return 1;
    long long int& ret = fact[n];
    if (ret != -1) return ret;
    ret = mul(n - 2, numDerangements(n - 1));
    return ret;
  }
  long long int add(long long int x, long long int y) {
    if (x == inf || y == inf || x + y >= inf) return inf;
    return x + y;
  }
  long long int mul(long long int x, long long int y) {
    if (x == 0 || y == 0) return 0;
    if (x == inf || y == inf) {
      return inf;
    }
    if (x >= (inf + y - 1) / y) return inf;
    return x * y;
  }
  bool sufficient(long long int a, long long int b, long long int k) {
    return mul(a, b) > k;
  }
  vector<int> getKth(int n, long long int k) {
    fact = vector<long long int>(n + 1, -1);
    goodPerms = vector<long long int>(n + 1, -1);
    int taken = 0;
    vector<int> ret;
    while (taken < n) {
      bool found = false;
      for (int i = 1; i + taken <= n; i++) {
        long long int A = numDerangements(i);
        long long int B = getGoodPerms(n - taken - i);
        if (sufficient(A, B, k)) {
          vector<int> v = getDerangement(i, k / B);
          for (auto x : v) ret.push_back(taken + 1 + x);
          taken += i;
          k = k % B;
          found = true;
          break;
        } else {
          k -= A * B;
        }
      }
      if (!found) return vector<int>();
    }
    return ret;
  }
  bool solveCase() {
    int n = ri();
    if (n < 0) return false;
    long long int k = rl() - 1;
    vector<int> v = getKth(n, k);
    if (v.empty())
      cout << -1 << endl;
    else {
      for (auto x : v) cout << x << " ";
      cout << endl;
    }
    return true;
  }
};
int main() {
  ri();
  while (SingleCase().solveCase()) {
  };
  return 0;
}
