/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Egor Kulikov
 */

#include <bits/stdc++.h>

using namespace std;

template<typename T>
class Vector : public vector<T> {
    using parent = vector<T>;
public:
    Vector() : parent() {}

    explicit Vector(size_t __n) : parent(__n) {}

    Vector(size_t __n, const T &__value) : parent(__n, __value) {}

    explicit Vector(const parent &__x) : parent(__x) {}

    Vector(const Vector &__x) : parent(__x) {}

    Vector(Vector &&__x) noexcept : parent(move(__x)) {}

    Vector(initializer_list<T> __l) : parent(__l) {}

    template<typename _InputIterator, typename = std::_RequireInputIter<_InputIterator>>
    Vector(_InputIterator __first, _InputIterator __last) : parent(__first, __last) {}

    const T &operator[](size_t ind) const {
#ifdef LOCAL
        if (ind >= parent::size()) {
            throw "Out of bounds";
        }
#endif
        return *(parent::_M_impl._M_start + ind);
    }

    T &operator[](size_t ind) {
#ifdef LOCAL
        if (ind >= parent::size()) {
            throw "Out of bounds";
        }
#endif
        return *(parent::_M_impl._M_start + ind);
    }

    Vector<T> &operator=(Vector<T> &&__x) noexcept {
        parent::operator=(__x);
        return *this;
    }

    Vector<T> &operator=(const Vector<T> &__x) {
        parent::operator=(__x);
        return *this;
    }
};

template<>
class Vector<bool> : public vector<bool> {
    using parent = vector<bool>;
public:
    Vector() : parent() {}

    explicit Vector(size_t __n) : parent(__n) {}

    Vector(size_t __n, const bool &__value) : parent(__n, __value) {}

    explicit Vector(const parent &__x) : parent(__x) {}

    Vector(const Vector &__x) : parent(__x) {}

    Vector(Vector &&__x) noexcept : parent(move(__x)) {}

    Vector(initializer_list<bool> __l) : parent(__l) {}

    template<typename _InputIterator, typename = std::_RequireInputIter<_InputIterator>>
    Vector(_InputIterator __first, _InputIterator __last) : parent(__first, __last) {}

    parent::const_reference operator[](size_t ind) const {
#ifdef LOCAL
        if (ind >= parent::size()) {
            throw "Out of bounds";
        }
#endif
        return *const_iterator(this->_M_impl._M_start._M_p
                               + ind / int(_S_word_bit), ind % int(_S_word_bit));
    }

    parent::reference operator[](size_t ind) {
#ifdef LOCAL
        if (ind >= parent::size()) {
            throw "Out of bounds";
        }
#endif
        return *iterator(this->_M_impl._M_start._M_p
                         + ind / int(_S_word_bit), ind % int(_S_word_bit));
    }

    Vector<bool> &operator=(Vector<bool> &&__x) noexcept {
        parent::operator=(__x);
        return *this;
    }

    Vector<bool> &operator=(const Vector<bool> &__x) {
        parent::operator=(__x);
        return *this;
    }
};

#ifdef LOCAL
#define vec Vector
#else
#define vec vector
#endif

using vi = vec<int>;

#define all(v) (v).begin(), (v).end()

using ll = long long;
using pii = pair<int, int>;

const double PI = atan(1) * 4;

template<typename T>
T minim(T &was, T cand) {
    return was = min(was, cand);
}

template<typename T>
T maxim(T &was, T cand) {
    return was = max(was, cand);
}

template<typename D>
D dPower(D base, ll exponent) {
    if (exponent < 0) {
        return dPower(1 / base, -exponent);
    }
    if (exponent == 0) {
        return 1;
    }
    if ((exponent & 1) == 1) {
        return dPower(base, exponent - 1) * base;
    } else {
        D res = dPower(base, exponent >> 1);
        return res * res;
    }
}

template<typename T>
class arr {
    T *b;
    T *e;
    int n;
public:
    arr() : b(nullptr), e(nullptr), n(0) {}

    arr(int n) : n(n) {
#ifdef LOCAL
        if (n < 0) {
            throw "bad alloc";
        }
#endif
        if (n > 0) {
            b = new T[n];
            e = b + n;
        } else {
            b = e = nullptr;
        }
    }

    arr(int n, const T &init) : n(n) {
#ifdef LOCAL
        if (n < 0) {
            throw "bad alloc";
        }
#endif
        if (n > 0) {
            b = new T[n];
            e = b + n;
            fill(b, e, init);
        } else {
            b = e = nullptr;
        }
    }

    arr(initializer_list<T> l) : n(l.size()) {
        if (n == 0) {
            b = e = nullptr;
        } else {
            b = new T[l.size()];
            e = b + n;
            copy(all(l), b);
        }
    }

    arr(T *b, int n) : b(b), e(b + n), n(n) {}

    arr(T *b, T *e) : b(b), e(e), n(e - b) {}

    size_t size() const {
        return n;
    }

    T *begin() {
        return b;
    }

    const T *begin() const {
        return b;
    }

    T *end() {
        return e;
    }

    const T *end() const {
        return e;
    }

    arr<T> clone() const {
        arr<T> res(n);
        copy(b, e, res.begin());
        return res;
    }

    T &operator[](int at) {
#ifdef LOCAL
        if (at < 0 || at >= n) {
            throw "Out of bounds";
        }
#endif
        return b[at];
    }

    const T &operator[](int at) const {
#ifdef LOCAL
        if (at < 0 || at >= n) {
            throw "Out of bounds";
        }
#endif
        return b[at];
    }

    void swap(arr<T> &arr) {
        std::swap(b, arr.b);
        std::swap(e, arr.e);
        std::swap(n, arr.n);
    }
};

class BoolIterator : iterator<forward_iterator_tag, bool> {
    unsigned *at;
    unsigned pos;

public:
    BoolIterator(unsigned int *at, unsigned int pos) : at(at), pos(pos) {}

    bool operator*() {
        return (*at >> pos & 1) == 1;
    }

    BoolIterator &operator++() {
        pos++;
        if (pos == 32) {
            pos = 0;
            at++;
        }
        return *this;
    }
};

typedef arr<int> arri;

void decreaseByOne() {}

template<typename T, class...Vs>
void decreaseByOne(arr<T> &array, Vs &...vs) {
    int n = array.size();
    for (int i = 0; i < n; ++i) {
        array[i]--;
    }
    decreaseByOne(vs...);
}

template<typename T, typename U>
void decreaseByOne(arr<pair<T, U> > &v) {
    for (auto &p : v) {
        p.first--;
        p.second--;
    }
}

template<typename T>
class arr2d {
    T *b;
    T *e;
    int d1;
    int d2;
    int sz;

public:
    arr2d() : b(nullptr), e(nullptr), d1(0), d2(0), sz(0) {}

    arr2d(int d1, int d2) : d1(d1), d2(d2), sz(d1 * d2) {
        b = new T[sz];
        e = b + sz;
    }

    arr2d(int d1, int d2, const T &init) : d1(d1), d2(d2), sz(d1 * d2) {
        b = new T[sz];
        e = b + sz;
        fill(b, e, init);
    }

    arr2d(T *b, int d1, int d2) : b(b), e(b + d1 * d2), d1(d1), d2(d2), sz(d1 * d2) {}

    size_t size() const {
        return sz;
    }

    size_t dim1() const {
        return d1;
    }

    size_t dim2() const {
        return d2;
    }

    T *begin() {
        return b;
    }

    T *end() {
        return e;
    }

    T &operator()(int i1, int i2) {
#ifdef LOCAL
        if (i1 < 0 || i1 >= d1 || i2 < 0 || i2 >= d2) {
            throw "Out of bounds";
        }
#endif
        return b[i1 * d2 + i2];
    }

    const T &operator()(int i1, int i2) const {
#ifdef LOCAL
        if (i1 < 0 || i1 >= d1 || i2 < 0 || i2 >= d2) {
            throw "Out of bounds";
        }
#endif
        return b[i1 * d2 + i2];
    }

    arr<T> operator[](int at) {
#ifdef LOCAL
        if (at < 0 || at >= d1) {
            throw "Out of bounds";
        }
#endif
        return arr<T>(b + at * d2, d2);
    }

    void swap(arr2d<T> &a) {
        std::swap(b, a.b);
        std::swap(e, a.e);
        std::swap(d1, a.d1);
        std::swap(d2, a.d2);
        std::swap(sz, a.sz);
    }
};

template<typename T>
class arr3d {
    T *b;
    T *e;
    int d1;
    int d2;
    int d3;
    int shift;
    int sz;

public:
    arr3d() : b(nullptr), e(nullptr), d1(0), d2(0), d3(0), shift(0), sz(0) {}

    arr3d(int d1, int d2, int d3) : d1(d1), d2(d2), d3(d3), shift(d2 * d3), sz(d1 * d2 * d3) {
        b = new T[sz];
        e = b + sz;
    }

    arr3d(int d1, int d2, int d3, const T &init) : d1(d1), d2(d2), d3(d3), shift(d2 * d3), sz(d1 * d2 * d3) {
        b = new T[sz];
        e = b + sz;
        fill(b, e, init);
    }

    arr3d(T *b, int d1, int d2, int d3) : b(b), e(b + d1 * d2 * d3), d1(d1), d2(d2), d3(d3), shift(d2 * d3),
                                          sz(d1 * d2 * d3) {}

    size_t size() const {
        return sz;
    }

    size_t dim1() const {
        return d1;
    }

    size_t dim2() const {
        return d2;
    }

    size_t dim3() const {
        return d3;
    }

    T *begin() {
        return b;
    }

    T *end() {
        return e;
    }

    T &operator()(int i1, int i2, int i3) {
#ifdef LOCAL
        if (i1 < 0 || i1 >= d1 || i2 < 0 || i2 >= d2 || i3 < 0 || i3 >= d3) {
            throw "Out of bounds";
        }
#endif
        return b[i1 * shift + i2 * d3 + i3];
    }

    const T &operator()(int i1, int i2, int i3) const {
#ifdef LOCAL
        if (i1 < 0 || i1 >= d1 || i2 < 0 || i2 >= d2 || i3 < 0 || i3 >= d3) {
            throw "Out of bounds";
        }
#endif
        return b[i1 * shift + i2 * d3 + i3];
    }

    arr2d<T> operator[](int at) {
#ifdef LOCAL
        if (at < 0 || at >= d1) {
            throw "Out of bounds";
        }
#endif
        return arr2d<T>(b + at * shift, d2, d3);
    }
};

template<typename T>
class arr4d {
    T *b;
    T *e;
    int d1;
    int d2;
    int d3;
    int d4;
    int shift1;
    int shift2;
    int sz;

public:
    arr4d() : b(nullptr), e(nullptr), d1(0), d2(0), d3(0), d4(0), shift1(0), shift2(0), sz(0) {}

    arr4d(int d1, int d2, int d3, int d4) : d1(d1), d2(d2), d3(d3), d4(d4), shift1(d2 * d3 * d4), shift2(d3 * d4),
                                            sz(d1 * d2 * d3 * d4) {
        b = new T[sz];
        e = b + sz;
    }

    arr4d(int d1, int d2, int d3, int d4, const T &init) : d1(d1), d2(d2), d3(d3), d4(d4), shift1(d2 * d3 * d4),
                                                           shift2(d3 * d4), sz(d1 * d2 * d3 * d4) {
        b = new T[sz];
        e = b + sz;
        fill(b, e, init);
    }

    arr4d(T *b, int d1, int d2, int d3, int d4) : d1(d1), d2(d2), d3(d3), d4(d4), shift1(d2 * d3 * d4), shift2(d3 * d4),
                                                  sz(d1 * d2 * d3 * d4) {}

    size_t size() const {
        return sz;
    }

    size_t dim1() const {
        return d1;
    }

    size_t dim2() const {
        return d2;
    }

    size_t dim3() const {
        return d3;
    }

    size_t dim4() const {
        return d4;
    }

    T *begin() {
        return b;
    }

    T *end() {
        return e;
    }

    T &operator()(int i1, int i2, int i3, int i4) {
#ifdef LOCAL
        if (i1 < 0 || i1 >= d1 || i2 < 0 || i2 >= d2 || i3 < 0 || i3 >= d3 || i4 < 0 || i4 >= d4) {
            throw "Out of bounds";
        }
#endif
        return b[i1 * shift1 + i2 * shift2 + i3 * d4 + i4];
    }

    const T &operator()(int i1, int i2, int i3, int i4) const {
#ifdef LOCAL
        if (i1 < 0 || i1 >= d1 || i2 < 0 || i2 >= d2 || i3 < 0 || i3 >= d3 || i4 < 0 || i4 >= d4) {
            throw "Out of bounds";
        }
#endif
        return b[i1 * shift1 + i2 * shift2 + i3 * d4 + i4];
    }

    arr2d<T> operator[](int at) {
#ifdef LOCAL
        if (at < 0 || at >= d1) {
            throw "Out of bounds";
        }
#endif
        return arr3d<T>(b + at * shift1, d2, d3, d4);
    }
};

inline bool isWhitespace(int c) {
    return isspace(c) || c == EOF;
}

class Input {
private:
    istream ∈
    bool exhausted = false;

    inline int get() {
        if (exhausted) {
#ifdef LOCAL
            throw "Input exhausted";
#endif
            return EOF;
        }
        int c = in.get();
        if (c == EOF) {
            exhausted = true;
        }
        return c;
    }

