// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
// #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define FAST std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DECIMAL(n)  std::cout << std::fixed;std::cout << std::setprecision(n);
#define hell 1000000007
#define narak 998244353
#define PI 3.14159265358979323844
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define endl "\n"
#define sqr(a) (a)*(a)
#define __lcm(m,n) m*(n/__gcd(m,n))
#define inputarr(a,n) for(int xxx=0;xxx<n;++xxx) cin>>(a)[xxx]
#define sz(a) (int)a.size()
#define sl(a) (int)a.length()
#define display(x) for(auto &a:x) cout<<a<<" ";cout<<endl
#define debug cerr<<"bhau"<<endl
#define between(n,a,b) (a<=n&&n<=b)
#define elasped_time 1.0 * clock() / CLOCKS_PER_SEC

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}

template<typename T, typename U> static inline void amin(T &x, U y) {
    if (y < x)
        x = y;
}

template<typename T, typename U> static inline void amax(T &x, U y) {
    if (x < y)
        x = y;
}

template<typename T,typename U> std::ostream& operator<<(std::ostream& out, std::pair<T,U> a) {
    out<<a.fi<<" "<<a.se;
    return out;
}

template<typename T,typename U> std::istream& operator>>(std::istream& in, std::pair<T,U> &a) {
    in>>a.fi>>a.se;
    return in;
}

inline long long toint(const std::string &s) {std::stringstream ss; ss << s; long long x; ss >> x; return x;}
inline std::string tostring(long long number) {std::stringstream ss; ss << number; return ss.str();}
inline std::string tobin(long long x) {return std::bitset<63>(x).to_string();}

std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());

#define ll long long
#define pii std::pair<int, int>
#define vi vector<int>
#define vvi vector<vi >
#define vii vector<pii >
#define point complex<ll>
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*------------------------------------------------*/

//primes for hashing 937,991,1013,1409,1741