    template<typename T>
    inline T readInteger() {
        skipWhitespace();
        int c = get();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = get();
        }
        T res = 0;
        do {
            if (!isdigit(c)) {
#ifdef LOCAL
                throw "Number format error";
#endif
                return sgn * res;
            }
            res *= 10;
            res += c - '0';
            c = get();
        } while (!isWhitespace(c));
        return res * sgn;
    }

    void initArrays(int) {}

    template<typename T, class...Vs>
    void initArrays(int n, arr<T> &array, Vs &...vs) {
        array = arr<T>(n);
        initArrays(n, vs...);
    }

    void readImpl(int) {}

    template<typename T, class...Vs>
    void readImpl(int i, arr<T> &arr, Vs &...vs) {
        arr[i] = readType<T>();
        readImpl(i, vs...);
    }

public:
    Input(istream &in) : in(in) {};

    inline void skipWhitespace() {
        int c;
        while (isWhitespace(c = in.peek()) && c != EOF) {
            in.get();
        }
        if (c == EOF) {
            exhausted = true;
        }
    }

    inline int readInt() {
        return readInteger<int>();
    }

    ll readLong() {
        return readInteger<ll>();
    }

    string readString() {
        skipWhitespace();
        int c = get();
        if (c == EOF) {
#ifdef LOCAL
            throw "Input exhausted";
#endif
            return "";
        }
        vec<char> res;
        do {
            res.push_back(c);
        } while (!isWhitespace(c = get()));
        return string(all(res));
    }

    arri readIntArray(int size) {
        return readArray<int>(size);
    }

    template<typename T>
    T readType() {
        throw "Operation not supported";
    }

    template<typename U, typename V>
    pair<U, V> readType() {
        U first = readType<U>();
        V second = readType<V>();
        return make_pair(first, second);
    }

    template<typename T>
    arr<T> readArray(int n) {
        arr<T> res(n);
        for (int i = 0; i < n; i++) {
            res[i] = readType<T>();
        }
        return res;
    }

    template<class...Vs>
    void readArrays(int n, Vs &...vs) {
        initArrays(n, vs...);
        for (int i = 0; i < n; i++) {
            readImpl(i, vs...);
        }
    }

    template<typename U, typename V>
    arr<pair<U, V> > readArray(int n) {
        arr<pair<U, V> > res(n);
        for (int i = 0; i < n; i++) {
            res[i] = readType<U, V>();
        }
        return res;
    }

    template<typename T>
    arr2d<T> readTable(int rows, int cols) {
        arr2d<T> result(rows, cols);
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                result(i, j) = readType<T>();
            }
        }
        return result;
    }

    string readLine() {
        int c = get();
        if (c == EOF) {
#ifdef LOCAL
            throw "Input exhausted";
#endif
            return "";
        }
        vec<char> res;
        do {
            res.push_back(c);
            c = get();
        } while (c != '\n' && c != '\r' && c != EOF);
        return string(all(res));
    }

    double readDouble() {
        skipWhitespace();
        int c = get();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = get();
        }
        double res = 0;
        do {
            if (tolower(c) == 'e') {
                return sgn * res * dPower(10, readInt());
            }
            if (!isdigit(c)) {
#ifdef LOCAL
                throw "Number format error";
#endif
                return sgn * res;
            }
            res *= 10;
            res += c - '0';
            c = get();
        } while (!isWhitespace(c) && c != '.');
        if (c == '.') {
            double add = 0;
            int length = 0;
            c = get();
            do {
                if (tolower(c) == 'e') {
                    return sgn * (res + add * dPower(10, -length)) * dPower(10, readInt());
                }
                if (!isdigit(c)) {
#ifdef LOCAL
                    throw "Number format error";
#endif
                    res += add * dPower(10, -length);
                    return res * sgn;
                }
                add *= 10;
                add += c - '0';
                length++;
                c = get();
            } while (!isWhitespace(c));
            res += add * dPower(10, -length);
        }
        return res * sgn;
    }

    char readChar() {
        skipWhitespace();
        int c = get();
        if (c == EOF) {
#ifdef LOCAL
            throw "Input exhausted";
#endif
            return 0;
        }
        return c;
    }

    bool isExhausted() { return exhausted; }
};

template<>
double Input::readType() {
    return readDouble();
}

template<>
int Input::readType() {
    return readInt();
}

template<>
ll Input::readType() {
    return readLong();
}

template<>
char Input::readType() {
    return readChar();
}

template<>
string Input::readType() {
    return readString();
}

class Output {
private:
    ostream &out;

    template<typename T>
    void printSingle(const T &value) {
        out << value;
    }

    template<typename T>
    void printSingle(const vec<T> &array) {
        size_t n = array.size();
        for (int i = 0; i < n; ++i) {
            out << array[i];
            if (i + 1 != n) {
                out << ' ';
            }
        }
    }

    template<typename T>
    void printSingle(const arr<T> &array) {
        size_t n = array.size();
        for (int i = 0; i < n; ++i) {
            out << array[i];
            if (i + 1 != n) {
                out << ' ';
            }
        }
    }

    template<typename T>
    void printSingle(const arr2d<T> &array) {
        size_t n = array.dim1();
        size_t m = array.dim2();
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                out << array(i, j);
                if (j + 1 != m) {
                    out << ' ';
                }
            }
            if (i + 1 != n) {
                out << '\n';
            }
        }
    }

    template<typename T, typename U>
    void printSingle(const pair<T, U> &value) {
        out << value.first << ' ' << value.second;
    }

public:
    Output(ostream &out) : out(out) {
        out << fixed << setprecision(20);
    }

    void print() {}

    template<typename T, typename...Targs>
    void print(const T &first, const Targs... args) {
        printSingle(first);
        if (sizeof...(args) != 0) {
            out << ' ';
            print(args...);
        }
    }

    template<typename...Targs>
    void printLine(const Targs... args) {
        print(args...);
        out << '\n';
    }

    void flush() {
        out.flush();
    }
};

class NumberIterator : iterator<forward_iterator_tag, int> {
public:
    int v;

    NumberIterator(int v) : v(v) {}

    operator int &() { return v; }

    int operator*() { return v; }
};

class range : pii {
public:
    range(int begin, int end) : pii(begin, max(begin, end)) {}

    range(int n) : pii(0, max(0, n)) {}

    NumberIterator begin() {
        return first;
    }

    NumberIterator end() {
        return second;
    }
};

template<typename T>
inline void unique(vec<T> &v) {
    v.resize(unique(all(v)) - v.begin());
}

arri createOrder(int n) {
    arri order(n);
    for (int i = 0; i < n; i++) {
        order[i] = i;
    }
    return order;
}

template<class Collection, typename Iterator>
inline void addAll(Collection &v, Iterator begin, Iterator end) {
    v.insert(v.end(), begin, end);
}

template<typename Iterator>
arri getQty(Iterator begin, Iterator end, int length) {
    arri res(length, 0);
    for (Iterator it = begin; it != end; it++) {
        res[*it]++;
    }
    return res;
}

template<typename Iterator>
arri getQty(Iterator begin, Iterator end) {
    return getQty(begin, end, *max_element(begin, end) + 1);
}

template<class Collection>
void collect(Collection &) {}

template<class Collection, class ...Vs>
void collect(Collection &all, Collection &a, Vs &...vs) {
    addAll(all, all(a));
    collect(all, vs...);
}

void replace(const vi &) {}

template<class ...Vs>
void replace(const vi &all, vi &a, Vs &...vs) {
    for (int &i : a) {
        i = lower_bound(all(all), i) - all.begin();
    }
    replace(all, vs...);
}

template<class ...Vs>
void replace(const vi &all, arri &a, Vs &...vs) {
    for (int &i : a) {
        i = lower_bound(all(all), i) - all.begin();
    }
    replace(all, vs...);
}

template<class ...Vs>
vi compress(Vs &...vs) {
    vi vals;
    collect(vals, vs...);
    sort(all(vals));
    unique(vals);
    replace(vals, vs...);
    return vals;
}

class DSU {
    mutable arri id;
    arri sz;
    int count;

public:
    DSU(int n) {
        id = createOrder(n);
        sz = arri(n, 1);
        count = n;
    }

    int get(int i) const {
        if (id[i] == i) {
            return i;
        }
        return id[i] = get(id[i]);
    }

    int operator[](int i) const { return get(i); }

    int size(int i) const { return sz[get(i)]; }

    int setCount() const { return count; }

    bool join(int a, int b) {
        a = get(a);
        b = get(b);
        if (a == b) {
            return false;
        }
        sz[a] += sz[b];
        sz[b] = 0;
        id[b] = a;
        count--;
        return true;
    }
};

//#pragma comment(linker, "/STACK:200000000")

arr<ll> q(51, 0);
arr<ll> step(51, 1);
bool first = true;