using namespace std;
using namespace __gnu_pbds;
#define int long long
int inf=1ll*hell*hell;
int add(int x,int y){
	return min(inf,x+y);
}
int mul(int x,int y){
	if(x<inf/y)return x*y;
	else return inf;
}
void meowmeow321()
{
	int n,k;
	cin>>n>>k;
	int dp[n+1]={0};
	dp[0]=1;
	int fact[n+1];
	fact[0]=1;
	for (int i = 1; i <= n; ++i) {
		fact[i]=mul(fact[i-1],i);
	}
	for (int i = 0; i < n; ++i) {
		for (int j = 1; j <=n-i; ++j) {
			if(j==1){
				dp[i+1]=add(dp[i+1],dp[i]);
				continue;
			}
			dp[i+j]=add(dp[i+j],mul(dp[i],fact[j-2]));
		}
	}
	k--;
	if(k>=dp[n]){
		cout<<-1<<endl;return;
	}
	int ans[n];
	memset(ans,-1,sizeof(ans));
	bool used[n+1]={0};
	for (int i = 0; i < n;) {
		for (int j = 1; i+j <= n; ++j) {
			int skip=mul(dp[n-i-j],(j>1?fact[j-2]:1));
			if(skip<=k){
				k-=skip;
				continue;
			}
			ans[i]=i+j;
			used[i+j]=1;
			for (int kk = 1; kk < j; ++kk) {
				int rem=j-kk;
				for (int d = i+1; d <i+j; ++d) {
					if(!used[d]&&i+kk+1!=d){
						if(kk<j-1){
							int tmp=ans[d-1];
							while(tmp!=-1&&tmp!=kk+i+1){
								tmp=ans[tmp-1];
							}
							if(tmp==kk+i+1)continue;
						}
						int skip=mul(dp[n-i-j],(rem>1?fact[rem-2]:1));
						if(skip<=k){
							k-=skip;
							continue;
						}
						ans[i+kk]=d;
						used[d]=1;
						break;
					}
				}
			}
			i=i+j;
			break;
		}
	}
	display(ans);
}
signed main()
{
    FAST;
    int testcases=1;
    cin>>testcases;
    for(int i=0;i<testcases;++i)
    {
        meowmeow321();
    }
    cerr<<endl<<"Time Elasped : "<<elasped_time<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

void add(int64_t& a, int64_t b)
{
    if(a == -1 || b == -1 || b > numeric_limits<int64_t>::max() - a)
        a = -1;
    else
        a += b;
}

void mul(int64_t& a, int64_t b)
{
    if(a == -1 || b == -1 || b > numeric_limits<int64_t>::max() / a)
        a = -1;
    else
        a *= b;
}

bool inf(int64_t a, int64_t b)
{
    if(a == -1 && b == -1)
        exit(1);

    if(a == -1)
        return false;

    if(b == -1)
        return false;

    return a < b;
}

vector<pair<int64_t, vector<int64_t>>> num(51);

vector<int> concat(const vector<int>& a, const vector<int>& b)
{
    vector<int> res;
    res.reserve(a.size() + b.size());
    res.insert(res.end(), a.begin(), a.end());

    int n = a.size();
    for(int v : b)
        res.push_back(v + n);

    return res;
}

vector<int> findP(int n, int64_t k)
{
    if(inf(num[n].first, k))
        return {};

    if(k == 0)
    {
        vector<int> res;
        res.reserve(n);
        for(int i = 1; i <= n; ++i)
            res.push_back(i);

        return res;
    }

    for(int s = 1; s <= n; ++s)
    {
        if(inf(k, num[n].second[s]))
        {
            int base = num[n - s].first;

            vector<int> front;
            if(k < base)
            {
                front.reserve(s);
                front.push_back(s);
                for(int i = 1; i < s; ++i)
                    front.push_back(i);
            }
            else
            {
                front = findP(s, k / base);
            }

            if(s == n)
            {
                return front;
            }

            return concat(front, findP(n - s, k % base));
        }
        else
        {
            k -= num[n].second[s];
        }
    }

    exit(1);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    vector<int64_t> fac(51);
    fac[0] = 1;
    for(int i = 1; i <= 50; ++i)
    {
        fac[i] = fac[i - 1];
        mul(fac[i], i);
    }

    num[0].first = 1;
    for(int n = 1; n <= 50; ++n)
    {
        auto& vec = num[n].second;
        vec.resize(n + 1);
        for(int s = 1; s <= n; ++s)
        {
            vec[s] = fac[max(0, s - 2)];
            mul(vec[s], num[n - s].first);
            add(num[n].first, vec[s]);
        }
    }

    int T;
    cin >> T;
    cin.ignore();
    for(int t = 0; t < T; ++t)
    {
        int n;
        int64_t k;
        cin >> n >> k;
        cin.ignore();

        auto ans = findP(n, k - 1);
        if(ans.empty())
            cout << "-1" << endl;
        else
        {
            for(auto v : ans)
                cout << v << ' ';
            cout << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

void add(int64_t& a, int64_t b)
{
    if(a == -1 || b == -1 || b > numeric_limits<int64_t>::max() - a)
        a = -1;
    else
        a += b;
}

void mul(int64_t& a, int64_t b)
{
    if(a == -1 || b == -1 || b > numeric_limits<int64_t>::max() / a)
        a = -1;
    else
        a *= b;
}

bool inf(int64_t a, int64_t b)
{
    if(a == -1 && b == -1)
        exit(1);

    if(a == -1)
        return false;

    if(b == -1)
        return false;

    return a < b;
}

class UnionFind
{
public:
    explicit UnionFind(int size) : m_size(size, 1), m_subgroups(size)
    {
        m_parent.reserve(size);
        for(int i = 0; i < size; ++i)
            m_parent.push_back(i);
    }

    int subgroups() const
    {
        return m_subgroups;
    }

    int groupSize(int i)
    {
        return m_size[root(i)];
    }

    int root(int i)
    {
        return (m_parent[i] == i) ? i : (m_parent[i] = root(m_parent[i]));
    }

    bool connected(int i, int j)
    {
        return root(i) == root(j);
    }

    void connect(int i, int j)
    {
        int root1 = root(i);
        int root2 = root(j);
        if(root1 != root2)
        {
            if(m_size[root1] < m_size[root2])
            {
                m_parent[root1] = root2;
                m_size[root2] += m_size[root1];
            }
            else
            {
                m_parent[root2] = root1;
                m_size[root1] += m_size[root2];
            }
            --m_subgroups;
        }
    }

private:
    vector<int> m_parent, m_size;
    int m_subgroups;
};

vector<pair<int64_t, vector<int64_t>>> num(51);
vector<int64_t> fac(51);

vector<int> getFront(int n, int64_t k)
{
    vector<int> res(n, -1);
    res[0] = n;

    set<int> left;
    for(int i = 1; i < n; ++i)
        left.emplace_hint(left.end(), i);

    UnionFind uf(n);
    for(int index = 2; index < n; ++index)
    {
        for(auto it = left.begin(); it != left.end(); ++it)
        {
            if(*it != index && !uf.connected(index - 1, *it - 1))
            {
                auto size = fac[n - index - 1];
                if(inf(k, size))
                {
                    res[index - 1] = *it;
                    uf.connect(index - 1, *it - 1);
                    left.erase(it);
                    break;
                }
                else
                {
                    k -= size;
                }
            }
        }
    }
    if(!left.empty())
        res[n - 1] = *left.begin();

    return res;
}

vector<int> concat(const vector<int>& a, const vector<int>& b)
{
    vector<int> res;
    res.reserve(a.size() + b.size());
    res.insert(res.end(), a.begin(), a.end());

    int n = a.size();
    for(int v : b)
        res.push_back(v + n);

    return res;
}

vector<int> findP(int n, int64_t k)
{
    if(inf(num[n].first, k) || num[n].first == k)
        return {};

    if(k == 0)
    {
        vector<int> res;
        res.reserve(n);
        for(int i = 1; i <= n; ++i)
            res.push_back(i);

        return res;
    }

    for(int s = 1; s <= n; ++s)
    {
        if(inf(k, num[n].second[s]))
        {
            int base = num[n - s].first;

            auto front = getFront(s, k / base);
            if(s == n)
                return front;

            return concat(front, findP(n - s, k % base));
        }
        else
        {
            k -= num[n].second[s];
        }
    }

    exit(2);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    fac[0] = 1;
    for(int i = 1; i <= 50; ++i)
    {
        fac[i] = fac[i - 1];
        mul(fac[i], i);
    }

    num[0].first = 1;
    for(int n = 1; n <= 50; ++n)
    {
        auto& vec = num[n].second;
        vec.resize(n + 1);
        for(int s = 1; s <= n; ++s)
        {
            vec[s] = fac[max(0, s - 2)];
            mul(vec[s], num[n - s].first);
            add(num[n].first, vec[s]);
        }
    }

    int T;
    cin >> T;
    cin.ignore();
    for(int t = 0; t < T; ++t)
    {
        int n;
        int64_t k;
        cin >> n >> k;
        cin.ignore();

        auto ans = findP(n, k - 1);
        if(ans.empty())
            cout << "-1" << endl;
        else
        {
            for(auto v : ans)
                cout << v << ' ';
            cout << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

void add(int64_t& a, int64_t b)
{
    if(a == -1 || b == -1 || b > numeric_limits<int64_t>::max() - a)
        a = -1;
    else
        a += b;
}

void mul(int64_t& a, int64_t b)
{
    if(a == -1 || b == -1 || b > numeric_limits<int64_t>::max() / a)
        a = -1;
    else
        a *= b;
}

bool inf(int64_t a, int64_t b)
{
    if(a == -1 && b == -1)
        exit(1);

    if(a == -1)
        return false;

    if(b == -1)
        return false;

    return a < b;
}

vector<pair<int64_t, vector<int64_t>>> num(51);
vector<int64_t> fac(51);

vector<int> getFront(int n, int64_t k)
{
    vector<int> res(n, -1);
    res[0] = n;

    set<int> left;
    for(int i = 1; i < n; ++i)
        left.emplace_hint(left.end(), i);

    for(int index = 2; index < n; ++index)
    {
        for(auto it = left.begin(); it != left.end(); ++it)
        {
            if(*it != index)
            {
                auto size = fac[n - index - 1];
                if(k < size)
                {
                    res[index - 1] = *it;
                    left.erase(it);
                    break;
                }
                else
                {
                    k -= size;
                }
            }
        }
    }
    if(!left.empty())
        res[n - 1] = *left.begin();

    return res;
}

vector<int> concat(const vector<int>& a, const vector<int>& b)
{
    vector<int> res;
    res.reserve(a.size() + b.size());
    res.insert(res.end(), a.begin(), a.end());

    int n = a.size();
    for(int v : b)
        res.push_back(v + n);

    return res;
}

vector<int> findP(int n, int64_t k)
{
    if(inf(num[n].first, k) || num[n].first == k)
        return {};

    if(k == 0)
    {
        vector<int> res;
        res.reserve(n);
        for(int i = 1; i <= n; ++i)
            res.push_back(i);

        return res;
    }

    for(int s = 1; s <= n; ++s)
    {
        if(inf(k, num[n].second[s]))
        {
            int base = num[n - s].first;

            auto front = getFront(s, k / s);
            if(s == n)
                return front;

            return concat(front, findP(n - s, k % base));
        }
        else
        {
            k -= num[n].second[s];
        }
    }

    exit(2);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    fac[0] = 1;
    for(int i = 1; i <= 50; ++i)
    {
        fac[i] = fac[i - 1];
        mul(fac[i], i);
    }

    num[0].first = 1;
    for(int n = 1; n <= 50; ++n)
    {
        auto& vec = num[n].second;
        vec.resize(n + 1);
        for(int s = 1; s <= n; ++s)
        {
            vec[s] = fac[max(0, s - 2)];
            mul(vec[s], num[n - s].first);
            add(num[n].first, vec[s]);
        }
    }

    int T;
    cin >> T;
    cin.ignore();
    for(int t = 0; t < T; ++t)
    {
        int n;
        int64_t k;
        cin >> n >> k;
        cin.ignore();

        auto ans = findP(n, k - 1);
        if(ans.empty())
            cout << "-1" << endl;
        else
        {
            for(auto v : ans)
                cout << v << ' ';
            cout << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

void add(int64_t& a, int64_t b)
{
    if(a == -1 || b == -1 || b > numeric_limits<int64_t>::max() - a)
        a = -1;
    else
        a += b;
}

void mul(int64_t& a, int64_t b)
{
    if(a == -1 || b == -1 || b > numeric_limits<int64_t>::max() / a)
        a = -1;
    else
        a *= b;
}

bool inf(int64_t a, int64_t b)
{
    if(a == -1 && b == -1)
        exit(1);

    if(a == -1)
        return false;

    if(b == -1)
        return false;

    return a < b;
}

vector<pair<int64_t, vector<int64_t>>> num(51);
vector<int64_t> fac(51);

vector<int> getFront(int n, int64_t k)
{
    vector<int> res(n, -1);
    res[0] = n;

    set<int> left;
    for(int i = 1; i < n; ++i)
        left.emplace_hint(left.end(), i);

    for(int index = 2; index < n; ++index)
    {
        for(auto it = left.begin(); it != left.end(); ++it)
        {
            if(*it != index)
            {
                auto size = fac[n - index - 1];
                if(k < size)
                {
                    res[index - 1] = *it;
                    left.erase(it);
                    break;
                }
                else
                {
                    k -= size;
                }
            }
        }
    }
    if(!left.empty())
        res[n - 1] = *left.begin();

    return res;
}

vector<int> concat(const vector<int>& a, const vector<int>& b)
{
    vector<int> res;
    res.reserve(a.size() + b.size());
    res.insert(res.end(), a.begin(), a.end());

    int n = a.size();
    for(int v : b)
        res.push_back(v + n);

    return res;
}

vector<int> findP(int n, int64_t k)
{
    if(inf(num[n].first, k))
        return {};

    if(k == 0)
    {
        vector<int> res;
        res.reserve(n);
        for(int i = 1; i <= n; ++i)
            res.push_back(i);

        return res;
    }

    for(int s = 1; s <= n; ++s)
    {
        if(inf(k, num[n].second[s]))
        {
            int base = num[n - s].first;

            auto front = getFront(s, k / s);
            if(s == n)
                return front;

            return concat(front, findP(n - s, k % base));
        }
        else
        {
            k -= num[n].second[s];
        }
    }

    exit(2);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    fac[0] = 1;
    for(int i = 1; i <= 50; ++i)
    {
        fac[i] = fac[i - 1];
        mul(fac[i], i);
    }

    num[0].first = 1;
    for(int n = 1; n <= 50; ++n)
    {
        auto& vec = num[n].second;
        vec.resize(n + 1);
        for(int s = 1; s <= n; ++s)
        {
            vec[s] = fac[max(0, s - 2)];
            mul(vec[s], num[n - s].first);
            add(num[n].first, vec[s]);
        }
    }

    int T;
    cin >> T;
    cin.ignore();
    for(int t = 0; t < T; ++t)
    {
        int n;
        int64_t k;
        cin >> n >> k;
        cin.ignore();

        auto ans = findP(n, k - 1);
        if(ans.empty())
            cout << "-1" << endl;
        else
        {
            for(auto v : ans)
                cout << v << ' ';
            cout << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

void add(int64_t& a, int64_t b)
{
    if(a == -1 || b == -1 || b > numeric_limits<int64_t>::max() - a)
        a = -1;
    else
        a += b;
}

void mul(int64_t& a, int64_t b)
{
    if(a == -1 || b == -1 || b > numeric_limits<int64_t>::max() / a)
        a = -1;
    else
        a *= b;
}

bool inf(int64_t a, int64_t b)
{
    if(a == -1 && b == -1)
        exit(1);

    if(a == -1)
        return false;

    if(b == -1)
        return false;

    return a < b;
}

vector<pair<int64_t, vector<int64_t>>> num(51);

vector<int> concat(const vector<int>& a, const vector<int>& b)
{
    vector<int> res;
    res.reserve(a.size() + b.size());
    res.insert(res.end(), a.begin(), a.end());

    int n = a.size();
    for(int v : b)
        res.push_back(v + n);

    return res;
}

vector<int> findP(int n, int64_t k)
{
    if(inf(num[n].first, k))
        return {};

    if(k == 0)
    {
        vector<int> res;
        res.reserve(n);
        for(int i = 1; i <= n; ++i)
            res.push_back(i);

        return res;
    }

    for(int s = 1; s <= n; ++s)
    {
        if(inf(k, num[n].second[s]))
        {
            int base = num[n - s].first;

            vector<int> front;
            if(k < base)
            {
                front.reserve(s);
                front.push_back(s);
                for(int i = 1; i < s; ++i)
                    front.push_back(i);
            }
            else
            {
                front = findP(s, k / base);
            }

            if(s == n)
            {
                return front;
            }

            return concat(front, findP(n - s, k % base));
        }
        else
        {
            k -= num[n].second[s];
        }
    }

    exit(2);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    vector<int64_t> fac(51);
    fac[0] = 1;
    for(int i = 1; i <= 50; ++i)
    {
        fac[i] = fac[i - 1];
        mul(fac[i], i);
    }

    num[0].first = 1;
    for(int n = 1; n <= 50; ++n)
    {
        auto& vec = num[n].second;
        vec.resize(n + 1);
        for(int s = 1; s <= n; ++s)
        {
            vec[s] = fac[max(0, s - 2)];
            mul(vec[s], num[n - s].first);
            add(num[n].first, vec[s]);
        }
    }

    int T;
    cin >> T;
    cin.ignore();
    for(int t = 0; t < T; ++t)
    {
        int n;
        int64_t k;
        cin >> n >> k;
        cin.ignore();

        auto ans = findP(n, k - 1);
        if(ans.empty())
            cout << "-1" << endl;
        else
        {
            for(auto v : ans)
                cout << v << ' ';
            cout << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

void add(int64_t& a, int64_t b)
{
    if(a == -1 || b == -1 || b > numeric_limits<int64_t>::max() - a)
        a = -1;
    else
        a += b;
}

void mul(int64_t& a, int64_t b)
{
    if(a == -1 || b == -1 || b > numeric_limits<int64_t>::max() / a)
        a = -1;
    else
        a *= b;
}

bool inf(int64_t a, int64_t b)
{
    if(a == -1 && b == -1)
        exit(1);

    if(a == -1)
        return false;

    if(b == -1)
        return false;

    return a < b;
}

class UnionFind
{
public:
    explicit UnionFind(int size) : m_size(size, 1), m_subgroups(size)
    {
        m_parent.reserve(size);
        for(int i = 0; i < size; ++i)
            m_parent.push_back(i);
    }

    int subgroups() const
    {
        return m_subgroups;
    }

    int groupSize(int i)
    {
        return m_size[root(i)];
    }

    int root(int i)
    {
        return (m_parent[i] == i) ? i : (m_parent[i] = root(m_parent[i]));
    }

    bool connected(int i, int j)
    {
        return root(i) == root(j);
    }

    void connect(int i, int j)
    {
        int root1 = root(i);
        int root2 = root(j);
        if(root1 != root2)
        {
            if(m_size[root1] < m_size[root2])
            {
                m_parent[root1] = root2;
                m_size[root2] += m_size[root1];
            }
            else
            {
                m_parent[root2] = root1;
                m_size[root1] += m_size[root2];
            }
            --m_subgroups;
        }
    }

private:
    vector<int> m_parent, m_size;
    int m_subgroups;
};

vector<pair<int64_t, vector<int64_t>>> num(51);
vector<int64_t> fac(51);

vector<int> getFront(int n, int64_t k)
{
    vector<int> res(n, -1);
    res[0] = n;

    set<int> left;
    for(int i = 1; i < n; ++i)
        left.emplace_hint(left.end(), i);

    UnionFind uf(n);
    for(int index = 2; index < n; ++index)
    {
        for(auto it = left.begin(); it != left.end(); ++it)
        {
            if(*it != index && !uf.connected(index - 1, *it - 1))
            {
                auto size = fac[n - index - 1];
                if(k < size)
                {
                    res[index - 1] = *it;
                    uf.connect(index - 1, *it - 1);
                    left.erase(it);
                    break;
                }
                else
                {
                    k -= size;
                }
            }
        }
    }
    if(!left.empty())
        res[n - 1] = *left.begin();

    return res;
}

vector<int> concat(const vector<int>& a, const vector<int>& b)
{
    vector<int> res;
    res.reserve(a.size() + b.size());
    res.insert(res.end(), a.begin(), a.end());

    int n = a.size();
    for(int v : b)
        res.push_back(v + n);

    return res;
}

vector<int> findP(int n, int64_t k)
{
    if(inf(num[n].first, k) || num[n].first == k)
        return {};

    if(k == 0)
    {
        vector<int> res;
        res.reserve(n);
        for(int i = 1; i <= n; ++i)
            res.push_back(i);

        return res;
    }

    for(int s = 1; s <= n; ++s)
    {
        if(inf(k, num[n].second[s]))
        {
            int base = num[n - s].first;

            auto front = getFront(s, k / base);
            if(s == n)
                return front;

            return concat(front, findP(n - s, k % base));
        }
        else
        {
            k -= num[n].second[s];
        }
    }

    exit(2);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    fac[0] = 1;
    for(int i = 1; i <= 50; ++i)
    {
        fac[i] = fac[i - 1];
        mul(fac[i], i);
    }

    num[0].first = 1;
    for(int n = 1; n <= 50; ++n)
    {
        auto& vec = num[n].second;
        vec.resize(n + 1);
        for(int s = 1; s <= n; ++s)
        {
            vec[s] = fac[max(0, s - 2)];
            mul(vec[s], num[n - s].first);
            add(num[n].first, vec[s]);
        }
    }

    int T;
    cin >> T;
    cin.ignore();
    for(int t = 0; t < T; ++t)
    {
        int n;
        int64_t k;
        cin >> n >> k;
        cin.ignore();

        auto ans = findP(n, k - 1);
        if(ans.empty())
            cout << "-1" << endl;
        else
        {
            for(auto v : ans)
                cout << v << ' ';
            cout << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

void add(int64_t& a, int64_t b)
{
    if(a == -1 || b == -1 || b > numeric_limits<int64_t>::max() - a)
        a = -1;
    else
        a += b;
}

void mul(int64_t& a, int64_t b)
{
    if(a == -1 || b == -1 || b > numeric_limits<int64_t>::max() / a)
        a = -1;
    else
        a *= b;
}

bool inf(int64_t a, int64_t b)
{
    if(a == -1 && b == -1)
        exit(1);

    if(a == -1)
        return false;

    if(b == -1)
        return false;

    return a < b;
}

class UnionFind
{
public:
    explicit UnionFind(int size) : m_size(size, 1), m_subgroups(size)
    {
        m_parent.reserve(size);
        for(int i = 0; i < size; ++i)
            m_parent.push_back(i);
    }

    int subgroups() const
    {
        return m_subgroups;
    }

    int groupSize(int i)
    {
        return m_size[root(i)];
    }

    int root(int i)
    {
        return (m_parent[i] == i) ? i : (m_parent[i] = root(m_parent[i]));
    }

    bool connected(int i, int j)
    {
        return root(i) == root(j);
    }

    void connect(int i, int j)
    {
        int root1 = root(i);
        int root2 = root(j);
        if(root1 != root2)
        {
            if(m_size[root1] < m_size[root2])
            {
                m_parent[root1] = root2;
                m_size[root2] += m_size[root1];
            }
            else
            {
                m_parent[root2] = root1;
                m_size[root1] += m_size[root2];
            }
            --m_subgroups;
        }
    }

private:
    vector<int> m_parent, m_size;
    int m_subgroups;
};

vector<pair<int64_t, vector<int64_t>>> num(51);
vector<int64_t> fac(51);

vector<int> getFront(int n, int64_t k)
{
    vector<int> res(n, -1);
    res[0] = n;

    set<int> left;
    for(int i = 1; i < n; ++i)
        left.emplace_hint(left.end(), i);

    UnionFind uf(n);
    for(int index = 2; index < n; ++index)
    {
        for(auto it = left.begin(); it != left.end(); ++it)
        {
            if(*it != index && !uf.connected(index - 1, *it - 1))
            {
                auto size = fac[n - index - 1];

                if(inf(k, size))
                {
                    res[index - 1] = *it;
                    uf.connect(index - 1, *it - 1);
                    if(!left.count(index))
                    {
                        for(int i = 1; i < index; ++i)
                        {
                            if(res[i - 1] == index)
                            {
                                uf.connect(i - 1, index - 1);
                                break;
                            }
                        }
                    }
                    left.erase(it);
                    break;
                }
                else
                {
                    k -= size;
                }
            }
        }
    }
    if(!left.empty())
        res[n - 1] = *left.begin();

    return res;
}

vector<int> concat(const vector<int>& a, const vector<int>& b)
{
    vector<int> res;
    res.reserve(a.size() + b.size());
    res.insert(res.end(), a.begin(), a.end());

    int n = a.size();
    for(int v : b)
        res.push_back(v + n);

    return res;
}

vector<int> findP(int n, int64_t k)
{
    if(inf(num[n].first, k) || num[n].first == k)
        return {};

    if(k == 0)
    {
        vector<int> res;
        res.reserve(n);
        for(int i = 1; i <= n; ++i)
            res.push_back(i);

        return res;
    }

    for(int s = 1; s <= n; ++s)
    {
        if(inf(k, num[n].second[s]))
        {
            int base = num[n - s].first;

            auto front = getFront(s, k / base);
            if(s == n)
                return front;

            return concat(front, findP(n - s, k % base));
        }
        else
        {
            k -= num[n].second[s];
        }
    }

    exit(2);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    fac[0] = 1;
    for(int i = 1; i <= 50; ++i)
    {
        fac[i] = fac[i - 1];
        mul(fac[i], i);
    }

    num[0].first = 1;
    for(int n = 1; n <= 50; ++n)
    {
        auto& vec = num[n].second;
        vec.resize(n + 1);
        for(int s = 1; s <= n; ++s)
        {
            vec[s] = fac[max(0, s - 2)];
            mul(vec[s], num[n - s].first);
            add(num[n].first, vec[s]);
        }
    }

    int T;
    cin >> T;
    cin.ignore();
    for(int t = 0; t < T; ++t)
    {
        int n;
        int64_t k;
        cin >> n >> k;
        cin.ignore();

        auto ans = findP(n, k - 1);
        if(ans.empty())
            cout << "-1" << endl;
        else
        {
            for(auto v : ans)
                cout << v << ' ';
            cout << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

void add(int64_t& a, int64_t b)
{
    if(a == -1 || b == -1 || b > numeric_limits<int64_t>::max() - a)
        a = -1;
    else
        a += b;
}

void mul(int64_t& a, int64_t b)
{
    if(a == -1 || b == -1 || b > numeric_limits<int64_t>::max() / a)
        a = -1;
    else
        a *= b;
}

bool inf(int64_t a, int64_t b)
{
    if(a == -1 && b == -1)
        exit(1);

    if(a == -1)
        return false;

    if(b == -1)
        return false;

    return a < b;
}

vector<pair<int64_t, vector<int64_t>>> num(51);
vector<int64_t> fac(51);

vector<int> getFront(int n, int64_t k)
{
    vector<int> res(n, -1);
    res[0] = n;

    set<int> left;
    for(int i = 1; i < n; ++i)
        left.emplace_hint(left.end(), i);

    for(int index = 2; index < n; ++index)
    {
        for(auto it = left.begin(); it != left.end(); ++it)
        {
            if(*it != index)
            {
                auto size = fac[n - index - 1];
                if(k < size)
                {
                    res[index - 1] = *it;
                    left.erase(it);
                    break;
                }
                else
                {
                    k -= size;
                }
            }
        }
    }
    if(!left.empty())
        res[n - 1] = *left.begin();

    return res;
}

vector<int> concat(const vector<int>& a, const vector<int>& b)
{
    vector<int> res;
    res.reserve(a.size() + b.size());
    res.insert(res.end(), a.begin(), a.end());

    int n = a.size();
    for(int v : b)
        res.push_back(v + n);

    return res;
}

vector<int> findP(int n, int64_t k)
{
    if(inf(num[n].first, k) || num[n].first == k)
        return {};

    if(k == 0)
    {
        vector<int> res;
        res.reserve(n);
        for(int i = 1; i <= n; ++i)
            res.push_back(i);

        return res;
    }

    for(int s = 1; s <= n; ++s)
    {
        if(inf(k, num[n].second[s]))
        {
            int base = num[n - s].first;

            auto front = getFront(s, k / base);
            if(s == n)
                return front;

            return concat(front, findP(n - s, k % base));
        }
        else
        {
            k -= num[n].second[s];
        }
    }

    exit(2);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    fac[0] = 1;
    for(int i = 1; i <= 50; ++i)
    {
        fac[i] = fac[i - 1];
        mul(fac[i], i);
    }

    num[0].first = 1;
    for(int n = 1; n <= 50; ++n)
    {
        auto& vec = num[n].second;
        vec.resize(n + 1);
        for(int s = 1; s <= n; ++s)
        {
            vec[s] = fac[max(0, s - 2)];
            mul(vec[s], num[n - s].first);
            add(num[n].first, vec[s]);
        }
    }

    int T;
    cin >> T;
    cin.ignore();
    for(int t = 0; t < T; ++t)
    {
        int n;
        int64_t k;
        cin >> n >> k;
        cin.ignore();

        auto ans = findP(n, k - 1);
        if(ans.empty())
            cout << "-1" << endl;
        else
        {
            for(auto v : ans)
                cout << v << ' ';
            cout << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

constexpr int64_t INF = numeric_limits<int64_t>::max();

void add(int64_t& a, int64_t b)
{
    if(a == INF || b == INF || b > INF - a)
        a = INF;
    else
        a += b;
}

void mul(int64_t& a, int64_t b)
{
    if(a == INF || b == INF || b > INF / a)
        a = INF;
    else
        a *= b;
}

bool inf(int64_t a, int64_t b)
{
    if(a == INF && b == INF)
        exit(1);

    if(a == INF)
        return false;

    if(b == INF)
        return true;

    return a < b;
}

class UnionFind
{
public:
    explicit UnionFind(int size) : m_size(size, 1), m_subgroups(size)
    {
        m_parent.reserve(size);
        for(int i = 0; i < size; ++i)
            m_parent.push_back(i);
    }

    int subgroups() const
    {
        return m_subgroups;
    }

    int groupSize(int i)
    {
        return m_size[root(i)];
    }

    int root(int i)
    {
        return (m_parent[i] == i) ? i : (m_parent[i] = root(m_parent[i]));
    }

    bool connected(int i, int j)
    {
        return root(i) == root(j);
    }

    void connect(int i, int j)
    {
        int root1 = root(i);
        int root2 = root(j);
        if(root1 != root2)
        {
            if(m_size[root1] < m_size[root2])
            {
                m_parent[root1] = root2;
                m_size[root2] += m_size[root1];
            }
            else
            {
                m_parent[root2] = root1;
                m_size[root1] += m_size[root2];
            }
            --m_subgroups;
        }
    }

private:
    vector<int> m_parent, m_size;
    int m_subgroups;
};

vector<pair<int64_t, vector<int64_t>>> num(51);
vector<int64_t> fac(51);

vector<int> getFront(int n, int64_t k)
{
    vector<int> res(n, -1);
    res[0] = n;

    set<int> left;
    for(int i = 1; i < n; ++i)
        left.emplace_hint(left.end(), i);

    UnionFind uf(n);
    for(int index = 2; index < n; ++index)
    {
        for(auto it = left.begin(); it != left.end(); ++it)
        {
            if(*it != index && !uf.connected(index - 1, *it - 1))
            {
                auto size = fac[n - index - 1];

                if(inf(k, size))
                {
                    res[index - 1] = *it;
                    uf.connect(index - 1, *it - 1);
                    if(!left.count(index))
                    {
                        for(int i = 1; i < index; ++i)
                        {
                            if(res[i - 1] == index)
                            {
                                uf.connect(i - 1, index - 1);
                                break;
                            }
                        }
                    }
                    left.erase(it);
                    break;
                }
                else
                {
                    k -= size;
                }
            }
        }
    }
    if(!left.empty())
        res[n - 1] = *left.begin();

    return res;
}

vector<int> concat(const vector<int>& a, const vector<int>& b)
{
    vector<int> res;
    res.reserve(a.size() + b.size());
    res.insert(res.end(), a.begin(), a.end());

    int n = a.size();
    for(int v : b)
        res.push_back(v + n);

    return res;
}

vector<int> findP(int n, int64_t k)
{
    if(inf(num[n].first, k) || num[n].first == k)
        return {};

    if(k == 0)
    {
        vector<int> res;
        res.reserve(n);
        for(int i = 1; i <= n; ++i)
            res.push_back(i);

        return res;
    }

    for(int s = 1; s <= n; ++s)
    {
        if(inf(k, num[n].second[s]))
        {
            auto base = num[n - s].first;
            auto front = getFront(s, k / base);
            if(s == n)
                return front;

            return concat(front, findP(n - s, k % base));
        }
        else
        {
            k -= num[n].second[s];
        }
    }

    exit(2);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    fac[0] = 1;
    for(int i = 1; i <= 50; ++i)
    {
        fac[i] = fac[i - 1];
        mul(fac[i], i);
    }

    num[0].first = 1;
    for(int n = 1; n <= 50; ++n)
    {
        auto& vec = num[n].second;
        vec.resize(n + 1);
        for(int s = 1; s <= n; ++s)
        {
            vec[s] = fac[max(0, s - 2)];
            mul(vec[s], num[n - s].first);
            add(num[n].first, vec[s]);
        }
    }

    int T;
    cin >> T;
    cin.ignore();
    for(int t = 0; t < T; ++t)
    {
        int n;
        int64_t k;
        cin >> n >> k;
        cin.ignore();

        auto ans = findP(n, k - 1);
        if(ans.empty())
            cout << "-1" << endl;
        else
        {
            for(auto v : ans)
                cout << v << ' ';
            cout << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

#define all(v) (v).begin(), (v).end()
#define sortv(v) sort(all(v))
#define uniqv(v) (v).erase(unique(all(v)), (v).end())
#define pb push_back
#define FI first
#define SE second
#define lb lower_bound
#define ub upper_bound
#define mp make_pair
#define test 1
#define TEST if(test)

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

const int MOD = 1000000007; // 998244353
const int INF = 2e9;
const ll INFLL = 1e18;
const int MAX_N = 60;

int T;
int N;
ll K;
ll per[MAX_N+1];
ll dp[MAX_N+1];

ll multi(ll A, ll B){
	if(A==0 || B==0)	return 0;
	if(INFLL/A<B){
		return INFLL;
	}return A*B;
}
ll add(ll A, ll B){
	if(A>=INFLL || B>=INFLL)	return INFLL;
	return min(A+B, INFLL);
}

vector<int> ans;

bool chk[MAX_N+1];
int sel[MAX_N+1];

int main(){
	per[0] = per[1] = 1;
	for(int i=2; i<=MAX_N; i++){
		per[i] = multi(per[i-1], (ll)i);
	}
	scanf("%d", &T);
	while(T--){
		scanf("%d%lld", &N, &K);
		for(int i=1; i<=N; i++)	chk[i] = sel[i] = 0;
		dp[N+1] = dp[N] = 1;
		for(int i=N-1; i>=1; i--){
			dp[i] = dp[i+1];
			for(int j=1; i+j<=N; j++){
				dp[i] = add(dp[i], multi(dp[i+j+1], per[j-1]));
			}
			//cout<<i<<" "<<dp[i]<<endl;
		}
		if(K>dp[1]){
			printf("-1\n");
			continue;
		}
		for(int i=1; i<=N; i++){
			if(K<=dp[i+1]){
				ans.pb(i);
				continue;
			}
			K -= dp[i+1];
			for(int j=1; j+i<=N; j++){
				if(multi(dp[i+j+1], per[j-1])>=K){
					ans.pb(i+j);
					chk[i+j] = true;
					sel[i+j] = i;
					for(int k=i+1; k<i+j; k++){
						int tmp = k;
						while(1){
							chk[tmp] = true;
							if(sel[tmp]==0)	break;
							tmp = sel[tmp];
						}
						for(int t=i; t<i+j; t++){
							if(chk[t])	continue;
							if(k!=i+j && multi(dp[i+j+1], per[i+j-1-k])<K){
								K -= multi(dp[i+j+1], per[i+j-1-k]);
							}else{
								sel[t] = k;
								chk[t] = true;
								ans.pb(t);
								break;
							}
						}
						tmp = k;
						while(1){
							chk[tmp] = false;
							if(sel[tmp]==0)	break;
							tmp = sel[tmp];
						}
					}
					for(int t=i; t<i+j; t++){
						if(chk[t])	continue;
						chk[t] = true;
						ans.pb(t);
						break;
					}
					i = i+j;
					break;
				}else{
					K -= multi(dp[i+j+1], per[j]);
				}
			}
		}
		for(int i=0; i<ans.size(); i++){
			printf("%d ", ans[i]);
		}
		printf("\n");
		while(!ans.empty())	ans.pop_back();
	}
	return 0;
}


#include <bits/stdc++.h>

#define all(v) (v).begin(), (v).end()
#define sortv(v) sort(all(v))
#define uniqv(v) (v).erase(unique(all(v)), (v).end())
#define pb push_back
#define FI first
#define SE second
#define lb lower_bound
#define ub upper_bound
#define mp make_pair
#define test 1
#define TEST if(test)

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

const int MOD = 1000000007; // 998244353
const int INF = 2e9;
const ll INFLL = 1e18;
const int MAX_N = 60;

int T;
int N;
ll K;
ll per[MAX_N+1];
ll dp[MAX_N+1];

ll multi(ll A, ll B){
	if(A==0 || B==0)	return 0;
	if(INFLL/A<B){
		return INFLL;
	}return A*B;
}
ll add(ll A, ll B){
	if(A>=INFLL || B>=INFLL)	return INFLL;
	return min(A+B, INFLL);
}

vector<int> ans;

bool chk[MAX_N+1];
int sel[MAX_N+1];

int main(){
	per[0] = per[1] = 1;
	for(int i=2; i<=MAX_N; i++){
		per[i] = multi(per[i-1], (ll)i);
	}
	scanf("%d", &T);
	while(T--){
		scanf("%d%lld", &N, &K);
		for(int i=1; i<=N; i++)	chk[i] = sel[i] = 0;
		dp[N+1] = dp[N] = 1;
		for(int i=N-1; i>=1; i--){
			dp[i] = dp[i+1];
			for(int j=1; i+j<=N; j++){
				dp[i] = add(dp[i], multi(dp[i+j+1], per[j-1]));
			}
			//cout<<i<<" "<<dp[i]<<endl;
		}
		if(K>dp[1]){
			printf("-1\n");
			continue;
		}
		for(int i=1; i<=N; i++){
			if(K<=dp[i+1]){
				ans.pb(i);
				continue;
			}
			K -= dp[i+1];
			for(int j=1; j+i<=N; j++){
				if(multi(dp[i+j+1], per[j-1])>=K){
					ans.pb(i+j);
					chk[i+j] = true;
					sel[i+j] = i;
					for(int k=i+1; k<i+j; k++){
						int tmp = k;
						while(1){
							chk[tmp] = true;
							if(sel[tmp]==0)	break;
							tmp = sel[tmp];
						}
						for(int t=i; t<i+j; t++){
							if(chk[t])	continue;
							if(k!=i+j && multi(dp[i+j+1], per[i+j-1-k])<K){
								K -= multi(dp[i+j+1], per[i+j-1-k]);
							}else{
								sel[t] = k;
								chk[t] = true;
								ans.pb(t);
								break;
							}
						}
						tmp = k;
						while(1){
							if(sel[tmp]==0)	{
								chk[tmp] = false;
								break;
							}
							tmp = sel[tmp];
						}
					}
					for(int t=i; t<i+j; t++){
						if(chk[t])	continue;
						chk[t] = true;
						ans.pb(t);
						break;
					}
					i = i+j;
					break;
				}else{
					K -= multi(dp[i+j+1], per[j-1]);
				}
			}
		}
		for(int i=0; i<ans.size(); i++){
			printf("%d ", ans[i]);
		}
		printf("\n");
		while(!ans.empty())	ans.pop_back();
	}
	return 0;
}


#include <bits/stdc++.h>

#define all(v) (v).begin(), (v).end()
#define sortv(v) sort(all(v))
#define uniqv(v) (v).erase(unique(all(v)), (v).end())
#define pb push_back
#define FI first
#define SE second
#define lb lower_bound
#define ub upper_bound
#define mp make_pair
#define test 1
#define TEST if(test)

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

const int MOD = 1000000007; // 998244353
const int INF = 2e9;
const ll INFLL = 1e18;
const int MAX_N = 60;

int T;
int N;
ll K;
ll per[MAX_N+1];
ll dp[MAX_N+1];

ll multi(ll A, ll B){
	if(A==0 || B==0)	return 0;
	if(INFLL/A<B){
		return INFLL;
	}return A*B;
}
ll add(ll A, ll B){
	if(A>=INFLL || B>=INFLL)	return INFLL;
	return min(A+B, INFLL);
}

vector<int> ans;

bool chk[MAX_N+1];

int main(){
	per[0] = per[1] = 1;
	for(int i=2; i<=MAX_N; i++){
		per[i] = multi(per[i-1], (ll)i);
	}
	scanf("%d", &T);
	while(T--){
		scanf("%d%lld", &N, &K);
		for(int i=1; i<=N; i++)	chk[i] = false;
		dp[N+1] = dp[N] = 1;
		for(int i=N-1; i>=1; i--){
			dp[i] = dp[i+1];
			for(int j=1; i+j<=N; j++){
				dp[i] = add(dp[i], multi(dp[i+j+1], per[j-1]));
			}
			//cout<<i<<" "<<dp[i]<<endl;
		}
		if(K>dp[1]){
			printf("-1\n");
			continue;
		}
		for(int i=1; i<=N; i++){
			if(K<=dp[i+1]){
				ans.pb(i);
				continue;
			}
			K -= dp[i+1];
			for(int j=1; j+i<=N; j++){
				if(multi(dp[i+j+1], per[j-1])>=K){
					ans.pb(i+j);
					chk[i+j] = true;
					for(int k=i+1; k<=i+j; k++){
						for(int t=i; t<i+j; t++){
							if(chk[t])	continue;
							if(k!=i+j && multi(dp[i+j+1], per[i+j-1-k])<K){
								K -= multi(dp[i+j+1], per[i+j-1-k]);
							}else{
								chk[t] = true;
								ans.pb(t);
								break;
							}
						}
					}
					i = i+j;
					break;
				}else{
					K -= multi(dp[i+j+1], per[j]);
				}
			}
		}
		for(int i=0; i<ans.size(); i++){
			printf("%d ", ans[i]);
		}
		printf("\n");
		while(!ans.empty())	ans.pop_back();
	}
	return 0;
}


#include <bits/stdc++.h>

#define all(v) (v).begin(), (v).end()
#define sortv(v) sort(all(v))
#define uniqv(v) (v).erase(unique(all(v)), (v).end())
#define pb push_back
#define FI first
#define SE second
#define lb lower_bound
#define ub upper_bound
#define mp make_pair
#define test 1
#define TEST if(test)

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

const int MOD = 1000000007; // 998244353
const int INF = 2e9;
const ll INFLL = 1e18;
const int MAX_N = 60;

int T;
int N;
ll K;
ll per[MAX_N+1];
ll dp[MAX_N+1];

ll multi(ll A, ll B){
	if(A==0 || B==0)	return 0;
	if(INFLL/A<B){
		return INFLL;
	}return A*B;
}
ll add(ll A, ll B){
	if(A>=INFLL || B>=INFLL)	return INFLL;
	return min(A+B, INFLL);
}

vector<int> ans;

bool chk[MAX_N+1];
int sel[MAX_N+1];

int main(){
	per[0] = per[1] = 1;
	for(int i=2; i<=MAX_N; i++){
		per[i] = multi(per[i-1], (ll)i);
	}
	scanf("%d", &T);
	while(T--){
		scanf("%d%lld", &N, &K);
		for(int i=1; i<=N; i++)	chk[i] = sel[i] = 0;
		dp[N+1] = dp[N] = 1;
		for(int i=N-1; i>=1; i--){
			dp[i] = dp[i+1];
			for(int j=1; i+j<=N; j++){
				dp[i] = add(dp[i], multi(dp[i+j+1], per[j-1]));
			}
			//cout<<i<<" "<<dp[i]<<endl;
		}
		if(K>dp[1]){
			printf("-1\n");
			continue;
		}
		for(int i=1; i<=N; i++){
			if(K<=dp[i+1]){
				ans.pb(i);
				continue;
			}
			K -= dp[i+1];
			for(int j=1; j+i<=N; j++){
				if(multi(dp[i+j+1], per[j-1])>=K){
					ans.pb(i+j);
					chk[i+j] = true;
					sel[i+j] = i;
					for(int k=i+1; k<i+j; k++){
						int tmp = k;
						while(1){
							chk[tmp] = true;
							if(sel[tmp]==0)	break;
							tmp = sel[tmp];
						}
						for(int t=i; t<i+j; t++){
							if(chk[t])	continue;
							if(k!=i+j && multi(dp[i+j+1], per[i+j-1-k])<K){
								K -= multi(dp[i+j+1], per[i+j-1-k]);
							}else{
								sel[t] = k;
								chk[t] = true;
								ans.pb(t);
								break;
							}
						}
						tmp = k;
						while(1){
							if(sel[tmp]==0)	{
								chk[tmp] = false;
								break;
							}
							tmp = sel[tmp];
						}
					}
					for(int t=i; t<i+j; t++){
						if(chk[t])	continue;
						chk[t] = true;
						ans.pb(t);
						break;
					}
					i = i+j;
					break;
				}else{
					K -= multi(dp[i+j+1], per[j]);
				}
			}
		}
		for(int i=0; i<ans.size(); i++){
			printf("%d ", ans[i]);
		}
		printf("\n");
		while(!ans.empty())	ans.pop_back();
	}
	return 0;
}


#include <sstream>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<ctime>
#include<cmath>
#include<map>
#include<unordered_map>
#include<stack>
#include<random>
#include<queue>
#include<list>
#include<math.h>
#include<bitset>
#include<fstream>
using namespace std;
#define ll long long int
#define ld long double
#define key pair<ld,ld>
#define ii pair<int,int>
#define si set<int>
#define vii vector<pair<int,int> >
#define vi vector<int>
#define vll vector<ll>
#define vb vector<bool>
#define vvi vector<vector<int> >
#define vs vector<string>
#define all(v) v.begin(),v.end()
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define nu 100001
#define mul(x,y) ((ll)(x)*(y))%mod
#define tr(c,i) for(auto i = (c).begin(); i != (c).end(); i++)
#define fastio  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define forn(i, n) for (int i = 0; i < int(n); i++)
const ll INF=9223372036854775807;
const ll mod=998244353;
int  MOD(int  a, int  b)
{
    if(a>b)
        return a-b;
    else
        return b-a;
}
ll  max3(ll  a,ll  b, ll  c)
{
    return max(c,max(a,b));
}
ll min3(ll  a,ll  b,ll  c)
{
    return min(a,min(b,c));
}
ll  power(ll  x,ll  y,ll mod1)
{
    ll res = 1;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x)%mod1;
        y = y>>1;
        x = (x*x)%mod1;
    }
    return res;
}
ll logg(ll  a)
{
    ll  x=0;
    while(a>1)
    {
        x++;
        a/=2;
    }
    return x;
}
ll  gcd(ll  a,ll   b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
ll abso(ll x)
{
    if(x<0)
    {
        return -x;
    }
    return x;
}
ll ceiling (ll  a,ll  b)
{
    if(a%b==0)
    {
        return a/b;
    }
    else
    {
        return a/b+1;
    }
}
ll modinv(ll x,ll p)
{
    return power(x,p-2,p);
}
struct point
{
    ll x;ll y;
};
ll cross(point a,point b)
{
    return a.x*b.y-b.x*a.y;
}
bool ccw(point p1,point p2)
{
   /* point n1;n1.x=p1.x-p0.x;n1.y=p1.y-p0.y;
    point n2;n2.x=p2.x-p0.x;n2.y=p2.y-p0.y;*/
    if(cross(p1,p2)>0)
    {
        return true;
    }
    else
    {
        return false;
        //No need to worry about cross=0 case in this question;
    }
}
void ccw_sort(vector<point> &v)
{
    vector<point> v1,v2;
    for(int i=0;i<v.size();i++)
    {
        if(v[i].y>0)
        {
            v1.push_back(v[i]);
        }
        else
        {
            v2.push_back(v[i]);
        }
    }
    sort(all(v1),ccw);
    sort(all(v2),ccw);
    for(int i=0;i<v2.size();i++)
    {
        v1.push_back(v2[i]);
    }
    v=v1;
}
vll fac(21,1);
vll dp(51);
vector<ll> findxn(ll n,ll k)
{
    if(n==0)
    {
        vll a;a.resize(0);
        return a;
    }
    if(n==1)
    {
        vll a;a.push_back(1);
        return a;
    }
    ll i=1;ll sm=0;
    while(sm+dp[n-i]*fac[max((ll)0,(ll)i-2)]<k)
    {
        sm+=dp[n-i]*fac[max((ll)0,(ll)i-2)];
        i++;
    }
    ll x=i;
    vll a;a.push_back(i);
    set<ll> g;
    g.insert(i);
    ll cur=sm;
    vector<vi> ds(i+1);vi col(i+1);
    for(int j=1;j<=i;j++)
    {
        ds[j].pb(j);
        col[j]=j;
    }
    for(int j=2;j<=i;j++)
    {

        for(int kk=1;kk<=i-1;kk++)
        {

            int xx=col[j];
            int yy=col[kk];
            if(kk!=j&&(g.find(kk)==g.end())&&xx!=yy)
            {
                if(cur+(fac[max((ll)0,x-j-1)])*dp[n-i]>=k)
                {
                    a.push_back(kk);
                    g.insert(kk);
                    if(ds[xx].size()>ds[yy].size())
                    {
                        for(int ff=0;ff<ds[yy].size();ff++)
                        {
                            ds[xx].push_back(ds[yy][ff]);
                            col[ds[yy][ff]]=xx;
                        }
                    }
                    else
                    {
                        int gg=xx;
                        xx=yy;
                        yy=gg;
                        for(int ff=0;ff<ds[yy].size();ff++)
                        {
                            ds[xx].push_back(ds[yy][ff]);
                            col[ds[yy][ff]]=xx;
                        }
                    }

                    break;
                }
                else
                {
                    cur+=(fac[max((ll)0,x-j-1)])*dp[n-i];
                }
            }
        }
    }

    auto b=findxn((ll)n-i,k-cur);
    for(int j=0;j<b.size();j++)
    {
        b[j]+=i;
    }
    for(int j=0;j<b.size();j++)
    {
        a.push_back(b[j]);
    }
    return a;
}
int main()
{
    for(ll i=1;i<=20;i++)
    {
        fac[i]=fac[i-1]*i;
    }
    dp[0]=1;
    for(ll i=1;i<=22;i++)
    {
        for(ll x=1;x<=i;x++)
        {
            dp[i]=dp[i]+dp[i-x]*fac[max((ll)0,x-2)];
        }
    }
    for(ll i=23;i<=50;i++)
    {
        dp[i]=INF;
    }
    int t;
    cin>>t;
    while(t--)
    {
        int n;ll k;
        cin>>n>>k;
        vector<int> ans;
        int num=0;
        if(n<=22&&dp[n]<k)
        {
            cout<<-1<<endl;continue;
        }
        while(n>22)
        {
            num++;n--;
            ans.push_back(num);
        }
        auto v=findxn(n,k);
        for(int i=0;i<v.size();i++)
        {
            v[i]+=num;
        }
        for(int i=0;i<v.size();i++)
        {
            ans.push_back(v[i]);
        }
        for(int i=0;i<ans.size();i++)
        {
            cout<<ans[i]<<' ';
        }
        cout<<endl;
    }
}


#include <sstream>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<ctime>
#include<cmath>
#include<map>
#include<unordered_map>
#include<stack>
#include<random>
#include<queue>
#include<list>
#include<math.h>
#include<bitset>
#include<fstream>
using namespace std;
#define ll long long int
#define ld long double
#define key pair<ld,ld>
#define ii pair<int,int>
#define si set<int>
#define vii vector<pair<int,int> >
#define vi vector<int>
#define vll vector<ll>
#define vb vector<bool>
#define vvi vector<vector<int> >
#define vs vector<string>
#define all(v) v.begin(),v.end()
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define nu 100001
#define mul(x,y) ((ll)(x)*(y))%mod
#define tr(c,i) for(auto i = (c).begin(); i != (c).end(); i++)
#define fastio  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define forn(i, n) for (int i = 0; i < int(n); i++)
const ll INF=9223372036854775807;
const ll mod=998244353;
int  MOD(int  a, int  b)
{
    if(a>b)
        return a-b;
    else
        return b-a;
}
ll  max3(ll  a,ll  b, ll  c)
{
    return max(c,max(a,b));
}
ll min3(ll  a,ll  b,ll  c)
{
    return min(a,min(b,c));
}
ll  power(ll  x,ll  y,ll mod1)
{
    ll res = 1;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x)%mod1;
        y = y>>1;
        x = (x*x)%mod1;
    }
    return res;
}
ll logg(ll  a)
{
    ll  x=0;
    while(a>1)
    {
        x++;
        a/=2;
    }
    return x;
}
ll  gcd(ll  a,ll   b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
ll abso(ll x)
{
    if(x<0)
    {
        return -x;
    }
    return x;
}
ll ceiling (ll  a,ll  b)
{
    if(a%b==0)
    {
        return a/b;
    }
    else
    {
        return a/b+1;
    }
}
ll modinv(ll x,ll p)
{
    return power(x,p-2,p);
}
struct point
{
    ll x;ll y;
};
ll cross(point a,point b)
{
    return a.x*b.y-b.x*a.y;
}
bool ccw(point p1,point p2)
{
   /* point n1;n1.x=p1.x-p0.x;n1.y=p1.y-p0.y;
    point n2;n2.x=p2.x-p0.x;n2.y=p2.y-p0.y;*/
    if(cross(p1,p2)>0)
    {
        return true;
    }
    else
    {
        return false;
        //No need to worry about cross=0 case in this question;
    }
}
void ccw_sort(vector<point> &v)
{
    vector<point> v1,v2;
    for(int i=0;i<v.size();i++)
    {
        if(v[i].y>0)
        {
            v1.push_back(v[i]);
        }
        else
        {
            v2.push_back(v[i]);
        }
    }
    sort(all(v1),ccw);
    sort(all(v2),ccw);
    for(int i=0;i<v2.size();i++)
    {
        v1.push_back(v2[i]);
    }
    v=v1;
}
vll fac(21,1);
vll dp(51);
vector<ll> findxn(ll n,ll k)
{
    if(n==0)
    {
        vll a;a.resize(0);
        return a;
    }
    ll i=1;ll sm=0;
    while(sm+dp[n-i]*fac[max((ll)0,(ll)i-2)]<k)
    {
        sm+=dp[n-i]*fac[max((ll)0,(ll)i-2)];
        i++;
    }
    ll x=i;
    vll a;a.push_back(i);
    set<ll> g;
    g.insert(i);
    ll cur=sm;
    for(int j=2;j<=i;j++)
    {

        for(int kk=1;kk<=i-1;kk++)
        {
            if(kk!=j&&(g.find(kk)==g.end()))
            {
                if(cur+(fac[max((ll)0,x-j-1)])*dp[n-i]>=k)
                {
                    a.push_back(kk);
                    g.insert(kk);
                    break;
                }
                else
                {
                    cur+=(fac[max((ll)0,x-j-1)])*dp[n-i];
                }
            }
        }
    }

    auto b=findxn((ll)n-i,k-cur);
    for(int j=0;j<b.size();j++)
    {
        b[j]+=i;
    }
    for(int j=0;j<b.size();j++)
    {
        a.push_back(b[j]);
    }
    return a;
}
int main()
{
    for(ll i=1;i<=20;i++)
    {
        fac[i]=fac[i-1]*i;
    }
    dp[0]=1;
    for(ll i=1;i<=22;i++)
    {
        for(ll x=1;x<=i;x++)
        {
            dp[i]=dp[i]+dp[i-x]*fac[max((ll)0,x-2)];
        }
    }
    for(ll i=23;i<=50;i++)
    {
        dp[i]=INF;
    }
    int t;
    cin>>t;
    while(t--)
    {
        int n;ll k;
        cin>>n>>k;
        vector<int> ans;
        int num=0;
        if(n<=22&&dp[n]<k)
        {
            cout<<-1<<endl;continue;
        }
        while(n>22)
        {
            num++;n--;
            ans.push_back(num);
        }
        auto v=findxn(n,k);
        for(int i=0;i<v.size();i++)
        {
            v[i]+=num;
        }
        for(int i=0;i<v.size();i++)
        {
            ans.push_back(v[i]);
        }
        for(int i=0;i<ans.size();i++)
        {
            cout<<ans[i]<<' ';
        }
        cout<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

typedef long long int ll;
const ll INF = 1e18;
ll dp[60],cyc[60];

ll add(ll x,ll y){
	return min(x+y,INF);
}

ll mult(ll x,ll y){
	if(1.0L*x*y>INF)	return INF;
	else	return x*y;
}
int ans[51];
bool vis[51];
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int T;
	cin>>T;
	cyc[0]=cyc[1]=cyc[2]=1;
	for(int i=3;i<=50;i++)
		cyc[i]=mult(cyc[i-1],(i-2));
	while(T--){
		int n,k;
		cin>>n>>k;
		dp[n+1]=1;
		for(int i=n;i>=1;i--){
			dp[i]=0;
			for(int j=i;j<=n;j++)
				dp[i]=add(dp[i],mult(dp[j+1],cyc[j-i+1]));
		}
		memset(vis,false,sizeof vis);
		memset(ans,0,sizeof ans);

		if(dp[1]<k){
			cout<<-1<<'\n';
			continue;
		}
		for(int pos=1;pos<=n;pos++){
			for(int i=pos;i<=n;i++){
				if(mult(dp[i+1],cyc[i-pos+1])<k){
					k-=mult(dp[i+1],cyc[i-pos+1]);
					continue;
				}
				ll cnt;
				if(k%dp[i+1]==0){
					cnt=k/dp[i+1];
					k=dp[i+1];
				}
				else{
					cnt=k/dp[i+1];
					k-=dp[i+1]*cnt;
					cnt++;
				}
				vis[i]=true;
				ans[pos]=i;
				for(int j=pos+1;j<=i;j++){
					for(int z=pos;z<i;z++){
						if(z==j||vis[z])	continue;
						if(i==j){
							ans[j]=z;
							vis[z]=true;
							break;
						}
						int t=z;
						while(t!=j&&t!=0)	t=ans[t];
						if(t==0){
							if(cyc[i-j+1]<cnt){
								cnt-=cyc[i-j+1];
								continue;
							}
							else{
								ans[j]=z;
								vis[z]=true;
								break;
							}
						}
						else	continue;
					}
				}
				pos=i;
				break;
			}
		}
		for(int i=1;i<=n;i++)
			cout<<ans[i]<<' ';
		cout<<'\n';
	}
}


#include<bits/stdc++.h>
using namespace std;

typedef long long int ll;
const ll INF = 1e18;
ll dp[60],cyc[60];

ll add(ll x,ll y){
	return min(x+y,INF);
}

ll mult(ll x,ll y){
	if(1.0L*x*y>INF)	return INF;
	else	return x*y;
}
int ans[51];
bool vis[51];
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int T;
	cin>>T;
	cyc[0]=cyc[1]=cyc[2]=1;
	for(int i=3;i<50;i++)
		cyc[i]=mult(cyc[i-1],(i-2));
	while(T--){
		int n,k;
		cin>>n>>k;
		dp[n+1]=1;
		for(int i=n;i>=1;i--){
			dp[i]=0;
			for(int j=i;j<=n;j++)
				dp[i]=add(dp[i],mult(dp[j+1],cyc[j-i+1]));
		}
		memset(vis,false,sizeof vis);
		memset(ans,0,sizeof ans);

		if(dp[1]<k){
			cout<<-1<<'\n';
			continue;
		}
		for(int pos=1;pos<=n;pos++){
			for(int i=pos;i<=n;i++){
				if(mult(dp[i+1],cyc[i-pos+1])<k){
					k-=mult(dp[i+1],cyc[i-pos+1]);
					continue;
				}
				ll cnt=(k-1)/dp[i+1];
				k-=cnt*dp[i+1];
				vis[i]=true;
				ans[pos]=i;
				cnt++;
				for(int j=pos+1;j<=i;j++){
					for(int z=pos;z<i;z++){
						if(z==j||vis[z])	continue;
						if(i==j){
							ans[j]=z;
							vis[z]=true;
							break;
						}
						int t=z;
						while(t!=j&&t!=0)	t=ans[t];
						if(t==0){
							if(i!=j+1&&cyc[i-j-2]<cnt){
								cnt-=cyc[i-j-2];
								continue;
							}
							else{
								ans[j]=z;
								vis[z]=true;
								break;
							}
						}
						else	continue;
					}
				}
				pos=i;
				break;
			}
		}
		for(int i=1;i<=n;i++)
			cout<<ans[i]<<' ';
		cout<<'\n';
	}
}


#include<bits/stdc++.h>
using namespace std;

typedef long long int ll;
const ll INF = 2e18;
ll dp[60],cyc[60];

ll add(ll x,ll y){
	return min(x+y,INF);
}

ll mult(ll x,ll y){
	if(1.0L*x*y>INF)	return INF;
	else	return x*y;
}
int ans[51];
bool vis[51];
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int T;
	cin>>T;
	cyc[0]=cyc[1]=cyc[2]=1;
	for(int i=3;i<=50;i++)
		cyc[i]=mult(cyc[i-1],(i-2));
	while(T--){
		int n;
		ll k;
		cin>>n>>k;
		dp[n+1]=1;
		for(int i=n;i>=1;i--){
			dp[i]=0;
			for(int j=i;j<=n;j++)
				dp[i]=add(dp[i],mult(dp[j+1],cyc[j-i+1]));
		}
		memset(vis,false,sizeof vis);
		memset(ans,0,sizeof ans);

		if(dp[1]<k){
			cout<<-1<<'\n';
			continue;
		}
		for(int pos=1;pos<=n;pos++){
			for(int i=pos;i<=n;i++){
				if(mult(dp[i+1],cyc[i-pos+1])<k){
					k-=mult(dp[i+1],cyc[i-pos+1]);
					continue;
				}
				ll cnt;
				if(k%dp[i+1]==0){
					cnt=k/dp[i+1];
					k=dp[i+1];
				}
				else{
					cnt=k/dp[i+1];
					k-=dp[i+1]*cnt;
					cnt++;
				}
				vis[i]=true;
				ans[pos]=i;
				for(int j=pos+1;j<=i;j++){
					for(int z=pos;z<i;z++){
						if(z==j||vis[z])	continue;
						if(i==j){
							ans[j]=z;
							vis[z]=true;
							break;
						}
						int t=z;
						while(t!=j&&t!=0)	t=ans[t];
						if(t==0){
							if(cyc[i-j+1]<cnt){
								cnt-=cyc[i-j+1];
								continue;
							}
							else{
								ans[j]=z;
								vis[z]=true;
								break;
							}
						}
						else	continue;
					}
				}
				pos=i;
				break;
			}
		}
		for(int i=1;i<=n;i++)
			cout<<ans[i]<<' ';
		cout<<'\n';
	}
}


#include <bits/stdc++.h>

#define task "E"
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define Rep(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X) { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
#define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';}
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a));
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div
#define j0 ___j0
#define jn ___jn

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vl;
const int N = 52;
int T, n, a[N], rt[N], rk[N];
ll k, gt[N], dp[N];
bool use[N];
int frt(int i) {return rt[i] == i ? i : rt[i] = frt(rt[i]);}
void join(int i, int j)
{
    int x = frt(i), y = frt(j);
    if (rk[x] < rk[y]) swap(x, y);
    if (rk[x] == rk[y]) rk[x]++;
    rt[y] = x;

}
int main()
{
#ifdef HynDuf
    freopen(task".in", "r", stdin);
    //freopen(task".out", "w", stdout);
#else
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
#endif
    gt[0] = 1;
    rep(i, 1, 50) gt[i] = gt[i - 1] * i;
    cin >> T;
    while (T--)
    {
        cin >> n >> k;
        rep(i, 1, n) rt[i] = i, rk[i] = 0, use[i] = 0, dp[i] = a[i] = 0;
        dp[n + 1] = 1;
        Rep(i, n, 1) rep(j, i, n) dp[i] += dp[j + 1] * gt[max(0, j - i - 1)];
        //PR(dp, 1, n);
        rep(pos, 1, n)
        {
            rep(i, pos, n)
            {
                int nn = i - pos + 1;
                if (k > gt[max(0, nn - 2)] * dp[i + 1]) k -= gt[max(0, nn - 2)] * dp[i + 1];
                else
                {
                    a[pos] = i;
                    use[i] = 1;
                    join(pos, i);
                    rep(j, pos + 1, i)
                    {
                        rep(l, pos, i) if (!use[l] && l != j)
                        {
                            if (j == i)
                            {
                                a[j] = l;
                                use[l] = 1;
                                break;
                            }
                            if (frt(j) != frt(l))
                            {
                                if (k <= gt[max(0, nn - 2 - (j - pos))] * dp[i + 1])
                                {
                                    a[j] = l;
                                    use[l] = 1;
                                    join(j, l);
                                    break;
                                } else k -= gt[max(0, nn - 2 - (j - pos))] * dp[i + 1];
                            }
                        }
                    }
                    pos = i;
                    break;
                }
            }
        }
        rep(i, 1, n) if (a[i] == 0) k = 1e9;
        if (k > 1) cout << "-1\n";
        else rep(i, 1, n) cout << a[i] << " \n"[i == n];
    }
    return 0;
}


#include <bits/stdc++.h>

#define task "E"
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define Rep(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X) { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
#define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';}
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a));
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div
#define j0 ___j0
#define jn ___jn

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vl;
const int N = 52;
const ll INF = 2e18;
int T, n, a[N], rt[N], rk[N];
ll k, gt[N], dp[N];
bool use[N];
int frt(int i) {return rt[i] == i ? i : rt[i] = frt(rt[i]);}
void join(int i, int j)
{
    int x = frt(i), y = frt(j);
    if (rk[x] < rk[y]) swap(x, y);
    if (rk[x] == rk[y]) rk[x]++;
    rt[y] = x;
}
ll mul(ll x, ll y)
{
    ll res = 0;
    while (y)
    {
        if (y & 1) res = min(INF, res + x);
        x = min(INF, x + x);
        y >>= 1;
    }
    return res;
}
int main()
{
#ifdef HynDuf
    freopen(task".in", "r", stdin);
    //freopen(task".out", "w", stdout);
#else
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
#endif
    gt[0] = 1;
    rep(i, 1, 19) gt[i] = gt[i - 1] * i;
    rep(i, 20, 50) gt[i] = INF;
    cin >> T;
    while (T--)
    {
        cin >> n >> k;
        rep(i, 1, n) rt[i] = i, rk[i] = 0, use[i] = 0, dp[i] = a[i] = 0;
        dp[n + 1] = 1;
        Rep(i, n, 1) rep(j, i, n) dp[i] = min(INF, dp[i] + mul(dp[j + 1], gt[max(0, j - i - 1)]));
        //PR(dp, 1, n);
        rep(pos, 1, n)
        {
            rep(i, pos, n)
            {
                int nn = i - pos + 1;
                if (k > mul(gt[max(0, nn - 2)], dp[i + 1])) k -= mul(gt[max(0, nn - 2)], dp[i + 1]);
                else
                {
                    a[pos] = i;
                    use[i] = 1;
                    join(pos, i);
                    rep(j, pos + 1, i)
                    {
                        rep(l, pos, i) if (!use[l] && l != j)
                        {
                            if (j == i)
                            {
                                a[j] = l;
                                use[l] = 1;
                                break;
                            }
                            if (frt(j) != frt(l))
                            {
                                if (k <= mul(gt[max(0, nn - 2 - (j - pos))], dp[i + 1]))
                                {
                                    a[j] = l;
                                    use[l] = 1;
                                    join(j, l);
                                    break;
                                } else k -= mul(gt[max(0, nn - 2 - (j - pos))], dp[i + 1]);
                            }
                        }
                    }
                    pos = i;
                    break;
                }
            }
        }
        rep(i, 1, n) if (a[i] == 0) k = 1e9;
        if (k > 1) cout << "-1\n";
        else rep(i, 1, n) cout << a[i] << " \n"[i == n];
    }
    return 0;
}


#include <bits/stdc++.h>

#define task "E"
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define Rep(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X) { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
#define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';}
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a));
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div
#define j0 ___j0
#define jn ___jn

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vl;
const int N = 52;
int T, n, a[N], rt[N], rk[N];
ll k, gt[N], dp[N];
bool use[N];
int frt(int i) {return rt[i] == i ? i : rt[i] = frt(rt[i]);}
void join(int i, int j)
{
    int x = frt(i), y = frt(j);
    if (rk[x] < rk[y]) swap(x, y);
    if (rk[x] == rk[y]) rk[x]++;
    rt[y] = x;

}
int main()
{
#ifdef HynDuf
    freopen(task".in", "r", stdin);
    //freopen(task".out", "w", stdout);
#else
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
#endif
    gt[0] = 1;
    rep(i, 1, 19) gt[i] = gt[i - 1] * i;
    rep(i, 20, 50) gt[i] = 2e18;
    cin >> T;
    while (T--)
    {
        cin >> n >> k;
        rep(i, 1, n) rt[i] = i, rk[i] = 0, use[i] = 0, dp[i] = a[i] = 0;
        dp[n + 1] = 1;
        Rep(i, n, 1) rep(j, i, n) dp[i] += dp[j + 1] * gt[max(0, j - i - 1)];
        //PR(dp, 1, n);
        rep(pos, 1, n)
        {
            rep(i, pos, n)
            {
                int nn = i - pos + 1;
                if (k > gt[max(0, nn - 2)] * dp[i + 1]) k -= gt[max(0, nn - 2)] * dp[i + 1];
                else
                {
                    a[pos] = i;
                    use[i] = 1;
                    join(pos, i);
                    rep(j, pos + 1, i)
                    {
                        rep(l, pos, i) if (!use[l] && l != j)
                        {
                            if (j == i)
                            {
                                a[j] = l;
                                use[l] = 1;
                                break;
                            }
                            if (frt(j) != frt(l))
                            {
                                if (k <= gt[max(0, nn - 2 - (j - pos))] * dp[i + 1])
                                {
                                    a[j] = l;
                                    use[l] = 1;
                                    join(j, l);
                                    break;
                                } else k -= gt[max(0, nn - 2 - (j - pos))] * dp[i + 1];
                            }
                        }
                    }
                    pos = i;
                    break;
                }
            }
        }
        rep(i, 1, n) if (a[i] == 0) k = 1e9;
        if (k > 1) cout << "-1\n";
        else rep(i, 1, n) cout << a[i] << " \n"[i == n];
    }
    return 0;
}


#include <bits/stdc++.h>

#define task "E"
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define Rep(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X) { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
#define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';}
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a));
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div
#define j0 ___j0
#define jn ___jn

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vl;
const int N = 52;
int T, n, a[N], rt[N], rk[N];
ll k, gt[N], dp[N];
bool use[N];
int frt(int i) {return rt[i] == i ? i : rt[i] = frt(rt[i]);}
void join(int i, int j)
{
    int x = frt(i), y = frt(j);
    if (rk[x] < rk[y]) swap(x, y);
    if (rk[x] == rk[y]) rk[x]++;
    rt[y] = x;

}
int main()
{
#ifdef HynDuf
    freopen(task".in", "r", stdin);
    //freopen(task".out", "w", stdout);
#else
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
#endif
    gt[0] = 1;
    rep(i, 1, 50) gt[i] = gt[i - 1] * i;
    cin >> T;
    while (T--)
    {
        cin >> n >> k;
        rep(i, 1, n) rt[i] = i, rk[i] = 0, use[i] = 0, dp[i] = a[i] = 0;
        dp[n + 1] = 1;
        Rep(i, n, 1) rep(j, i, n) dp[i] += dp[j + 1] * gt[max(0, j - i - 1)];
        //PR(dp, 1, n);
        rep(pos, 1, n)
        {
            rep(i, pos, n)
            {
                int nn = i - pos + 1;
                if (k > gt[max(0, nn - 2)] * dp[i + 1]) k -= gt[max(0, nn - 2)] * dp[i + 1];
                else
                {
                    a[pos] = i;
                    use[i] = 1;
                    join(pos, i);
                    rep(j, pos + 1, i)
                    {
                        rep(l, pos, i) if (!use[l] && l != j)
                        {
                            if (j == i)
                            {
                                a[j] = l;
                                use[l] = 1;
                                break;
                            }
                            if (frt(j) != frt(l))
                            {
                                if (k <= gt[max(0, nn - 2 - (j - pos))] * dp[i + 1])
                                {
                                    a[j] = l;
                                    use[l] = 1;
                                    join(j, l);
                                    break;
                                } else k -= gt[max(0, nn - 2 - (j - pos))] * dp[i + 1];
                            }
                        }
                    }
                    pos = i;
                    break;
                }
            }
            if (a[pos] == 0) break;
        }
        if (k > 1) cout << "-1\n";
        else rep(i, 1, n) cout << a[i] << " \n"[i == n];
    }
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <utility>
#include <cassert>
#include <limits.h>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <queue>
#include <array>
#include <list>

using std::cin;
using std::cout;
using std::vector;
using std::string;
using std::endl;
using std::pair;

using vi = vector<int>;
using pii = pair<int, int>;
using vii = vector<pii>;
using ll = long long;

int solve();
int init();

int main(int argc, char* argv[])
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif //ONLINE_JUDGE
	::std::ios::sync_with_stdio(false);
	::std::cin.tie(0);
	::std::cout.tie(0);

	init();

	int t = 1;
	cin >> t;

	for (int i = 1; i <= t; ++i) {
		solve();
	}
}

constexpr int MOD = 998244353;
constexpr ll infinity_ll = 1'000'000'000'000'000'000LL + 8 ;

struct ll_int
{
	ll x;
};
ll operator * (ll_int a, ll_int b) {

	if (!b.x)
		return  0  ;

	if (infinity_ll / b.x < a.x)
		return   infinity_ll  ;
	else
		return b.x * a.x  ;
}

int N;
int ans[60];
int t[60];
ll K, fac[60], dp[60];
bool visit[60];
ll factorial(int n) {
	if (n < 0)return 1;
	return fac[n];
}

int init()
{
	fac[0] = 1;
	dp[0] = 1;
	for (int i = 1; i < 60; ++i) {
		fac[i] = ll_int{ i } *ll_int{ fac[i - 1] };
	}
	for (int i = 1; i < 60; ++i) {
		for (int j = 1; j <= i; ++j) {
			ll c = ll_int{ dp[i - j] } *ll_int{ factorial(j-2) };
			dp[i] += c;
			dp[i] = std::min(infinity_ll, dp[i]);
		}
		dp[i] = std::min(infinity_ll, dp[i]);
	}
	return 0;
}

void update(int L, int R)
{
	for (int i = L; i <= R; ++i)
	{
		t[i] = i;
		visit[i] = false;
	}
	ans[L] = R;
	visit[R] = 1;
	std::swap(t[L], t[R]);
	for (int i = L + 1; i <= R - 1; ++i) {
		for (int j = L; j <= R; ++j) {
			if (!visit[j] && i != t[j]) {
				const ll m = ll_int{ fac[R - i - 1] } *ll_int{ dp[N - R] };
				if (m >= K) {
					ans[i] = j;
					visit[j] = 1;
					int x = t[i];
					int y = t[j];
					t[x] = y;
					t[y] = x;
					break;
				}
				else {
					K -= m;
				}
			}
		}
	}

	ans[R] = t[R];
}

int solve()
{
	cin >> N >> K;
	if (dp[N] < K) {
		cout << -1 << '\n';
		return 0;
	}
	int cur = 1;
	while (cur <= N) {
		for (int i = 1; i <= N - cur + 1; ++i)
		{
			const ll m = ll_int{ factorial(i - 2) } *ll_int{ dp[N - cur - i + 1] };
			if ( m >= K) {
				update(cur, cur + i - 1);
				cur += i;
				break;
			}
			else {
				K -= m;

			}
		}
	}
	for (int i = 1; i <= N; ++i) {
		cout << ans[i] << (i < N ? ' ' : '\n');
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <utility>
#include <cassert>
#include <limits.h>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <queue>
#include <array>
#include <list>

using std::cin;
using std::cout;
using std::vector;
using std::string;
using std::endl;
using std::pair;

using vi = vector<int>;
using pii = pair<int, int>;
using vii = vector<pii>;
using ll = long long;

int solve();
int init();

int main(int argc, char* argv[])
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif //ONLINE_JUDGE
	::std::ios::sync_with_stdio(false);
	::std::cin.tie(0);
	::std::cout.tie(0);

	init();

	int t = 1;
	cin >> t;

	for (int i = 1; i <= t; ++i) {
		solve();
	}
}

constexpr int MOD = 998244353;
constexpr ll infinity_ll = 1LL << 62;

struct ll_int
{
	ll x;
};
ll operator * (ll_int a, ll_int b) {

	if (!b.x)
		return  0  ;

	if (infinity_ll / b.x < a.x)
		return   infinity_ll  ;
	else
		return b.x * a.x  ;
}

int N;
int ans[60];
int t[60];
ll K, fac[60], dp[60];
bool visit[60];
ll factorial(int n) {
	if (n < 0)return 1;
	return fac[n];
}

int init()
{
	fac[0] = 1;
	dp[0] = 1;
	for (int i = 1; i < 60; ++i) {
		fac[i] = ll_int{ i } *ll_int{ fac[i - 1] };
	}
	for (int i = 1; i < 60; ++i) {
		for (int j = 1; j <= i; ++j) {
			ll c = ll_int{ dp[i - j] } *ll_int{ factorial(j-2) };
			dp[i] += c;
		}
		dp[i] = std::min(infinity_ll, dp[i]);
	}
	return 0;
}

void update(int L, int R)
{
	for (int i = L; i <= R; ++i)
	{
		t[i] = i;
		visit[i] = false;
	}
	ans[L] = R;
	visit[R] = 1;
	std::swap(t[L], t[R]);
	for (int i = L + 1; i <= R - 1; ++i) {
		for (int j = L; j <= R; ++j) {
			if (!visit[j] && i != t[j]) {
				const ll m = ll_int{ fac[R - i - 1] } *ll_int{ dp[N - R] };
				if (m >= K) {
					ans[i] = j;
					visit[j] = 1;
					int x = t[i];
					int y = t[j];
					t[x] = y;
					t[y] = x;
					break;
				}
				else {
					K -= m;
				}
			}
		}
	}

	ans[R] = t[R];
}

int solve()
{
	cin >> N >> K;
	if (dp[N] < K) {
		cout << -1 << '\n';
		return 0;
	}
	int cur = 1;
	while (cur <= N) {
		for (int i = 1; i <= N - cur + 1; ++i)
		{
			const ll m = ll_int{ factorial(i - 2) } *ll_int{ dp[N - cur - i + 1] };
			if ( m >= K) {
				update(cur, cur + i - 1);
				cur += i;
				break;
			}
			else {
				K -= m;

			}
		}
	}
	for (int i = 1; i <= N; ++i) {
		cout << ans[i] << (i < N ? ' ' : '\n');
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <utility>
#include <cassert>
#include <limits.h>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <queue>
#include <array>
#include <list>

using std::cin;
using std::cout;
using std::vector;
using std::string;
using std::endl;
using std::pair;

using vi = vector<int>;
using pii = pair<int, int>;
using vii = vector<pii>;
using ll = long long;

int solve();
int init();

int main(int argc, char* argv[])
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif //ONLINE_JUDGE
	::std::ios::sync_with_stdio(false);
	::std::cin.tie(0);
	::std::cout.tie(0);

	init();

	int t = 1;
	cin >> t;

	for (int i = 1; i <= t; ++i) {
		solve();
	}
}

constexpr int MOD = 998244353;
constexpr ll infinity_ll = 1LL << 61;

struct ll_int
{
	ll x;
};
ll operator * (ll_int a, ll_int b) {

	if (!b.x)
		return  0  ;

	if (infinity_ll / b.x < a.x)
		return   infinity_ll  ;
	else
		return b.x * a.x  ;
}

int N;
int ans[60];
int t[60];
ll K, fac[60], dp[60];
bool visit[60];
ll factorial(int n) {
	if (n < 0)return 1;
	return fac[n];
}

int init()
{
	fac[0] = 1;
	dp[0] = 1;
	for (int i = 1; i < 60; ++i) {
		fac[i] = ll_int{ i } *ll_int{ fac[i - 1] };
	}
	for (int i = 1; i < 60; ++i) {
		for (int j = 1; j <= i; ++j) {
			ll c = ll_int{ dp[i - j] } *ll_int{ factorial(j-2) };
			dp[i] += c;
		}
		dp[i] = std::min(infinity_ll, dp[i]);
	}
	return 0;
}

void update(int L, int R)
{
	for (int i = L; i <= R; ++i)
	{
		t[i] = i;
		visit[i] = false;
	}
	ans[L] = R;
	visit[R] = 1;
	std::swap(t[L], t[R]);
	for (int i = L + 1; i <= R - 1; ++i) {
		for (int j = L; j <= R; ++j) {
			if (!visit[j] && i != t[j]) {
				const ll m = ll_int{ fac[R - i - 1] } *ll_int{ dp[N - R] };
				if (m >= K) {
					ans[i] = j;
					visit[j] = 1;
					int x = t[i];
					int y = t[j];
					t[x] = y;
					t[y] = x;
					break;
				}
				else {
					K -= m;
				}
			}
		}
	}

	ans[R] = t[R];
}

int solve()
{
	cin >> N >> K;
	if (dp[N] < K) {
		cout << -1 << '\n';
		return 0;
	}
	int cur = 1;
	while (cur <= N) {
		for (int i = 1; i <= N - cur + 1; ++i)
		{
			const ll m = ll_int{ factorial(i - 2) } *ll_int{ dp[N - cur - i + 1] };
			if ( m >= K) {
				update(cur, cur + i - 1);
				cur += i;
				break;
			}
			else {
				K -= m;

			}
		}
	}
	for (int i = 1; i <= N; ++i) {
		cout << ans[i] << (i < N ? ' ' : '\n');
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <utility>
#include <cassert>
#include <limits.h>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <queue>
#include <array>
#include <list>

using std::cin;
using std::cout;
using std::vector;
using std::string;
using std::endl;
using std::pair;

using vi = vector<int>;
using pii = pair<int, int>;
using vii = vector<pii>;
using ll = long long;

int solve();
int init();

int main(int argc, char* argv[])
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif //ONLINE_JUDGE
	::std::ios::sync_with_stdio(false);
	::std::cin.tie(0);
	::std::cout.tie(0);

	init();

	int t = 1;
	cin >> t;

	for (int i = 1; i <= t; ++i) {
		solve();
	}
}

constexpr int MOD = 998244353;
constexpr ll infinity_ll = 1LL << 60;

struct ll_int
{
	ll x;
};
ll operator * (ll_int a, ll_int b) {

	if (!b.x)
		return  0  ;

	if (infinity_ll / b.x < a.x)
		return   infinity_ll  ;
	else
		return b.x * a.x  ;
}

int N;
int ans[60];
int t[60];
ll K, fac[60], dp[60];
bool visit[60];
ll factorial(int n) {
	if (n < 0)return 1;
	return fac[n];
}

int init()
{
	fac[0] = 1;
	dp[0] = 1;
	for (int i = 1; i < 60; ++i) {
		fac[i] = ll_int{ i } *ll_int{ fac[i - 1] };
	}
	for (int i = 1; i < 60; ++i) {
		for (int j = 1; j <= i; ++j) {
			ll c = ll_int{ dp[i - j] } *ll_int{ factorial(j-2) };
			dp[i] += c;
		}
		dp[i] = std::min(infinity_ll, dp[i]);
	}
	return 0;
}

void update(int L, int R)
{
	for (int i = L; i <= R; ++i)
	{
		t[i] = i;
		visit[i] = false;
	}
	ans[L] = R;
	visit[R] = 1;
	std::swap(t[L], t[R]);
	for (int i = L + 1; i <= R - 1; ++i) {
		for (int j = L; j <= R; ++j) {
			if (!visit[j] && i != t[j]) {
				const ll m = ll_int{ fac[R - i - 1] } *ll_int{ dp[N - R] };
				if (m >= K) {
					ans[i] = j;
					visit[j] = 1;
					int x = t[i];
					int y = t[j];
					t[x] = y;
					t[y] = x;
					break;
				}
				else {
					K -= m;
				}
			}
		}
	}

	ans[R] = t[R];
}

int solve()
{
	cin >> N >> K;
	if (dp[N] < K) {
		cout << -1 << '\n';
		return 0;
	}
	int cur = 1;
	while (cur <= N) {
		for (int i = 1; i <= N - cur + 1; ++i)
		{
			const ll m = ll_int{ factorial(i - 2) } *ll_int{ dp[N - cur - i + 1] };
			if ( m >= K) {
				update(cur, cur + i - 1);
				cur += i;
				break;
			}
			else {
				K -= m;

			}
		}
	}
	for (int i = 1; i <= N; ++i) {
		cout << ans[i] << (i < N ? ' ' : '\n');
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <utility>
#include <cassert>
#include <limits.h>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <queue>
#include <array>
#include <list>

using std::cin;
using std::cout;
using std::vector;
using std::string;
using std::endl;
using std::pair;

using vi = vector<int>;
using pii = pair<int, int>;
using vii = vector<pii>;
using ll = long long;

int solve();
int init();

int main(int argc, char* argv[])
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif //ONLINE_JUDGE
	::std::ios::sync_with_stdio(false);
	::std::cin.tie(0);
	::std::cout.tie(0);

	init();

	int t = 1;
	cin >> t;

	for (int i = 1; i <= t; ++i) {
		solve();
	}
}

constexpr int MOD = 998244353;
constexpr ll infinity_ll = 1LL << 62;

struct ll_int
{
	ll x;
};
ll operator * (ll_int a, ll_int b) {

	if (!b.x)
		return  0  ;

	if (infinity_ll / b.x < a.x)
		return   infinity_ll  ;
	else
		return b.x * a.x  ;
}

int N;
int ans[60];
int t[60];
ll K, fac[60], dp[60];
bool visit[60];
ll factorial(int n) {
	if (n < 0)return 1;
	return fac[n];
}

int init()
{
	fac[0] = 1;
	dp[0] = 1;
	for (int i = 1; i < 60; ++i) {
		fac[i] = ll_int{ i } *ll_int{ fac[i - 1] };
	}
	for (int i = 1; i < 60; ++i) {
		for (int j = 1; j <= i; ++j) {
			ll c = ll_int{ dp[i - j] } *ll_int{ factorial(j-2) };
			dp[i] += c;
			dp[i] = std::min(infinity_ll, dp[i]);
		}
		dp[i] = std::min(infinity_ll, dp[i]);
	}
	return 0;
}

void update(int L, int R)
{
	for (int i = L; i <= R; ++i)
	{
		t[i] = i;
		visit[i] = false;
	}
	ans[L] = R;
	visit[R] = 1;
	std::swap(t[L], t[R]);
	for (int i = L + 1; i <= R - 1; ++i) {
		for (int j = L; j <= R; ++j) {
			if (!visit[j] && i != t[j]) {
				const ll m = ll_int{ fac[R - i - 1] } *ll_int{ dp[N - R] };
				if (m >= K) {
					ans[i] = j;
					visit[j] = 1;
					int x = t[i];
					int y = t[j];
					t[x] = y;
					t[y] = x;
					break;
				}
				else {
					K -= m;
				}
			}
		}
	}

	ans[R] = t[R];
}

int solve()
{
	cin >> N >> K;
	if (dp[N] < K) {
		cout << -1 << '\n';
		return 0;
	}
	int cur = 1;
	while (cur <= N) {
		for (int i = 1; i <= N - cur + 1; ++i)
		{
			const ll m = ll_int{ factorial(i - 2) } *ll_int{ dp[N - cur - i + 1] };
			if ( m >= K) {
				update(cur, cur + i - 1);
				cur += i;
				break;
			}
			else {
				K -= m;

			}
		}
	}
	for (int i = 1; i <= N; ++i) {
		cout << ans[i] << (i < N ? ' ' : '\n');
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

template<class T>
class vec: public vector<T> {
    public:
    void operator+= (vec<T> &b) {
        this->insert(this->end(), b.begin(), b.end());
    }

    vec<T> operator+ (vec<T> &b) {
        vec<T> c;
        for(T x: *this) c.push_back(x);
        for(T x: b) c.push_back(x);
        return c;
    }

    void operator+= (T &x) {
        for(int i=0; i<this->size(); i++) {
            *this[i]+=x;
        }
    }

    vec<T> operator+ (T &x) {
        vec<T> c;
        for(auto y: *this) {
            c.push_back(x+y);
        }
        return c;
    }

    void operator-= (vec<T> &b) {
        for(auto x: b) {
            this->pop_back();
        }
    }

    void operator- (vec<T> &b) {
        vec<T> a = *this;
        for(auto x: b) {
            a.pop_back();
        }
        return a;
    }
};

#define vector vec

struct rll {
    long long MAX = LLONG_MAX - 7;
    long long val;

    rll(long long x=0): val(x) {}

    rll operator+ (rll b) {
        if(val >= MAX - b.val) {
            return rll(MAX);
        }
        return rll(val+b.val);
    }

    rll operator* (rll b) {
        if(val >= MAX/b.val) {
            return rll(MAX);
        }
        return rll(val*b.val);
    }

    operator long long() {
        return val;
    }

    bool operator== (int x) {return val==x;}
    bool operator== (long long x) {return val==x;}
    bool operator== (rll b) {return val==b.val;}
};

rll dp[55], dpfact[55];

rll fact(long long n) {
    if(dpfact[n]==-1) {
        if(n<=1) return 1;
        dpfact[n] = n*fact(n-1);
    }
    return dpfact[n];
}

rll combinations(long long n) {
    if(n<3) return fact(1);
    return fact(n + (-2) );
}

vector<vector<long long> > allGoodPermutations;
long long rv[55];

void trans(long long num, long long n, int cnt=0) {

    if(cnt==n) {
        vector<long long> goodPermutation;
        for(int i=1; i<=n; i++) goodPermutation.push_back(rv[i]);
        allGoodPermutations.push_back(goodPermutation);
        return;
    }

    if(cnt==0) {
        rv[1] = num;
        trans(1, n, cnt+1);
        rv[1] = 0;
        return;
    }

    if(cnt==n-1) {
        rv[n] = num;
        trans(n, n, cnt+1);
        rv[n] = 0;
        return;
    }

    for(int i=2; i<n; i++) {
        if(rv[i]==0) {
            rv[i] = num;
            trans(i, n, cnt+1);
            rv[i] = 0;
        }
    }
}

rll rec(long long n) {
    if(dp[n]==-1) {
        if(n<=1) {
            dp[n] = 1;
        } else {
            dp[n] = combinations(n);
            for(long long i=1; i<n; i++) {
                dp[n] = dp[n] + combinations(i)*rec(n-i);
            }
        }
    }
    return dp[n];
}

vector<pair<long long, long long> > sol;
void analyze(rll n, rll k) {
    if(k==0) {
        assert(n==0); //no more number left
        return;
    }

    if(n==1 && k==1) {
        sol.push_back({1,1});
        return;
    }
    for(int i=1; i<=n; i++) {
        if(rec(n-i)*combinations(i) < k) {
            k = k - rec(n-i)*combinations(i);
        } else {
            long long restCombinations = rec(n-i);
            long long cycleNumber = (long long) k / restCombinations;
            if((restCombinations*cycleNumber)<k) {
                k = k - restCombinations*cycleNumber;
                cycleNumber++;
            } else {
                k = k - restCombinations*(cycleNumber-1);
            }

            sol.push_back({i, cycleNumber});
            analyze(n-i, k);

            break;
        }
    }
}

long long arr[55], mark[55];
vector<long long> solution;

bool isvalid(int n, int position=1, int cnt=0) {
    if(position==1) {
        if(cnt==n) return true;
        if(cnt!=0) return false;
    }
    if(cnt==n) return false;

    return isvalid(n, arr[position], cnt+1);
}

bool isConflict(long long pos, long long num) {
    if(mark[num]) return true;
    if(num==pos) return true;

    bool loop = false;
    long long tmp = num;
    while(arr[tmp] != 0) {
        tmp = arr[tmp];
        if(tmp==pos) loop = true;
    }

    return loop;
}

void kth_trans(long long n, long long k, long long start=1) {
    if(n==1) {
        solution.push_back(start);
        return;
    }
    if(n==2) {
        solution.push_back(start+1);
        solution.push_back(start);
        return;
    }
    if(n==3) {
        solution.push_back(start+2);
        solution.push_back(start);
        solution.push_back(start+1);
        return;
    }

    arr[1] = n;
    mark[n] = 1;
    solution.push_back(n+start-1);

    for(int i=2; i<=n-3; i++) {
        rll left = n-i-1;
        rll possibleCombinationsforEachNumber = fact(left);

        long long whichNumber = k/possibleCombinationsforEachNumber;
        if(whichNumber*possibleCombinationsforEachNumber < k) whichNumber++;

        k = k%possibleCombinationsforEachNumber;
        if(k==0) k = possibleCombinationsforEachNumber;

        for(int j=1; j<=n; j++) {
            if(isConflict(i,j)) continue;

            whichNumber--;
            if(whichNumber==0) {
                arr[i] = j;
                mark[j] = 1;
                break;
            }
        }
        solution.push_back(start+arr[i]-1);
    }

    vector<long long> unmarked;
    for(int i=1; i<=n; i++) {
        if(mark[i]) continue;
        unmarked.push_back(i);
    }

    for(int i=0; i<3; i++) {
        for(int j=0; j<3; j++) {
            if(i==j) continue;
            for(int k2=0; k2<3; k2++) {
                if(j==k2) continue;
                arr[n-2] = unmarked[i];
                arr[n-1] = unmarked[j];
                arr[n] = unmarked[k2];

                // cout << "bleh" << start << " " <<  k << endl;

                if(isvalid(n)) k--;
                if(k==0) {
                    solution.push_back(start+arr[n-2]-1);
                    solution.push_back(start+arr[n-1]-1);
                    solution.push_back(start+arr[n]-1);
                    return;
                }
            }
        }
    }
}

int main() {
    for(int i=0; i<55; i++) {
        dp[i] = -1;
        dpfact[i] = -1;
    }

    // int n;
    // cin >> n;
    // allGoodPermutations.clear();
    // trans(6, 6);
    // sort(allGoodPermutations.begin(), allGoodPermutations.end());

    // for(auto goodPermutation: allGoodPermutations) {
    //     for(auto x: goodPermutation) {
    //         cout << x << " ";
    //     }
    //     cout << endl;
    // }
    // // cout << "Good transformation count: " << rec(n) << endl;
    // cout << "Cycle Count: " << allGoodPermutations.size() << endl;
    // if(!allGoodPermutations.empty()) return 0;

    int t;
    cin >> t;

    while(t--) {
        long long n, k;
        cin >> n >> k;

        long long total_possible = rec(n);

        // trans(n, n);

        // cout << total_possible << endl;
        if(total_possible<k) {
            cout << -1 << endl;
            continue;
        }

        sol.clear();
        analyze(n, k);

        // for(auto s: sol) {
        //     cout << s.first << "," << s.second << " ---- ";
        // }
        // cout << endl;

        solution.clear();
        long long start = 1;
        for(auto s: sol) {
            memset(arr, 0, sizeof arr);
            memset(mark, 0, sizeof mark);

            kth_trans(s.first, s.second, start);
            start+=s.first;
        }

        for(auto x: solution) {
            cout << x << " ";
        }
        cout << endl;
    }
    return 0;
}