class TaskE {
public:
    void solve(istream &inp, ostream &outp) {
        Input in(inp);
        Output out(outp);

        int n = in.readInt();
        ll k = in.readLong() - 1;

        if (first) {
            q[0] = 1;
            ll infty = numeric_limits<ll>::max();
            for (int i : range(1, 51)) {
                for (int j : range(1, i)) {
                    if (infty / step[i] < j) {
                        step[i] = infty;
                        break;
                    }
                    step[i] *= j;
                }
                for (int j : range(1, i + 1)) {
                    if ((infty - q[i]) / step[j - 1] < q[i - j]) {
                        q[i] = infty;
                        break;
                    }
                    q[i] += q[i - j] * step[j - 1];
                }
            }
            first = false;
        }

        if (k >= q[n]) {
            out.printLine(-1);
            return;
        }
        vi answer;
        int rem = n;
        int add = 0;
        while (rem > 0) {
            int len;
            for (int i : range(1, rem + 1)) {
                if (k / step[i - 1] < q[rem - i]) {
                    len = i;
                    break;
                } else {
                    k -= step[i - 1] * q[rem - i];
                }
            }
            answer.push_back(len + add);
            arr<bool> iedge(len, false);
            iedge[len - 1] = true;
            DSU dsu(len);
            dsu.join(0, len - 1);
            for (int i : range(1, len)) {
                ll cStep = step[len - i - 1];
                for (int j : range(0, len)) {
                    if (iedge[j] || (dsu[j] == dsu[i] && i + 1 != len)) {
                        continue;
                    }
                    if (k / cStep >= q[rem - len]) {
                        k -= cStep * q[rem - len];
                    } else {
                        answer.push_back(add + j + 1);
                        iedge[j] = true;
                        dsu.join(i, j);
                        break;
                    }
                }
            }
            add += len;
            rem -= len;
        }
        out.printLine(answer);
    }
};

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    TaskE solver;
    std::istream &in(std::cin);
    std::ostream &out(std::cout);
    int n;
    in >> n;
    for (int i = 0; i < n; ++i) {
        solver.solve(in, out);
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const ll XX = 1000000000000000000;

ll N, K;
ll D[55];
ll E[55];
int ans[55];
vector<int> perm;
int p[55];
bool chk[55];

int par(int x) {
	if(x == p[x]) return x;
	return p[x] = par(p[x]);
}

void unite(int a, int b) {
	a = p[a]; b = p[b];
	if(a == b) return;
	p[a] = b;
}

void f(int n, ll k) {
	perm.clear(); perm.push_back(n);
	k--;

	for(int i = 1; i <= n; i++) { p[i] = i; chk[i] = false; }
	unite(1, n); chk[n] = true;

	for(int i = 1; i < n; i++) {
		for(int j = 1; j < n; j++) {
			if(chk[j] || par(perm.size() + 1) == par(j)) continue;
			if(k >= D[n - i]) k -= D[n - i];
			else {
				perm.push_back(j); chk[j] = true;
				break;
			}
		}
	}
	for(int i = 1; i <= n; i++) if(!chk[i]) perm.push_back(i);
}

ll mul(ll a, ll b) {
	ll ret = 0;
	while(b) {
		if(b & 1) ret = ret + a;
		a = a + a;
		b >>= 1;
		if(ret > XX || a > XX) return XX + 1;
	}
	return ret;
}

int main() {
	D[0] = D[1] = D[2] = 1;
	for(int i = 3; i <= 22; i++) D[i] = D[i - 1] * (i - 2);
	for(int i = 23; i <= 50; i++) D[i] = D[i - 1];
	E[0] = 1;
	for(int i = 1; i <= 21; i++) {
		for(int j = 1; j <= i; j++) {
			E[i] += D[j] * E[i - j];
			// printf("%lld %lld\n", E[i], mul(D[j], E[i - j]));
		}
	}
	for(int i = 22; i <= 50; i++) E[i] = XX + 1;
	// printf("%lld %lld\n", D[22], E[21]);
	// printf("%lld %lld\n", D[21], E[21]);

	// f(5, 2);
	// for(int i : perm) printf("%d ", i); puts("");
	// for(int j = 2; j <= 21; j++) printf("%lld ", mul(D[j], E[22 - j]));

	int T; scanf("%d", &T);
	while(T--) {
		scanf("%lld%lld", &N, &K); K--;
		int i = 1;
		while(1) {
			if(E[N - i] > K) {
				ans[i] = i; i++; continue;
			}
			else break;
		}

		bool ok = true;

		while(i <= N) {
			int j;
			for(j = 1; j <= N - i + 1; j++) {
				if(E[N - i + 1 - j] <= K && mul(D[j], E[N - i + 1 - j]) <= K) {
					K -= D[j] * E[N - i + 1 - j];
				}
				else break;
			}
			if(j > N - i + 1) { puts("-1"); ok = false; break; }

			// printf("%d %d %lld\n", i, j, K);

			ll t = K / E[N - i + 1 - j];
			K -= t * E[N - i + 1 - j];

			f(j, t + 1);
			for(int k = 0; k < j; k++) ans[i + k] = perm[k] + i - 1;
			i += j;
		}

		if(!ok) continue;

		if(K > 0) puts("-1");
		else {
			for(int i = 1; i <= N; i++) printf("%d ", ans[i]); puts("");
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

ll N, K;
ll D[55];
ll E[55];
int ans[55];
vector<int> perm;
int p[55];
bool chk[55];

int par(int x) {
	if(x == p[x]) return x;
	return p[x] = par(p[x]);
}

void unite(int a, int b) {
	a = p[a]; b = p[b];
	if(a == b) return;
	p[a] = b;
}

void f(int n, ll k) {
	perm.clear(); perm.push_back(n);
	k--;

	for(int i = 1; i <= n; i++) { p[i] = i; chk[i] = false; }
	unite(1, n); chk[n] = true;

	for(int i = 1; i < n; i++) {
		for(int j = 1; j < n; j++) {
			if(chk[j] || par(perm.size() + 1) == par(j)) continue;
			if(k >= D[n - i]) k -= D[n - i];
			else {
				perm.push_back(j); chk[j] = true;
				break;
			}
		}
	}
	for(int i = 1; i <= n; i++) if(!chk[i]) perm.push_back(i);
}

int main() {
	D[0] = D[1] = D[2] = 1;
	for(int i = 3; i <= 22; i++) D[i] = D[i - 1] * (i - 2);
	for(int i = 23; i <= 50; i++) D[i] = D[i - 1];
	E[0] = 1;
	for(int i = 1; i <= 22; i++) {
		for(int j = 1; j <= i; j++) E[i] += D[j] * E[i - j];
	}
	for(int i = 23; i <= 50; i++) E[i] = E[i - 1];

	// f(5, 2);
	// for(int i : perm) printf("%d ", i); puts("");

	int T; scanf("%d", &T);
	while(T--) {
		scanf("%lld%lld", &N, &K); K--;
		int i = 1;
		while(1) {
			if(E[N - i] > K) {
				ans[i] = i; i++; continue;
			}
			else break;
		}

		bool ok = true;

		while(i <= N) {
			int j;
			for(j = 1; j <= N - i + 1; j++) {
				if(D[j] * E[N - i + 1 - j] <= K) {
					K -= D[j] * E[N - i + 1 - j];
				}
				else break;
			}
			if(j > N - i + 1) { puts("-1"); ok = false; break; }

			ll t = K / E[N - i + 1 - j];
			K -= t * E[N - i + 1 - j];

			f(j, t);
			for(int k = 0; k < j; k++) ans[i + k] = perm[k] + i - 1;
			i += j;
		}

		if(!ok) continue;

		if(K > 0) puts("-1");
		else {
			for(int i = 1; i <= N; i++) printf("%d ", ans[i]); puts("");
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const ll XX = 1000000000000000000;

ll N, K;
ll D[55];
ll E[55];
int ans[55];
vector<int> perm;
int p[55];
bool chk[55];

int par(int x) {
	if(x == p[x]) return x;
	return p[x] = par(p[x]);
}

void unite(int a, int b) {
	a = p[a]; b = p[b];
	if(a == b) return;
	p[a] = b;
}

void f(int n, ll k) {
	perm.clear(); perm.push_back(n);
	k--;

	for(int i = 1; i <= n; i++) { p[i] = i; chk[i] = false; }
	unite(1, n); chk[n] = true;

	for(int i = 1; i < n; i++) {
		for(int j = 1; j < n; j++) {
			if(chk[j] || par(perm.size() + 1) == par(j)) continue;
			if(k >= D[n - i]) k -= D[n - i];
			else {
				unite(perm.size() + 1, j);
				perm.push_back(j); chk[j] = true;
				break;
			}
		}
	}
	for(int i = 1; i <= n; i++) if(!chk[i]) perm.push_back(i);
}

ll mul(ll a, ll b) {
	ll ret = 0;
	while(b) {
		if(b & 1) ret = ret + a;
		a = a + a;
		b >>= 1;
		if(ret > XX || a > XX) return XX + 1;
	}
	return ret;
}

int main() {
	D[0] = D[1] = D[2] = 1;
	for(int i = 3; i <= 22; i++) D[i] = D[i - 1] * (i - 2);
	for(int i = 23; i <= 50; i++) D[i] = D[i - 1];
	E[0] = 1;
	for(int i = 1; i <= 21; i++) {
		for(int j = 1; j <= i; j++) {
			E[i] += D[j] * E[i - j];
			// printf("%lld %lld\n", E[i], mul(D[j], E[i - j]));
		}
	}
	for(int i = 22; i <= 50; i++) E[i] = XX + 1;
	// printf("%lld %lld\n", D[22], E[21]);
	// printf("%lld %lld\n", D[21], E[21]);

	// f(5, 2);
	// for(int i : perm) printf("%d ", i); puts("");
	// for(int j = 2; j <= 21; j++) printf("%lld ", mul(D[j], E[22 - j]));

	int T; scanf("%d", &T);
	while(T--) {
		scanf("%lld%lld", &N, &K); K--;
		int i = 1;
		while(1) {
			if(E[N - i] > K) {
				ans[i] = i; i++; continue;
			}
			else break;
		}

		bool ok = true;

		while(i <= N) {
			int j;
			for(j = 1; j <= N - i + 1; j++) {
				if(E[N - i + 1 - j] <= K && mul(D[j], E[N - i + 1 - j]) <= K) {
					K -= D[j] * E[N - i + 1 - j];
				}
				else break;
			}
			if(j > N - i + 1) { puts("-1"); ok = false; break; }

			// printf("%d %d %lld\n", i, j, K);

			ll t = K / E[N - i + 1 - j];
			K -= t * E[N - i + 1 - j];

			f(j, t + 1);
			for(int k = 0; k < j; k++) ans[i + k] = perm[k] + i - 1;
			i += j;
		}

		if(!ok) continue;

		if(K > 0) puts("-1");
		else {
			for(int i = 1; i <= N; i++) printf("%d ", ans[i]); puts("");
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

ll N, K;
ll D[55];
ll E[55];
int ans[55];
vector<int> perm;
int p[55];
bool chk[55];

int par(int x) {
	if(x == p[x]) return x;
	return p[x] = par(p[x]);
}

void unite(int a, int b) {
	a = p[a]; b = p[b];
	if(a == b) return;
	p[a] = b;
}

void f(int n, ll k) {
	perm.clear(); perm.push_back(n);
	k--;

	for(int i = 1; i <= n; i++) { p[i] = i; chk[i] = false; }
	unite(1, n); chk[n] = true;

	for(int i = 1; i < n; i++) {
		for(int j = 1; j < n; j++) {
			if(chk[j] || par(perm.size() + 1) == par(j)) continue;
			if(k >= D[n - i]) k -= D[n - i];
			else {
				perm.push_back(j); chk[j] = true;
				break;
			}
		}
	}
	for(int i = 1; i <= n; i++) if(!chk[i]) perm.push_back(i);
}

int main() {
	D[0] = D[1] = D[2] = 1;
	for(int i = 3; i <= 22; i++) D[i] = D[i - 1] * (i - 2);
	for(int i = 23; i <= 50; i++) D[i] = D[i - 1];
	E[0] = 1;
	for(int i = 1; i <= 22; i++) {
		for(int j = 1; j <= i; j++) E[i] += D[j] * E[i - j];
	}
	for(int i = 23; i <= 50; i++) E[i] = E[i - 1];

	// f(5, 2);
	// for(int i : perm) printf("%d ", i); puts("");

	int T; scanf("%d", &T);
	while(T--) {
		scanf("%lld%lld", &N, &K); K--;
		int i = 1;
		while(1) {
			if(E[N - i] > K) {
				ans[i] = i; i++; continue;
			}
			else break;
		}

		bool ok = true;

		while(i <= N) {
			int j;
			for(j = 1; j <= N - i + 1; j++) {
				if(E[N - i + 1 - j] <= K && D[j] * E[N - i + 1 - j] <= K) {
					K -= D[j] * E[N - i + 1 - j];
				}
				else break;
			}
			if(j > N - i + 1) { puts("-1"); ok = false; break; }

			// printf("%d %d %lld\n", i, j, K);

			ll t = K / E[N - i + 1 - j];
			K -= t * E[N - i + 1 - j];

			f(j, t + 1);
			for(int k = 0; k < j; k++) ans[i + k] = perm[k] + i - 1;
			i += j;
		}

		if(!ok) continue;

		if(K > 0) puts("-1");
		else {
			for(int i = 1; i <= N; i++) printf("%d ", ans[i]); puts("");
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using ll = long long;
using pii = std::pair <int, int>;
const int N = 60;
const ll INF = ll(1.5e18);

ll mult(ll a, ll b){
    if (!a || !b) return 0;
    return a <= INF / b ? a * b : INF;
}

ll add(ll a, ll b){
    return std::min(INF, a + b);
}

ll dp[N], fac[N], op[N];
std::vector <int> ans;

std::vector <int> get(int n, ll k){
    std::vector <bool> vis(n);
    std::vector <int> ret(n, -1);
    std::vector <int> begin(n, -1), end(n, -1);
    vis[n - 1] = true;
    ret[0] = n - 1;
    begin[n - 1] = 0;
    end[0] = n - 1;
    int chain_number = n - 1;
    for (int i = 1; i < n; ++ i){
        for (int j = 0; j < n; ++ j){
            if (vis[j]){
                continue;
            }
            std::vector <std::pair <int, int>> roll_back_begin, roll_back_end;
            int pre_chain_number = chain_number;
            ret[i] = j;
            if (i == n - 1){
                return ret;
            }
            if (i == j || end[j] == i){
                continue;
            }
            -- chain_number;
            if (begin[i] != -1 && end[j] != -1){
                int x = begin[i], y = end[j];
                roll_back_end.emplace_back(x, end[x]);
                roll_back_begin.emplace_back(y, begin[y]);
                end[x] = y;
                begin[y] = x;
            }
            else if (begin[i] != -1){
                roll_back_end.emplace_back(i, end[i]);
                end[begin[i]] = j;
            }
            else if (end[j] != -1){
                roll_back_begin.emplace_back(j, begin[j]);
                begin[end[j]] = i;
            }
            else{
                roll_back_end.emplace_back(i, end[i]);
                roll_back_begin.emplace_back(j, begin[j]);
                begin[j] = i;
                end[i] = j;
            }
            ll value = fac[chain_number - 1];
            if (k > value){
                k -= value;
                for (auto u : roll_back_begin){
                    begin[u.first] = u.second;
                }
                for (auto u : roll_back_end){
                    end[u.first] = u.second;
                }
                chain_number = pre_chain_number;
                continue;
            }
            else{
                ret[i] = j;
                vis[j] = true;
                break;
            }
        }
    }
}

void solve(int n, ll k, int offset){
    if (n == 0){
        return;
    }
    bool flag = true;
    for (int i = 1; i <= n; ++ i){
        ll value = mult(op[i], dp[n - i]);
        if (k > value){
            k -= value;
            continue;
        }
        flag = false;
        ll num = (k - 1) / dp[n - i] + 1;
        std::vector <int> vec = get(i, num);
        for (auto u : vec){
            ans.push_back(u + offset);
        }
        solve(n - i, k - (num - 1) * dp[n - i], offset + i);
    }
    if (flag){
        ans.push_back(-1);
        return;
    }
}

void solve(int n, ll k){
    if (k > dp[n]){
        puts("-1");
        return;
    }
    solve(n, k, 1);
    if (ans.back() == -1){
        puts("-1");
    }
    else{
        for (int i = 0; i < n; ++ i){
            printf("%d%c", ans[i], " \n"[i == n - 1]);
        }
    }
    ans.clear();
}

int main(){
    fac[0] = 1;
    dp[0] = 1;
    for (int i = 1; i < N; ++ i){
        fac[i] = mult(fac[i - 1], i);
        op[i] = i <= 2 ? 1 : fac[i - 2];
    }
    for (int i = 1; i < N; ++ i){
        for (int j = 1; j <= i; ++ j){
            dp[i] = add(dp[i], mult(op[j], dp[i - j]));
        }
    }
    int test;
    scanf("%d", &test);
    while (test --){
        int n;
        ll k;
        scanf("%d%lld", &n, &k);
        solve(n, k);
    }
    return 0;
}


#include <bits/stdc++.h>

using ll = long long;
const int N = 60;
const ll INF = ll(1.5e18);

ll mult(ll a, ll b){
    if (!a || !b) return 0;
    return a <= INF / b ? a * b : INF;
}

ll add(ll a, ll b){
    return std::min(INF, a + b);
}

ll dp[N], fac[N], op[N];
std::vector <int> ans;

std::vector <int> get(int n, ll k){
    std::vector <bool> vis(n);
    std::vector <int> ret(n, -1);
    vis[n - 1] = true;
    ret[0] = n - 1;
    for (int i = 1; i < n; ++ i){
        for (int j = 0; j < n; ++ j){
            if (vis[j]){
                continue;
            }
            ret[i] = j;
            std::vector <bool> vis1(n), head(n, true);
            for (int kk = 0; kk < n; ++ kk){
                if (ret[kk] != -1){
                    head[ret[kk]] = false;
                }
            }
            int sum = 0;
            for (int kk = 0; kk < n; ++ kk){
                if (ret[kk] == -1){
                    break;
                }
                if (!head[kk]){
                    continue;
                }
                int x = kk, sz = 0;
                bool complete = false;
                while (true){
                    vis1[x] = true;
                    ++ sz;
                    x = ret[x];
                    if (x == -1 || vis1[x]){
                        if (x != -1){
                            complete = true;
                        }
                        break;
                    }
                }
                if (complete && sz < n){
                    sum = -1;
                    break;
                }
                sum += sz - 1;
            }
            ll value = sum == -1 ? 0 : op[n - sum];
            if (k > value){
                k -= value;
                continue;
            }
            else{
                ret[i] = j;
                vis[j] = true;
                break;
            }
        }
    }
    return ret;
}

void solve(int n, ll k, int offset = 1){
    if (n == 0){
        return;
    }
    bool flag = true;
    for (int i = 1; i <= n; ++ i){
        ll value = mult(op[i], dp[n - i]);
        if (k > value){
            k -= value;
            continue;
        }
        flag = false;
        ll num = (k - 1) / dp[n - i] + 1;
        std::vector <int> vec = get(i, num);
        for (auto u : vec){
            ans.push_back(u + offset);
        }
        solve(n - i, k - (num - 1) * dp[n - i], offset + i);
    }
    if (flag){
        ans.push_back(-1);
        return;
    }
}

void solve(){
    int n;
    ll k;
    scanf("%d%lld", &n, &k);
    if (k > dp[n]){
        puts("-1");
        return;
    }
    solve(n, k);
    if (ans.back() == -1){
        puts("-1");
    }
    else{
        for (int i = 0; i < n; ++ i){
            printf("%d%c", ans[i], " \n"[i == n - 1]);
        }
    }
    ans.clear();
}

int main(){
    fac[0] = 1;
    dp[0] = 1;
    for (int i = 1; i < N; ++ i){
        fac[i] = mult(fac[i - 1], i);
        op[i] = i <= 2 ? 1 : fac[i - 2];
    }
    for (int i = 1; i < N; ++ i){
        for (int j = 1; j <= i; ++ j){
            dp[i] = add(dp[i], mult(op[j], dp[i - j]));
        }
    }
    int test;
    scanf("%d", &test);
    while (test --){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>

using ll = long long;
const int N = 60;
const ll INF = ll(1.5e18);

ll mult(ll a, ll b){
    if (!a || !b) return 0;
    return a <= INF / b ? a * b : INF;
}

ll add(ll a, ll b){
    return std::min(INF, a + b);
}

ll dp[N], fac[N], op[N];
std::vector <int> ans;

std::vector <int> get(int n, ll k){
    std::vector <bool> vis(n);
    std::vector <int> ret(n, -1);
    vis[n - 1] = true;
    ret[0] = n - 1;
    for (int i = 1; i < n; ++ i){
        for (int j = 0; j < n; ++ j){
            if (vis[j]){
                continue;
            }
            ret[i] = j;
            std::vector <bool> vis1(n), head(n, true);
            for (int kk = 0; kk < n; ++ kk){
                if (ret[kk] != -1){
                    head[ret[kk]] = false;
                }
            }
            int sum = 0;
            for (int kk = 0; kk < n; ++ kk){
                if (ret[kk] == -1){
                    break;
                }
                if (!head[kk]){
                    continue;
                }
                int x = kk, sz = 0;
                bool complete = false;
                while (true){
                    vis1[x] = true;
                    ++ sz;
                    x = ret[x];
                    if (x == -1 || vis1[x]){
                        if (x != -1){
                            complete = true;
                        }
                        break;
                    }
                }
                if (complete && sz < n){
                    sum = -1;
                    break;
                }
            }
            for (int kk = 0; kk < n; ++ kk){
                if (!vis1[kk] && ret[kk] != -1){
                    int x = kk, sz = 0;
                    while (x != -1 && !vis1[x]){
                        ++ sz;
                        vis1[x] = true;
                        x = ret[x];
                    }
                    if (sz != n){
                        sum = -1;
                        break;
                    }
                }
            }
            if (sum == -1){
                continue;
            }
            for (int kk = 0; kk < n; ++ kk){
                if (!vis1[kk] && ret[kk] == -1){
                    ++ sum;
                }
                else if (head[kk]){
                    ++ sum;
                }
            }
            ll value = fac[sum - 1];
            if (k > value){
                k -= value;
                continue;
            }
            else{
                ret[i] = j;
                vis[j] = true;
                break;
            }
        }
    }
    return ret;
}

void solve(int n, ll k, int offset){
    if (n == 0){
        return;
    }
    bool flag = true;
    for (int i = 1; i <= n; ++ i){
        ll value = mult(op[i], dp[n - i]);
        if (k > value){
            k -= value;
            continue;
        }
        flag = false;
        ll num = (k - 1) / dp[n - i] + 1;
        std::vector <int> vec = get(i, num);
        for (auto u : vec){
            ans.push_back(u + offset);
        }
        solve(n - i, k - (num - 1) * dp[n - i], offset + i);
        break;
    }
    if (flag){
        ans.push_back(-1);
        return;
    }
}

void solve(int n, ll k){
    if (k > dp[n]){
        puts("-1");
        return;
    }
    solve(n, k, 1);
    if (ans.back() == -1){
        puts("-1");
    }
    else{
        for (int i = 0; i < n; ++ i){
            printf("%d%c", ans[i], " \n"[i == n - 1]);
        }
    }
    ans.clear();
}

int main(){
    fac[0] = 1;
    dp[0] = 1;
    for (int i = 1; i < N; ++ i){
        fac[i] = mult(fac[i - 1], i);
        op[i] = i <= 2 ? 1 : fac[i - 2];
    }
    for (int i = 1; i < N; ++ i){
        for (int j = 1; j <= i; ++ j){
            dp[i] = add(dp[i], mult(op[j], dp[i - j]));
        }
    }
    /*for (int i = 1; i <= dp[6] + 1; ++ i){
        solve(6, i);
    }*/
    int test;
    scanf("%d", &test);
    while (test --){
        int n;
        ll k;
        scanf("%d%lld", &n, &k);
        solve(n, k);
    }
    return 0;
}


#include <bits/stdc++.h>

using ll = long long;
const int N = 60;
const ll INF = ll(1.5e18);

ll mult(ll a, ll b){
    if (!a || !b) return 0;
    return a <= INF / b ? a * b : INF;
}

ll add(ll a, ll b){
    return std::min(INF, a + b);
}

ll dp[N], fac[N], op[N];
std::vector <int> ans;

std::vector <int> get(int n, ll k){
    std::vector <bool> vis(n);
    std::vector <int> ret(n, -1);
    vis[n - 1] = true;
    ret[0] = n - 1;
    for (int i = 1; i < n; ++ i){
        for (int j = 0; j < n; ++ j){
            if (vis[j]){
                continue;
            }
            ret[i] = j;
            std::vector <bool> vis1(n), head(n, true);
            for (int kk = 0; kk < n; ++ kk){
                if (ret[kk] != -1){
                    head[ret[kk]] = false;
                }
            }
            int sum = 0;
            for (int kk = 0; kk < n; ++ kk){
                if (ret[kk] == -1){
                    break;
                }
                if (!head[kk]){
                    continue;
                }
                int x = kk, sz = 0;
                bool complete = false;
                while (true){
                    vis1[x] = true;
                    ++ sz;
                    x = ret[x];
                    if (x == -1 || vis1[x]){
                        if (x != -1){
                            complete = true;
                        }
                        break;
                    }
                }
                if (complete && sz < n){
                    sum = -1;
                    break;
                }
            }
            for (int kk = 0; kk < n; ++ kk){
                if (!vis1[kk] && ret[kk] != -1){
                    int x = kk, sz = 0;
                    while (x != -1 && !vis1[x]){
                        ++ sz;
                        vis1[x] = true;
                        x = ret[x];
                    }
                    if (sz != n){
                        sum = -1;
                        break;
                    }
                }
            }
            if (sum == -1){
                continue;
            }
            for (int kk = 0; kk < n; ++ kk){
                if (!vis1[kk] && ret[kk] == -1){
                    ++ sum;
                }
                else if (head[kk]){
                    ++ sum;
                }
            }
            ll value = fac[sum - 1];
            if (k > value){
                k -= value;
                continue;
            }
            else{
                ret[i] = j;
                vis[j] = true;
                break;
            }
        }
    }
    return ret;
}

void solve(int n, ll k, int offset){
    if (n == 0){
        return;
    }
    bool flag = true;
    for (int i = 1; i <= n; ++ i){
        ll value = mult(op[i], dp[n - i]);
        if (k > value){
            k -= value;
            continue;
        }
        flag = false;
        ll num = (k - 1) / dp[n - i] + 1;
        std::vector <int> vec = get(i, num);
        for (auto u : vec){
            ans.push_back(u + offset);
        }
        solve(n - i, k - (num - 1) * dp[n - i], offset + i);
    }
    if (flag){
        ans.push_back(-1);
        return;
    }
}

void solve(int n, ll k){
    if (k > dp[n]){
        puts("-1");
        return;
    }
    solve(n, k, 1);
    if (ans.back() == -1){
        puts("-1");
    }
    else{
        for (int i = 0; i < n; ++ i){
            printf("%d%c", ans[i], " \n"[i == n - 1]);
        }
    }
    ans.clear();
}

int main(){
    fac[0] = 1;
    dp[0] = 1;
    for (int i = 1; i < N; ++ i){
        fac[i] = mult(fac[i - 1], i);
        op[i] = i <= 2 ? 1 : fac[i - 2];
    }
    for (int i = 1; i < N; ++ i){
        for (int j = 1; j <= i; ++ j){
            dp[i] = add(dp[i], mult(op[j], dp[i - j]));
        }
    }
    /*for (int i = 1; i <= dp[6] + 1; ++ i){
        solve(6, i);
    }*/
    int test;
    scanf("%d", &test);
    while (test --){
        int n;
        ll k;
        scanf("%d%lld", &n, &k);
        solve(n, k);
    }
    return 0;
}


#include <bits/stdc++.h>

using ll = long long;
using pii = std::pair <int, int>;
const int N = 60;
const ll INF = ll(1.5e18);

ll mult(ll a, ll b){
    if (!a || !b) return 0;
    return a <= INF / b ? a * b : INF;
}

ll add(ll a, ll b){
    return std::min(INF, a + b);
}

ll dp[N], fac[N], op[N];
std::vector <int> ans;

std::vector <int> get(int n, ll k){
    std::vector <bool> vis(n);
    std::vector <int> ret(n, -1);
    std::vector <int> begin(n, -1), end(n, -1);
    vis[n - 1] = true;
    ret[0] = n - 1;
    begin[n - 1] = 0;
    end[0] = n - 1;
    int chain_number = n - 1;
    for (int i = 1; i < n; ++ i){
        for (int j = 0; j < n; ++ j){
            if (vis[j]){
                continue;
            }
            std::vector <std::pair <int, int>> roll_back_begin, roll_back_end;
            int pre_chain_number = chain_number;
            ret[i] = j;
            if (i == n - 1){
                return ret;
            }
            if (i == j || end[j] == i){
                continue;
            }
            -- chain_number;
            if (begin[i] != -1 && end[j] != -1){
                int x = begin[i], y = end[j];
                roll_back_end.emplace_back(x, end[x]);
                roll_back_begin.emplace_back(y, begin[y]);
                end[x] = y;
                begin[y] = x;
            }
            else if (begin[i] != -1){
                roll_back_end.emplace_back(i, end[i]);
                end[begin[i]] = j;
            }
            else if (end[j] != -1){
                roll_back_begin.emplace_back(j, begin[j]);
                begin[end[j]] = i;
            }
            else{
                roll_back_end.emplace_back(i, end[i]);
                roll_back_begin.emplace_back(j, begin[j]);
                begin[j] = i;
                end[i] = j;
            }
            ll value = fac[chain_number - 1];
            if (k > value){
                k -= value;
                for (auto u : roll_back_begin){
                    begin[u.first] = u.second;
                }
                for (auto u : roll_back_end){
                    end[u.first] = u.second;
                }
                chain_number = pre_chain_number;
                continue;
            }
            else{
                ret[i] = j;
                vis[j] = true;
                break;
            }
        }
    }
}

void solve(int n, ll k, int offset){
    if (n == 0){
        return;
    }
    bool flag = true;
    for (int i = 1; i <= n; ++ i){
        ll value = mult(op[i], dp[n - i]);
        if (k > value){
            k -= value;
            continue;
        }
        flag = false;
        ll num = (k - 1) / dp[n - i] + 1;
        std::vector <int> vec = get(i, num);
        for (auto u : vec){
            ans.push_back(u + offset);
        }
        solve(n - i, k - (num - 1) * dp[n - i], offset + i);
        break;
    }
    if (flag){
        ans.push_back(-1);
        return;
    }
}

void solve(int n, ll k){
    if (k > dp[n]){
        puts("-1");
        return;
    }
    solve(n, k, 1);
    if (ans.back() == -1){
        puts("-1");
    }
    else{
        for (int i = 0; i < n; ++ i){
            printf("%d%c", ans[i], " \n"[i == n - 1]);
        }
    }
    ans.clear();
}

int main(){
    fac[0] = 1;
    dp[0] = 1;
    for (int i = 1; i < N; ++ i){
        fac[i] = mult(fac[i - 1], i);
        op[i] = i <= 2 ? 1 : fac[i - 2];
    }
    for (int i = 1; i < N; ++ i){
        for (int j = 1; j <= i; ++ j){
            dp[i] = add(dp[i], mult(op[j], dp[i - j]));
        }
    }
    int test;
    scanf("%d", &test);
    while (test --){
        int n;
        ll k;
        scanf("%d%lld", &n, &k);
        solve(n, k);
    }
    return 0;
}


#include <bits/stdc++.h>

using ll = long long;
using pii = std::pair <int, int>;
const int N = 60;
const ll INF = ll(1.5e18);

ll mult(ll a, ll b){
    if (!a || !b) return 0;
    return a <= INF / b ? a * b : INF;
}

ll add(ll a, ll b){
    return std::min(INF, a + b);
}

ll dp[N], fac[N], op[N];
std::vector <int> ans;

std::vector <int> get(int n, ll k){
    if (n == 1){
        return {0};
    }
    std::vector <bool> vis(n);
    std::vector <int> ret(n, -1);
    std::vector <int> begin(n, -1), end(n, -1);
    vis[n - 1] = true;
    ret[0] = n - 1;
    begin[n - 1] = 0;
    end[0] = n - 1;
    int chain_number = n - 1;
    for (int i = 1; i < n; ++ i){
        for (int j = 0; j < n; ++ j){
            if (vis[j]){
                continue;
            }
            ret[i] = j;
            if (i == n - 1){
                return ret;
            }
            if (i == j || end[j] == i){
                continue;
            }
            std::vector <std::pair <int, int>> roll_back_begin, roll_back_end;
            int pre_chain_number = chain_number;
            -- chain_number;
            if (begin[i] != -1 && end[j] != -1){
                int x = begin[i], y = end[j];
                roll_back_end.emplace_back(x, end[x]);
                roll_back_begin.emplace_back(y, begin[y]);
                end[x] = y;
                begin[y] = x;
            }
            else if (begin[i] != -1){
                roll_back_end.emplace_back(i, end[i]);
                end[begin[i]] = j;
            }
            else if (end[j] != -1){
                roll_back_begin.emplace_back(j, begin[j]);
                begin[end[j]] = i;
            }
            else{
                roll_back_end.emplace_back(i, end[i]);
                roll_back_begin.emplace_back(j, begin[j]);
                begin[j] = i;
                end[i] = j;
            }
            ll value = fac[chain_number - 1];
            if (k > value){
                k -= value;
                for (auto u : roll_back_begin){
                    begin[u.first] = u.second;
                }
                for (auto u : roll_back_end){
                    end[u.first] = u.second;
                }
                chain_number = pre_chain_number;
                continue;
            }
            else{
                ret[i] = j;
                vis[j] = true;
                break;
            }
        }
    }
    assert(false);
    return {-1};
}

void solve(int n, ll k, int offset){
    if (n == 0){
        return;
    }
    bool flag = true;
    for (int i = 1; i <= n; ++ i){
        ll value = mult(op[i], dp[n - i]);
        if (k > value){
            k -= value;
            continue;
        }
        flag = false;
        ll num = (k - 1) / dp[n - i] + 1;
        std::vector <int> vec = get(i, num);
        for (auto u : vec){
            ans.push_back(u + offset);
        }
        solve(n - i, k - (num - 1) * dp[n - i], offset + i);
        break;
    }
    if (flag){
        ans.push_back(-1);
        return;
    }
}

void solve(int n, ll k){
    if (k > dp[n]){
        puts("-1");
        return;
    }
    solve(n, k, 1);
    if (ans.back() == -1){
        puts("-1");
    }
    else{
        for (int i = 0; i < n; ++ i){
            printf("%d%c", ans[i], " \n"[i == n - 1]);
        }
    }
    ans.clear();
}

int main(){
    fac[0] = 1;
    dp[0] = 1;
    for (int i = 1; i < N; ++ i){
        fac[i] = mult(fac[i - 1], i);
        op[i] = i <= 2 ? 1 : fac[i - 2];
    }
    for (int i = 1; i < N; ++ i){
        for (int j = 1; j <= i; ++ j){
            dp[i] = add(dp[i], mult(op[j], dp[i - j]));
        }
    }
    int test;
    scanf("%d", &test);
    while (test --){
        int n;
        ll k;
        scanf("%d%lld", &n, &k);
        solve(n, k);
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll ts = 100005;

#define IOS  ios::sync_with_stdio(0); cin.tie(0);
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ii pair<int,int>
ll ans[55];
ll  dp[55];
ll cycle[55];
bool used[55];
void solve() {
    ll n , k; cin >> n >> k;
    cycle[0] = 1;
    cycle[1] = 1;
    k--;
    for(int i = 2; i <= n ; i++) {
	    cycle[i] = cycle[i - 1] * (i - 1) ;
	}
	dp[n] = 1;
	for (int i = n - 1; i >= 0; i--) {
		dp[i] = 0;
		for(int val = i ; val <= n - 1;val++) {
			int len = (val - i) + 1;
		    dp[i] += dp[i + len] * cycle[len - 1];
		}
	}
	if(dp[0] <= k) {
		cout <<"-1" << endl;
		return ;
	}
	memset(ans , -1,sizeof(ans));
	memset(used, 0, sizeof(used));
	for(int i = 0; i <= n - 1 ; i++)  {
		for(int val = i ; val <= n - 1; val++) {
			int len = (val - i) + 1;
			int cur = dp[val + 1] * cycle[len -1] ;
			//cout << i + 1  << " " << val + 1 << " " << cur << " " << k << endl;
			if(cur <= k) {
				k -= cur;
				continue;
			}
			ans[i] = val ;
			used[val] = true;
			for(int j = i + 1; j < i + len; j++)  {
				int lft = len - (j - i) - 1 ;
				for(int now_value = i ; now_value < val; now_value++) {

					if(!used[now_value] && now_value != j)  {
						if(j != val) {
							 int m = ans[now_value];
							 while(m != -1 && m != j) {
							 	m = ans[m];
							 }
							 if(m == j ) continue;
						}

				     	int res = cycle[lft] * dp[val + 1];
				    	if(res <= k ) {
				      	    k-= res;
					        continue;
				    	}
				     	ans[j] = now_value;
				     //	cout << now_value << endl;
				    	used[now_value] = true;
				    	break;
				    }
				}
			}
			i = i + len - 1 ;
			break;
		}
	}
	for(int i = 1; i <= n ; i++) cout << ans[i - 1]   + 1<< " ";
	cout << endl;
//	for(int i = 1; i<= n ; i++) cout << dp[i - 1] << endl;
//	cout << endl;
}
signed main() {

    IOS;
    int tc; cin >> tc ;
    while(tc--) {
    	 solve();
	}

 }


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll ts = 100005;

#define IOS  ios::sync_with_stdio(0); cin.tie(0);
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ii pair<int,int>
#define int ll
ll ans[55];
ll  dp[55];
ll cycle[55];
bool used[55];
ll  mul(ll i , ll j) {
	if(j > LLONG_MAX / i ) return LLONG_MAX ;
	else return i * j ;
}
void solve() {
    ll n , k; cin >> n >> k;
    cycle[0] = 1;
    cycle[1] = 1;
    k--;
    for(int i = 2; i <= n ; i++) {
	    cycle[i] = cycle[i - 1] * (i - 1) ;
	}
	dp[n] = 1;
	for (int i = n - 1; i >= 0; i--) {
		dp[i] = 0;
		for(int val = i ; val <= n - 1;val++) {
			int len = (val - i) + 1;
		    dp[i] += mul(dp[i + len] , cycle[len - 1] ) ;
		}
	}
	if(dp[0] <= k) {
		cout <<"-1" << endl;
		return ;
	}
	memset(ans , -1,sizeof(ans));
	memset(used, 0, sizeof(used));
	for(int i = 0; i <= n - 1 ; i++)  {
		for(int val = i ; val <= n - 1; val++) {
			int len = (val - i) + 1;
			int cur = mul(dp[val + 1]  , cycle[len -1] ) ;
			//cout << i + 1  << " " << val + 1 << " " << cur << " " << k << endl;
			if(cur <= k) {
				k -= cur;
				continue;
			}
			ans[i] = val ;
			used[val] = true;
			for(int j = i + 1; j < i + len; j++)  {
				int lft = len - (j - i) - 1 ;
				for(int now_value = i ; now_value < val; now_value++) {

					if(!used[now_value] && now_value != j)  {
						if(j != val) {
							 int m = ans[now_value];
							 while(m != -1 && m != j) {
							 	m = ans[m];
							 }
							 if(m == j ) continue;
						}

				     	int res = mul( cycle[lft] , dp[val + 1] ) ;
				    	if(res <= k ) {
				      	    k-= res;
					        continue;
				    	}
				     	ans[j] = now_value;
				     //	cout << now_value << endl;
				    	used[now_value] = true;
				    	break;
				    }
				}
			}
			i = i + len - 1 ;
			break;
		}
	}
	for(int i = 1; i <= n ; i++) cout << ans[i - 1]   + 1<< " ";

	cout << endl;
//	for(int i = 1; i<= n ; i++) cout << dp[i - 1] << endl;
//	cout << endl;
}
signed main() {

    IOS;
    int tc; cin >> tc ;
    while(tc--) {
    	 solve();
	}

 }


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll ts = 100005;

#define IOS  ios::sync_with_stdio(0); cin.tie(0);
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ii pair<int,int>
#define int ll
ll ans[55];
ll  dp[55];
ll cycle[55];
bool used[55];
ll  mul(ll i , ll j) {
	if(j > LLONG_MAX / i ) return LLONG_MAX ;
	else return i * j ;
}
ll add(ll i , ll j) {
	if(i > LLONG_MAX - j) return LLONG_MAX;
	else return i + j ;
}
void solve() {
    ll n , k; cin >> n >> k;
    cycle[0] = 1;
    cycle[1] = 1;
    k--;
    for(int i = 2; i <= n ; i++) {
	    cycle[i] = mul(cycle[i - 1] , i -1  )  ;
	}
	dp[n] = 1;
	for (int i = n - 1; i >= 0; i--) {
		dp[i] = 0;
		for(int val = i ; val <= n - 1;val++) {
			int len = (val - i) + 1;
		    dp[i] = add(dp[i] , mul(dp[i + len] , cycle[len - 1] ) );
		}
	}
	if(dp[0] <= k) {
		cout <<"-1" << endl;
		return ;
	}
	memset(ans , -1,sizeof(ans));
	memset(used, 0, sizeof(used));
	for(int i = 0; i <= n - 1 ; i++)  {
		for(int val = i ; val <= n - 1; val++) {
			int len = (val - i) + 1;
			int cur = mul(dp[val + 1]  , cycle[len -1] ) ;
			//cout << i + 1  << " " << val + 1 << " " << cur << " " << k << endl;
			if(cur <= k) {
				k -= cur;
				continue;
			}
			ans[i] = val ;
			used[val] = true;
			for(int j = i + 1; j < i + len; j++)  {
				int lft = len - (j - i) - 1 ;
				for(int now_value = i ; now_value < val; now_value++) {

					if(!used[now_value] && now_value != j)  {
						if(j != val) {
							 int m = ans[now_value];
							 while(m != -1 && m != j) {
							 	m = ans[m];
							 }
							 if(m == j ) continue;
						}

				     	int res = mul( cycle[lft] , dp[val + 1] ) ;
				    	if(res <= k ) {
				      	    k-= res;
					        continue;
				    	}
				     	ans[j] = now_value;
				     //	cout << now_value << endl;
				    	used[now_value] = true;
				    	break;
				    }
				}
			}
			i = i + len - 1 ;
			break;
		}
	}
	for(int i = 1; i <= n ; i++) cout << ans[i - 1]   + 1<< " ";

	cout << endl;
//	for(int i = 1; i<= n ; i++) cout << dp[i - 1] << endl;
//	cout << endl;
}
signed main() {

    IOS;
    int tc; cin >> tc ;
    while(tc--) {
    	 solve();
	}

 }


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll ts = 100005;

#define IOS  ios::sync_with_stdio(0); cin.tie(0);
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ii pair<int,int>
#define int ll
ll ans[55];
ll  dp[55];
ll cycle[55];
bool used[55];
ll  mul(ll i , ll j) {
	if(j > LLONG_MAX / i ) return LLONG_MAX ;
	else return i * j ;
}
void solve() {
    ll n , k; cin >> n >> k;
    cycle[0] = 1;
    cycle[1] = 1;
    k--;
    for(int i = 2; i <= n ; i++) {
	    cycle[i] = mul(cycle[i - 1] , i -1  )  ;
	}
	dp[n] = 1;
	for (int i = n - 1; i >= 0; i--) {
		dp[i] = 0;
		for(int val = i ; val <= n - 1;val++) {
			int len = (val - i) + 1;
		    dp[i] += mul(dp[i + len] , cycle[len - 1] ) ;
		}
	}
	if(dp[0] <= k) {
		cout <<"-1" << endl;
		return ;
	}
	memset(ans , -1,sizeof(ans));
	memset(used, 0, sizeof(used));
	for(int i = 0; i <= n - 1 ; i++)  {
		for(int val = i ; val <= n - 1; val++) {
			int len = (val - i) + 1;
			int cur = mul(dp[val + 1]  , cycle[len -1] ) ;
			//cout << i + 1  << " " << val + 1 << " " << cur << " " << k << endl;
			if(cur <= k) {
				k -= cur;
				continue;
			}
			ans[i] = val ;
			used[val] = true;
			for(int j = i + 1; j < i + len; j++)  {
				int lft = len - (j - i) - 1 ;
				for(int now_value = i ; now_value < val; now_value++) {

					if(!used[now_value] && now_value != j)  {
						if(j != val) {
							 int m = ans[now_value];
							 while(m != -1 && m != j) {
							 	m = ans[m];
							 }
							 if(m == j ) continue;
						}

				     	int res = mul( cycle[lft] , dp[val + 1] ) ;
				    	if(res <= k ) {
				      	    k-= res;
					        continue;
				    	}
				     	ans[j] = now_value;
				     //	cout << now_value << endl;
				    	used[now_value] = true;
				    	break;
				    }
				}
			}
			i = i + len - 1 ;
			break;
		}
	}
	for(int i = 1; i <= n ; i++) cout << ans[i - 1]   + 1<< " ";

	cout << endl;
//	for(int i = 1; i<= n ; i++) cout << dp[i - 1] << endl;
//	cout << endl;
}
signed main() {

    IOS;
    int tc; cin >> tc ;
    while(tc--) {
    	 solve();
	}

 }


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll ts = 100005;

#define IOS  ios::sync_with_stdio(0); cin.tie(0);
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ii pair<int,int>
ll ans[55];
ll  dp[55];
ll cycle[55];
bool used[55];
void solve() {
    ll n , k; cin >> n >> k;
    cycle[0] = 1;
    cycle[1] = 1;
    k--;
    for(int i = 2; i <= n ; i++) {
	    cycle[i] = cycle[i - 1] * (i - 1) ;
	}
	dp[n] = 1;
	for (int i = n - 1; i >= 0; i--) {
		dp[i] = 0;
		for(int val = i ; val <= n - 1;val++) {
			int len = (val - i) + 1;
		    dp[i] += dp[i + len] * cycle[len - 1];
		}
	}
	if(dp[0] <= k) {
	    cout << "-1";
	    return ;
	}
	memset(ans , -1,sizeof(ans));
	memset(used, 0, sizeof(used));
	for(int i = 0; i <= n - 1 ; i++)  {
		for(int val = i ; val <= n - 1; val++) {
			int len = (val - i) + 1;
			int cur = dp[val + 1] * cycle[len -1] ;
		//	cout << i  << " " << val << " " << cur << " " << k << endl;
			if(cur <= k) {
				k -= cur;
				continue;
			}
			ans[i] = val ;
			used[val] = true;
			for(int j = i + 1; j < i + len; j++)  {
				int lft = len - (j - i) - 1 ;
				for(int now_value = i ; now_value < val; now_value++) {

					if(!used[now_value] && now_value != j)  {
						if(j != val) {
							 int m = ans[now_value];
							 while(m != -1 && m != j) {
							 	m = ans[m];
							 }
							 if(m == j ) continue;
						}

				     	int res = cycle[lft] * dp[val + 1];
				    	if(res <= k ) {
				      	    k-= res;
					        continue;
				    	}
				     	ans[j] = now_value;
				     //	cout << now_value << endl;
				    	used[now_value] = true;
				    	break;
				    }
				}
			}
			i = i + len - 1 ;
			break;
		}
	}
	for(int i = 1; i <= n ; i++) cout << ans[i - 1]   + 1<< " ";
	cout << endl;
//	for(int i = 1; i<= n ; i++) cout << dp[i - 1] << endl;
//	cout << endl;
}
signed main() {

    IOS;
    int tc; cin >> tc ;
    while(tc--) {
    	 solve();
	}

 }


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll ts = 100005;

#define IOS  ios::sync_with_stdio(0); cin.tie(0);
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ii pair<int,int>
ll ans[55];
ll  dp[55];
ll cycle[55];
bool used[55];
void solve() {
    ll n , k; cin >> n >> k;
    cycle[0] = 1;
    cycle[1] = 1;
    k--;
    for(int i = 2; i <= n ; i++) {
	    cycle[i] = cycle[i - 1] * (i - 1) ;
	}
	dp[n] = 1;
	for (int i = n - 1; i >= 0; i--) {
		dp[i] = 0;
		for(int val = i ; val <= n - 1;val++) {
			int len = (val - i) + 1;
		    dp[i] += dp[i + len] * cycle[len - 1];
		}
	}
	memset(ans , -1,sizeof(ans));
	memset(used, 0, sizeof(used));
	for(int i = 0; i <= n - 1 ; i++)  {
		for(int val = i ; val <= n - 1; val++) {
			int len = (val - i) + 1;
			int cur = dp[val + 1] * cycle[len -1] ;
		//	cout << i  << " " << val << " " << cur << " " << k << endl;
			if(cur <= k) {
				k -= cur;
				continue;
			}
			ans[i] = val ;
			used[val] = true;
			for(int j = i + 1; j < i + len; j++)  {
				int lft = len - (j - i) - 1 ;
				for(int now_value = i ; now_value < val; now_value++) {

					if(!used[now_value] && now_value != j)  {
						if(j != val) {
							 int m = ans[now_value];
							 while(m != -1 && m != j) {
							 	m = ans[m];
							 }
							 if(m == j ) continue;
						}

				     	int res = cycle[lft] * dp[val + 1];
				    	if(res <= k ) {
				      	    k-= res;
					        continue;
				    	}
				     	ans[j] = now_value;
				     //	cout << now_value << endl;
				    	used[now_value] = true;
				    	break;
				    }
				}
			}
			i = i + len - 1 ;
			break;
		}
	}
	for(int i = 1; i <= n ; i++) cout << ans[i - 1]   + 1<< " ";
	cout << endl;
//	for(int i = 1; i<= n ; i++) cout << dp[i - 1] << endl;
//	cout << endl;
}
signed main() {

    IOS;
    int tc; cin >> tc ;
    while(tc--) {
    	 solve();
	}

 }


#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#define INF 2000000000000000000ll

using namespace std;

typedef long long ll;

ll cyc[51];
ll dp[51];
int ans[51];
int used[51];

ll muli(ll a, ll b) {
	if (a&&INF / a < b)
		return INF;
	return min(INF, a*b);
}
ll addi(ll a, ll b) {
	if (a + b > INF)
		return INF;
	return a + b;
}

int main() {
	ios::sync_with_stdio(0);
	cyc[0] = 1;
	cyc[1] = 1;
	cyc[2] = 1;
	for (int i = 3; i < 51; i++)
		cyc[i] = muli(cyc[i - 1], i - 2);
	int t;
	cin >> t;
	while (t--) {
		int n;
		ll k;
		cin >> n >> k;
		for (int i = 1; i <= n; i++)
			ans[i] = used[i] = 0;
		dp[n + 1] = 1;
		for (int i = n; i >= 1; i--) {
			dp[i] = 0;
			for (int j = i; j <= n; j++)
				dp[i] = addi(dp[i],muli(cyc[j - i + 1], dp[j+1]));
		}
		if (dp[1] < k) {
			cout << "-1\n";
			continue;
		}
		for (int i = 1; i <= n; i++) {
			for (int j = i; j <= n; j++) {
				if (muli(cyc[j - i + 1], dp[j + 1]) < k) {
					k -= muli(cyc[j - i + 1], dp[j + 1]);
					continue;
				}
				ans[i] = j;
				used[j] = i;
				for (int p = i + 1; p <= j; p++) {
					for (int v = i; v <= j; v++) {
						if (used[v])
							continue;
						ans[p] = v;
						int p2 = p;
						bool failed = false;
						while (p!=j&&p2 <= p) {
							p2 = ans[p2];
							if (p2 == p) {
								failed = true;
								break;
							}
						}
						if (failed)
							continue;
						int rem = (j - i + 1) - (p-i+1);
						ll after = muli(cyc[rem], dp[j + 1]);
						if (after < k) {
							k -= after;
							continue;
						}
						used[v] = p;
						break;
					}
				}
				i = j;
				break;
			}
		}
		for (int i = 1; i <= n; i++)
			cout << ans[i] << " ";
		cout << "\n";
	}
	//system("pause");
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#define INF 2000000000000000000ll

using namespace std;

typedef long long ll;

ll cyc[51];
ll dp[51];
int ans[51];
int used[51];

ll muli(ll a, ll b) {
	if (a&&INF / a < b)
		return INF;
	return min(INF, a*b);
}
ll addi(ll a, ll b) {
	if (a + b > INF)
		return INF;
	return a + b;
}

int main() {
	ios::sync_with_stdio(0);
	cyc[0] = 1;
	cyc[1] = 1;
	cyc[2] = 1;
	for (int i = 3; i < 51; i++)
		cyc[i] = muli(cyc[i - 1], i - 2);
	int t;
	cin >> t;
	while (t--) {
		int n;
		ll k;
		cin >> n >> k;
		for (int i = 1; i <= n; i++)
			ans[i] = used[i] = 0;
		dp[n + 1] = 1;
		for (int i = n; i >= 1; i--) {
			dp[i] = 0;
			for (int j = i; j <= n; j++)
				dp[i] = addi(dp[i],muli(cyc[j - i + 1], dp[j+1]));
		}
		if (dp[1] < k) {
			cout << "-1\n";
			continue;
		}
		for (int i = 1; i <= n; i++) {
			for (int j = i; j <= n; j++) {
				if (muli(cyc[j - i + 1], dp[j + 1]) < k) {
					k -= muli(cyc[j - i + 1], dp[j + 1]);
					continue;
				}
				ans[i] = j;
				used[j] = i;
				for (int p = i + 1; p <= j; p++) {
					for (int v = i; v <= j; v++) {
						if (used[v])
							continue;
						ans[p] = v;
						int p2 = p;
						bool failed = false;
						while (p!=j&&p2 <= p) {
							p2 = ans[p2];
							if (p2 == p) {
								failed = true;
								break;
							}
						}
						if (failed)
							continue;
						int rem = (j - i + 1) - (p-i+1);
						ll after = muli(cyc[rem+1], dp[j + 1]);
						if (after < k) {
							k -= after;
							continue;
						}
						used[v] = p;
						break;
					}
				}
				i = j;
				break;
			}
		}
		for (int i = 1; i <= n; i++)
			cout << ans[i] << " ";
		cout << "\n";
	}
	//system("pause");
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define pb push_back
#define mp make_pair

const int N = 55;
const long long inf = 1e18;

int tc, n, m;
long long k;
long long f[N], f1[N];
vector<int> aa;

long long mul(long long a, long long b)
{
    return inf / a < b ? inf : a * b;
}

long long fs(int n)
{
    return f1[max(n - 1, 0)];
}

void pre()
{
    f[0] = f[1] = 1;
    f1[0] = f1[1] = 1;
    for (int i = 2; i < N; i++)
    {
        f1[i] = mul(f1[i - 1], i);
        for (int j = 1; j <= i; j++)
        {
            f[i] = min(inf, f[i] + mul(fs(j - 1), f[i - j]));
        }
        //printf("%d %lld %lld\n", i, f1[i], f[i]);
    }
}

void upd(vector<int>& v, set<int>& s)
{
    s.clear();
    for (int i = 0; i < v.size(); i++)
    {
        v[i]--;
        s.insert(v[i]);
    }
}

vector<int> rua(int n, long long r, vector<int>& v, set<int>& s)
{
    if (n == 1)
    {
        return vector<int>(1, 1);
    }
    vector<int> ret;
    long long rr = (r - 1) / fs(n - 1) + 1;
    int selected = -1;
    for (int i = 0; i < v.size() && rr > 0; i++)
    {
        if (v[i] <= 1)
        {
            continue;
        }
        rr--;
        if (rr == 0)
        {
            selected = v[i];
        }
    }
    for (int i = 2; i <= n && rr > 0; i++)
    {
        if (s.find(i) != s.end())
        {
            continue;
        }
        rr--;
        if (rr == 0)
        {
            selected = i;
        }
    }
    assert(selected > 0);
    ret.pb(selected);
    upd(v, s);
    vector<int> last = rua(n - 1, (r - 1) % fs(n - 1) + 1, v, s);
    for (int i = 0; i < last.size(); i++)
    {
        ret.pb(last[i] == selected - 1 ? 1 : last[i] + 1);
    }
    return ret;
}

void dfs(int n, long long r, int w)
{
    if (n == 1)
    {
        aa.pb(n + w);
        return;
    }
    for (int i = 1; i <= n; i++)
    {
        long long t = mul(fs(i - 1), f[n - i]);
        if (t < r)
        {
            r -= t;
            continue;
        }
        long long rr = (r - 1) / f[n - i] + 1;
        aa.pb(i + w);
        if (i > 1)
        {
            vector<int> vp;
            set<int> sp;
            vp.pb(i - 1);
            sp.insert(i - 1);
            vector<int> v = rua(i - 1, rr, vp, sp);
            for (int j = 0; j < v.size(); j++)
            {
                int t = v[j];
                t++;
                if (t == i)
                {
                    t = 1;
                }
                aa.pb(t + w);
            }
        }
        dfs(n - i, (r - 1) % f[n - i] + 1, w + i);
        break;
    }
}

int main()
{
    pre();
    cin >> tc;
    while (tc--)
    {
        cin >> n >> k;
        if (k > f[n])
        {
            puts("-1");
            continue;
        }
        aa.clear();
        dfs(n, k, 0);
        for (int i = 0; i < aa.size(); i++)
        {
            printf("%d%c", aa[i], i + 1 == aa.size() ? '\n' : ' ');
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define pb push_back
#define mp make_pair

const int N = 55;
const long long inf = 1e18;

int tc, n, m;
long long k;
long long f[N], f1[N];
vector<int> aa;

long long mul(long long a, long long b)
{
    return inf / a < b ? inf : a * b;
}

long long fs(int n)
{
    return f1[max(n - 1, 0)];
}

void pre()
{
    f[0] = f[1] = 1;
    f1[0] = f1[1] = 1;
    for (int i = 2; i < N; i++)
    {
        f1[i] = mul(f1[i - 1], i);
        for (int j = 1; j <= i; j++)
        {
            f[i] = min(inf, f[i] + mul(fs(j - 1), f[i - j]));
        }
        //printf("%d %lld %lld\n", i, f1[i], f[i]);
    }
}

vector<int> rua(int n, long long r, int p)
{
    //printf("- %d %d %d\n", n, r, p);
    if (n == 1)
    {
        return vector<int>(1, 1);
    }
    vector<int> ret;
    long long rr = (r - 1) / fs(n - 1) + 1;
    int selected = rr == 1 ? p : (rr > p ? rr : rr - 1);
    ret.pb(selected);
    vector<int> last = rua(n - 1, (r - 1) % fs(n - 1) + 1, selected == 1 ? n - 1 : selected - 1);
    for (int i = 0; i < last.size(); i++)
    {
        ret.pb(last[i] == selected - 1 ? 1 : last[i] + 1);
    }
    return ret;
}

void dfs(int n, long long r, int w)
{
    if (n == 1)
    {
        aa.pb(n + w);
        return;
    }
    for (int i = 1; i <= n; i++)
    {
        long long t = mul(fs(i - 1), f[n - i]);
        if (t < r)
        {
            r -= t;
            continue;
        }
        long long rr = (r - 1) / f[n - i] + 1;
        aa.pb(i + w);
        if (i > 1)
        {
            vector<int> v = rua(i - 1, rr, i - 1);
            for (int j = 0; j < v.size(); j++)
            {
                int t = v[j];
                t++;
                if (t == i)
                {
                    t = 1;
                }
                aa.pb(t + w);
            }
        }
        dfs(n - i, (r - 1) % f[n - i] + 1, w + i);
        break;
    }
}

int main()
{
    pre();
    cin >> tc;
    while (tc--)
    {
        cin >> n >> k;
        if (k > f[n])
        {
            puts("-1");
            continue;
        }
        aa.clear();
        dfs(n, k, 0);
        for (int i = 0; i < aa.size(); i++)
        {
            printf("%d%c", aa[i], i + 1 == aa.size() ? '\n' : ' ');
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define pb push_back
#define mp make_pair

const int N = 55;
const long long inf = 1e18;

int tc, n, m;
long long k;
long long f[N], f1[N];
vector<int> aa;

long long mul(long long a, long long b)
{
    return inf / a < b ? inf : a * b;
}

long long fs(int n)
{
    return f1[max(n - 1, 0)];
}

void pre()
{
    f[0] = f[1] = 1;
    f1[0] = f1[1] = 1;
    for (int i = 2; i < N; i++)
    {
        f1[i] = mul(f1[i - 1], i);
        for (int j = 1; j <= i; j++)
        {
            f[i] = min(inf, f[i] + mul(fs(j - 1), f[i - j]));
        }
        //printf("%d %lld %lld\n", i, f1[i], f[i]);
    }
}

vector<int> rua(int n, long long r)
{
    vector<int> v(n + 1), ret;
    for (int i = 1; i <= n; i++)
    {
        v[i] = i == 1 ? n : i - 1;
    }
    for (int nn = n; nn > 0; nn--)
    {
        long long rr = (r - 1) / fs(nn - 1) + 1;
        ret.pb(v[rr]);
        v.erase(v.begin() + rr, v.begin() + rr + 1);
        r = (r - 1) % fs(nn - 1) + 1;
    }
    return ret;
}

void dfs(int n, long long r, int w)
{
    if (n == 1)
    {
        aa.pb(n + w);
        return;
    }
    for (int i = 1; i <= n; i++)
    {
        long long t = mul(fs(i - 1), f[n - i]);
        if (t < r)
        {
            r -= t;
            continue;
        }
        long long rr = (r - 1) / f[n - i] + 1;
        aa.pb(i + w);
        if (i > 1)
        {
            vector<int> v = rua(i - 1, rr);
            for (int j = 0; j < v.size(); j++)
            {
                int t = v[j];
                t++;
                if (t == i)
                {
                    t = 1;
                }
                aa.pb(t + w);
            }
        }
        dfs(n - i, (r - 1) % f[n - i] + 1, w + i);
        break;
    }
}

int main()
{
    pre();
    cin >> tc;
    while (tc--)
    {
        cin >> n >> k;
        if (k > f[n])
        {
            puts("-1");
            continue;
        }
        aa.clear();
        dfs(n, k, 0);
        for (int i = 0; i < aa.size(); i++)
        {
            printf("%d%c", aa[i], i + 1 == aa.size() ? '\n' : ' ');
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define pb push_back
#define mp make_pair

const int N = 55;
const long long inf = 1e18;

int tc, n, m;
long long k;
long long f[N], f1[N];
vector<int> aa;

long long mul(long long a, long long b)
{
    return inf / a < b ? inf : a * b;
}

long long fs(int n)
{
    return f1[max(n - 1, 0)];
}

void pre()
{
    f[0] = f[1] = 1;
    f1[0] = f1[1] = 1;
    for (int i = 2; i < N; i++)
    {
        f1[i] = mul(f1[i - 1], i);
        for (int j = 1; j <= i; j++)
        {
            f[i] = min(inf, f[i] + mul(fs(j - 1), f[i - j]));
        }
        //printf("%d %lld %lld\n", i, f1[i], f[i]);
    }
}

void upd(vector<int>& v, set<int>& s)
{
    s.clear();
    for (int i = 0; i < v.size(); i++)
    {
        v[i]--;
        s.insert(v[i]);
    }
}

vector<int> rua(int n, long long r, vector<int>& v, set<int>& s)
{
    if (n == 1)
    {
        return vector<int>(1, 1);
    }
    vector<int> ret;
    long long rr = (r - 1) / fs(n - 1) + 1;
    int selected = -1;
    for (int i = 0; i < v.size() && rr > 0; i++)
    {
        if (v[i] <= 1)
        {
            continue;
        }
        rr--;
        if (rr == 0)
        {
            selected = v[i];
        }
    }
    for (int i = 2; i <= n && rr > 0; i++)
    {
        if (s.find(i) != s.end())
        {
            continue;
        }
        rr--;
        if (rr == 0)
        {
            selected = i;
        }
    }
    assert(selected > 0);
    ret.pb(selected);
    //printf("select: %d\n", selected);
    if (s.find(selected) == s.end())
    {
        v.pb(selected);
        s.insert(selected);
    }
    else
    {
        vector<int> vv;
        for (int i = 0; i < v.size(); i++)
        {
            if (v[i] != selected)
            {
                vv.pb(v[i]);
            }
        }
        vv.pb(selected);
        v = vv;
    }
    upd(v, s);
    /*
    printf("v: ");
    for (int i = 0; i < v.size(); i++)
    {
        printf("%d%c", v[i], i == v.size() - 1 ? '\n' : ' ');
    }
    */
    vector<int> last = rua(n - 1, (r - 1) % fs(n - 1) + 1, v, s);
    for (int i = 0; i < last.size(); i++)
    {
        ret.pb(last[i] == selected - 1 ? 1 : last[i] + 1);
    }
    /*
    printf("ret: ");
    for (int i = 0; i < ret.size(); i++)
    {
        printf("%d%c", ret[i], i == ret.size() - 1 ? '\n' : ' ');
    }
    */
    return ret;
}

void dfs(int n, long long r, int w)
{
    if (n == 1)
    {
        aa.pb(n + w);
        return;
    }
    for (int i = 1; i <= n; i++)
    {
        long long t = mul(fs(i - 1), f[n - i]);
        if (t < r)
        {
            r -= t;
            continue;
        }
        long long rr = (r - 1) / f[n - i] + 1;
        aa.pb(i + w);
        if (i > 1)
        {
            vector<int> vp;
            set<int> sp;
            vp.pb(i - 1);
            sp.insert(i - 1);
            vector<int> v = rua(i - 1, rr, vp, sp);
            for (int j = 0; j < v.size(); j++)
            {
                int t = v[j];
                t++;
                if (t == i)
                {
                    t = 1;
                }
                aa.pb(t + w);
            }
        }
        dfs(n - i, (r - 1) % f[n - i] + 1, w + i);
        break;
    }
}

int main()
{
    pre();
    cin >> tc;
    while (tc--)
    {
        cin >> n >> k;
        if (k > f[n])
        {
            puts("-1");
            continue;
        }
        aa.clear();
        dfs(n, k, 0);
        for (int i = 0; i < aa.size(); i++)
        {
            printf("%d%c", aa[i], i + 1 == aa.size() ? '\n' : ' ');
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define pb push_back
#define mp make_pair

const int N = 55;
const long long inf = 1e18;

int tc, n, m;
long long k;
long long f[N], f1[N];
vector<int> aa;

long long mul(long long a, long long b)
{
    return inf / a < b ? inf : a * b;
}

long long fs(int n)
{
    return f1[max(n - 1, 0)];
}

void pre()
{
    f[0] = f[1] = 1;
    f1[0] = f1[1] = 1;
    for (int i = 2; i < N; i++)
    {
        f1[i] = mul(f1[i - 1], i);
        for (int j = 1; j <= i; j++)
        {
            f[i] = min(inf, f[i] + mul(fs(j - 1), f[i - j]));
        }
        //printf("%d %lld %lld\n", i, f1[i], f[i]);
    }
}

int chk(vector<int>& v, int x, int y)
{
    if (x == y)
    {
        return 0;
    }
    set<int> vis;
    vis.insert(x);
    for (int i = y;;)
    {
        if (vis.find(i) != vis.end())
        {
            return 0;
        }
        if (i > v.size())
        {
            break;
        }
        vis.insert(i);
        i = v[i - 1];
    }
    return 1;
}

vector<int> rua(int n, long long r)
{
    if (n == 1)
    {
        return vector<int>(1, 1);
    }
    vector<int> ret(1, n);
    set<int> s;
    for (int i = 1; i < n; i++)
    {
        s.insert(i);
    }
    for (int i = 1; i < n; i++)
    {
        if (i == n - 1)
        {
            ret.pb(*s.begin());
            break;
        }
        long long cur = fs(n - i - 1);
        int selected = -1;
        for (set<int>::iterator it = s.begin(); it != s.end(); it++)
        {
            if (!chk(ret, i + 1, *it))
            {
                continue;
            }
            if (r <= cur)
            {
                selected = *it;
                break;
            }
            r -= cur;
        }
        assert(selected > 0);
        ret.pb(selected);
        s.erase(selected);
    }
    return ret;
}

void dfs(int n, long long r, int w)
{
    if (n == 1)
    {
        aa.pb(n + w);
        return;
    }
    for (int i = 1; i <= n; i++)
    {
        long long t = mul(fs(i - 1), f[n - i]);
        if (t < r)
        {
            r -= t;
            continue;
        }
        long long rr = (r - 1) / f[n - i] + 1;
        vector<int> v = rua(i, rr);
        for (int j = 0; j < v.size(); j++)
        {
            aa.pb(v[j] + w);
        }
        dfs(n - i, (r - 1) % f[n - i] + 1, w + i);
        break;
    }
}

int main()
{
    pre();
    cin >> tc;
    while (tc--)
    {
        cin >> n >> k;
        if (k > f[n])
        {
            puts("-1");
            continue;
        }
        aa.clear();
        dfs(n, k, 0);
        for (int i = 0; i < aa.size(); i++)
        {
            printf("%d%c", aa[i], i + 1 == aa.size() ? '\n' : ' ');
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 52;
const ll oo = 2e18;

ll _fat[N + 1];
ll *fat = _fat + 1;
ll mul(ll a, ll b) {
	if(b and a > oo / b) return oo;
	return a * b;
}
ll add(ll a, ll b) {
	return a + b >= oo ? oo : a + b;
}

ll dp[N];
int ans[N];
int used[N];

void solve(int n, ll k) {
	dp[n] = 1;
	for(int i = n - 1; i >= 0; i--) {
		dp[i] = 0;
		for(int j = i; j < n; j++) {
			dp[i] = add(dp[i], mul(fat[j - i - 1], dp[j + 1]));
		}
	}
	if(dp[0] < k) {
		printf("-1\n");
		return;
	}

	memset(used, 0, sizeof used);

	for(int i = 0; i < n; i++) {
		int j = i;
		while(1) {
			ll tmp = mul(fat[j - i - 1], dp[j + 1]);
			if(tmp >= k) break;
			else {
				k -= tmp;
				j++;
			}
		}
		ans[i] = j;
		used[ ans[i] ] = 1;
		if(i + 1 == j) {
			ans[i + 1] = i;
			used[ ans[i + 1] ] = 1;
		}
		else {
			for(int z = i + 1; z <= j; z++) {
				int w = 0;
				while(1) {
					ll tmp = mul(fat[j - z - 1], dp[j + 1]);
					if(tmp >= k) break;
					else {
						k -= tmp;
						w++;
					}
				}
				for(int q = 0; q <= w; q++) w += used[q];
				ans[z] = w;
				used[ ans[z] ] = 1;
			}
			reverse(ans + i + 1, ans + j + 1);
		}
		i = j;
	}
	assert(k == 1);
	for(int i = 0; i < n; i++) printf("%d ", ans[i] + 1);
	printf("\n");
}

int main() {
	fat[-1] = 1;
	fat[0] = 1;
	for(int i = 1; i < N; i++) fat[i] = mul(i, fat[i - 1]);

	int t;

	scanf("%d", &t);

	while(t--) {
		int n; ll k;
		scanf("%d %lld", &n, &k);
		solve(n, k);
	}
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 52;
const ll oo = 2e18;

ll _fat[N + 1];
ll *fat = _fat + 1;
ll mul(ll a, ll b) {
	if(b and a > oo / b) return oo;
	return a * b;
}
ll add(ll a, ll b) {
	return a + b >= oo ? oo : a + b;
}

ll dp[N];
int ans[N];

void solve(int n, ll k) {
	dp[n] = 1;
	for(int i = n - 1; i >= 0; i--) {
		dp[i] = 0;
		for(int j = i; j < n; j++) {
			dp[i] = add(dp[i], mul(fat[j - i - 1], dp[j + 1]));
		}
	}
	if(dp[0] < k) {
		printf("-1\n");
		return;
	}

	for(int i = 0; i < n; i++) {
		int j = i;
		while(1) {
			ll tmp = mul(fat[j - i - 1], dp[j + 1]);
			if(tmp >= k) break;
			else {
				k -= tmp;
				j++;
			}
		}
		ans[i] = j;
		for(int z = i + 1; z <= j; z++) {
			j - z;
			int w = 0;
			while(1) {
				ll tmp = mul(fat[j - z - 1], dp[j + 1]);
				if(tmp >= k) break;
				else {
					k -= tmp;
					w++;
				}
			}
			int add = 0;
			for(int q = i + 1; q < z; q++) add += w <= ans[q];
			w += add;
			ans[z] = i + w;
		}
		i = j;
	}
	assert(k == 1);
	for(int i = 0; i < n; i++) printf("%d ", ans[i] + 1);
	printf("\n");
}

int main() {
	fat[-1] = 1;
	fat[0] = 1;
	for(int i = 1; i < N; i++) fat[i] = mul(i, fat[i - 1]);

	int t;

	scanf("%d", &t);

	while(t--) {
		int n; ll k;
		scanf("%d %lld", &n, &k);
		solve(n, k);
	}
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 52;
const ll oo = 2e18;

ll _fat[N + 1];
ll *fat = _fat + 1;
ll mul(ll a, ll b) {
	if(b and a > oo / b) return oo;
	return a * b;
}
ll add(ll a, ll b) {
	return a + b >= oo ? oo : a + b;
}

ll dp[N];
int ans[N];
int used[N];
int tt[N];

void solve(int n, ll k) {
	dp[n] = 1;
	for(int i = n - 1; i >= 0; i--) {
		dp[i] = 0;
		for(int j = i; j < n; j++) {
			dp[i] = add(dp[i], mul(fat[j - i - 1], dp[j + 1]));
		}
	}
	if(dp[0] < k) {
		printf("-1\n");
		return;
	}

	memset(used, 0, sizeof used);

	for(int i = 0; i < n; i++) {
		int j = i;
		while(1) {
			ll tmp = mul(fat[j - i - 1], dp[j + 1]);
			if(tmp >= k) break;
			else {
				k -= tmp;
				j++;
			}
		}
		ans[i] = j;
		used[ ans[i] ] = 1;
		if(i + 1 == j) {
			ans[i + 1] = i;
			used[ ans[i + 1] ] = 1;
		}
		else {
			ans[j] = i;
			used[i] = 1;
			for(int z = i + 1; z < j; z++) {
				int w = 0;
				while(1) {
					ll tmp = mul(fat[j - z - 1], dp[j + 1]);
					if(tmp >= k) break;
					else {
						k -= tmp;
						w++;
					}
				}
				for(int q = 0; q <= w; q++) w += used[q];
				ans[z] = w;
				used[ ans[z] ] = 1;
			}
			reverse(ans + i + 1, ans + j);
			for(int z = i + 1; z <= j; z++) tt[z] = ans[z];
			for(int z = i + 1, p = j; z <= j; z++) {
				ans[p] = tt[z];
				p = ans[p];
			}
		}
		i = j;
	}
	assert(k == 1);
	for(int i = 0; i < n; i++) printf("%d ", ans[i] + 1);
	printf("\n");
}

int main() {
	fat[-1] = 1;
	fat[0] = 1;
	for(int i = 1; i < N; i++) fat[i] = mul(i, fat[i - 1]);

	int t;

	scanf("%d", &t);

	while(t--) {
		int n; ll k;
		scanf("%d %lld", &n, &k);
		solve(n, k);
	}
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 252;
const ll oo = 2e18;

ll _fat[N + 5];
ll *fat = _fat + 5;
ll mul(ll a, ll b) {
	if(b and a > oo / b) return oo;
	return a * b;
}
ll add(ll a, ll b) {
	return a + b >= oo ? oo : a + b;
}

ll dp[N];
int ans[N];
int used[N];
int p[N];
int f(int u) {
	return u == p[u] ? u : p[u] = f(p[u]);
}

void solve(int n, ll k) {
	dp[n] = 1;
	for(int i = n - 1; i >= 0; i--) {
		dp[i] = 0;
		for(int j = i; j < n; j++) {
			dp[i] = add(dp[i], mul(fat[j - i - 1], dp[j + 1]));
		}
		p[i] = i;
	}
	if(dp[0] < k) {
		printf("-1\n");
		return;
	}

	memset(used, 0, sizeof used);

	for(int i = 0; i < n; i++) {
		int j = i;
		while(1) {
			ll tmp = mul(fat[j - i - 1], dp[j + 1]);
			if(tmp >= k) break;
			else {
				k -= tmp;
				j++;
			}
		}
		ans[i] = j;
		used[ ans[i] ] = 1;
		if(i + 1 == j) {
			ans[i + 1] = i;
			used[ ans[i + 1] ] = 1;
			p[f(i)] = f(i + 1);
		}
		else if(j > i + 1) {
			for(int z = i + 1; z < j; z++) {
				int w = 0;
				while(1) {
					ll tmp = mul(fat[j - z - 1], dp[j + 1]);
					if(tmp >= k) break;
					else {
						k -= tmp;
						w++;
					}
				}
				int tttt = used[z];
				used[z] = 1;
				for(int q = 0; q <= w; q++) w += used[q];
				while(f(z) == f(w)) {
					w++;
					for(int q = w; q <= w; q++) w += used[q];
				}
				used[z] = tttt;
				ans[z] = w;
				used[ ans[z] ] = 1;
				p[f(z)] = f(w);
			}
			int w = 0;
			while(used[w]) w++;
			ans[j] = w;
			used[w] = 1;
			p[f(j)] = f(w);
		}
		i = j;
	}
	assert(k == 1);
	for(int i = 0; i < n; i++) printf("%d ", ans[i] + 1);
	printf("\n");
}

int main() {
	fat[-2] = 1;
	fat[-1] = 1;
	fat[0] = 1;
	for(int i = 1; i < N; i++) fat[i] = mul(i, fat[i - 1]);

	int t;

	scanf("%d", &t);

	while(t--) {
		int n; ll k;
		scanf("%d %lld", &n, &k);
		solve(n, k);
	}
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 252;
const ll oo = 2e18;

ll _fat[N + 5];
ll *fat = _fat + 5;
ll mul(ll a, ll b) {
	if(b and a > oo / b) return oo;
	return a * b;
}
ll add(ll a, ll b) {
	return a + b >= oo ? oo : a + b;
}

ll dp[N];
int ans[N];
int used[N];
int p[N];
int f(int u) {
	return u == p[u] ? u : p[u] = f(p[u]);
}

void solve(int n, ll k) {
	dp[n] = 1;
	for(int i = n - 1; i >= 0; i--) {
		dp[i] = 0;
		for(int j = i; j < n; j++) {
			dp[i] = add(dp[i], mul(fat[j - i - 1], dp[j + 1]));
		}
		p[i] = i;
	}
	if(dp[0] < k) {
		printf("-1\n");
		return;
	}

	memset(used, 0, sizeof used);

	for(int i = 0; i < n; i++) {
		int j = i;
		while(1) {
			ll tmp = mul(fat[j - i - 1], dp[j + 1]);
			if(tmp >= k) break;
			else {
				k -= tmp;
				j++;
			}
		}
		ans[i] = j;
		used[ ans[i] ] = 1;
		if(i + 1 == j) {
			ans[i + 1] = i;
			used[ ans[i + 1] ] = 1;
			p[f(i)] = f(i + 1);
		}
		else if(j > i + 1) {
			for(int z = i + 1; z < j; z++) {
				int w = 0;
				while(1) {
					ll tmp = mul(fat[j - z - 1], dp[j + 1]);
					if(tmp >= k) break;
					else {
						k -= tmp;
						w++;
					}
				}
				for(int q = 0; q <= w; q++) {
					if(used[q] or f(z) == f(q)) {
						w++;
					}
				}
				ans[z] = w;
				used[ ans[z] ] = 1;
				p[f(z)] = f(w);
			}
			int w = 0;
			while(used[w]) w++;
			ans[j] = w;
			used[w] = 1;
			p[f(j)] = f(w);
		}
		i = j;
	}
	assert(k == 1);
	for(int i = 0; i < n; i++) printf("%d ", ans[i] + 1);
	printf("\n");
}

int main() {
	fat[-2] = 1;
	fat[-1] = 1;
	fat[0] = 1;
	for(int i = 1; i < N; i++) fat[i] = mul(i, fat[i - 1]);

	int t;

	scanf("%d", &t);

	while(t--) {
		int n; ll k;
		scanf("%d %lld", &n, &k);
		solve(n, k);
	}
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 252;
const ll oo = 2e18;

ll _fat[N + 5];
ll *fat = _fat + 5;
ll mul(ll a, ll b) {
	if(b and a > oo / b) return oo;
	return a * b;
}
ll add(ll a, ll b) {
	return a + b >= oo ? oo : a + b;
}

ll dp[N];
int ans[N];
int used[N];
int p[N];
int f(int u) {
	return u == p[u] ? u : p[u] = f(p[u]);
}

void solve(int n, ll k) {
	dp[n] = 1;
	for(int i = n - 1; i >= 0; i--) {
		dp[i] = 0;
		for(int j = i; j < n; j++) {
			dp[i] = add(dp[i], mul(fat[j - i - 1], dp[j + 1]));
		}
		p[i] = i;
	}
	if(dp[0] < k) {
		printf("-1\n");
		return;
	}

	memset(used, 0, sizeof used);

	for(int i = 0; i < n; i++) {
		int j = i;
		while(1) {
			ll tmp = mul(fat[j - i - 1], dp[j + 1]);
			if(tmp >= k) break;
			else {
				k -= tmp;
				j++;
			}
		}
		ans[i] = j;
		used[ ans[i] ] = 1;
		if(i + 1 == j) {
			ans[i + 1] = i;
			used[ ans[i + 1] ] = 1;
			p[f(i)] = f(i + 1);
		}
		else if(j > i + 1) {
			for(int z = i + 1; z < j; z++) {
				int w = 0;
				while(1) {
					ll tmp = mul(fat[j - z - 1], dp[j + 1]);
					if(tmp >= k) break;
					else {
						k -= tmp;
						w++;
					}
				}
				for(int q = 0; q <= w; q++) w += used[q];
				while(f(z) == f(w)) {
					w++;
					for(int q = w; q <= w; q++) w += used[q];
				}
				ans[z] = w;
				used[ ans[z] ] = 1;
				p[f(z)] = f(w);
			}
			int w = 0;
			while(used[w]) w++;
			ans[j] = w;
			used[w] = 1;
		}
		i = j;
	}
	assert(k == 1);
	for(int i = 0; i < n; i++) printf("%d ", ans[i] + 1);
	printf("\n");
}

int main() {
	fat[-2] = 1;
	fat[-1] = 1;
	fat[0] = 1;
	for(int i = 1; i < N; i++) fat[i] = mul(i, fat[i - 1]);

	int t;

	scanf("%d", &t);

	while(t--) {
		int n; ll k;
		scanf("%d %lld", &n, &k);
		solve(n, k);
	}
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 252;
const ll oo = 2e18;

ll _fat[N + 1];
ll *fat = _fat + 1;
ll mul(ll a, ll b) {
	if(b and a > oo / b) return oo;
	return a * b;
}
ll add(ll a, ll b) {
	return a + b >= oo ? oo : a + b;
}

ll dp[N];
int ans[N];
int used[N];
int tt[N];

void solve(int n, ll k) {
	dp[n] = 1;
	for(int i = n - 1; i >= 0; i--) {
		dp[i] = 0;
		for(int j = i; j < n; j++) {
			dp[i] = add(dp[i], mul(fat[j - i - 1], dp[j + 1]));
		}
	}
	if(dp[0] < k) {
		printf("-1\n");
		return;
	}

	memset(used, 0, sizeof used);

	for(int i = 0; i < n; i++) {
		int j = i;
		while(1) {
			ll tmp = mul(fat[j - i - 1], dp[j + 1]);
			if(tmp >= k) break;
			else {
				k -= tmp;
				j++;
			}
		}
		ans[i] = j;
		used[ ans[i] ] = 1;
		if(i + 1 == j) {
			ans[i + 1] = i;
			used[ ans[i + 1] ] = 1;
		}
		else if(j > i + 1) {
			ans[j] = i;
			used[i] = 1;
			for(int z = i + 1; z < j; z++) {
				int w = 0;
				while(1) {
					ll tmp = mul(fat[j - z - 1], dp[j + 1]);
					if(tmp >= k) break;
					else {
						k -= tmp;
						w++;
					}
				}
				for(int q = 0; q <= w; q++) w += used[q];
				ans[z] = w;
				used[ ans[z] ] = 1;
			}
			reverse(ans + i + 1, ans + j);
			for(int z = i + 1; z <= j; z++) tt[z] = ans[z];
			for(int z = i + 1, p = j; z <= j; z++) {
				ans[p] = tt[z];
				p = ans[p];
			}
		}
		i = j;
	}
	assert(k == 1);
	for(int i = 0; i < n; i++) printf("%d ", ans[i] + 1);
	printf("\n");
}

int main() {
	fat[-1] = 1;
	fat[0] = 1;
	for(int i = 1; i < N; i++) fat[i] = mul(i, fat[i - 1]);

	int t;

	scanf("%d", &t);

	while(t--) {
		int n; ll k;
		scanf("%d %lld", &n, &k);
		solve(n, k);
	}
}