using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Constrained_Tree
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);
        private static Leaf[] leafs;

        private static void Main(string[] args)
        {
            int n = Next();
            int c = Next();

            var nnn = new point[c];
            for (int i = 0; i < c; i++)
            {
                nnn[i] = new point(Next(), Next(), Next());
                if (nnn[i].n >= nnn[i].m)
                {
                    writer.WriteLine("IMPOSSIBLE");
                    writer.Flush();
                    return;
                }
            }

            Array.Sort(nnn, nnn[0]);

            var nn = new List<point>(c) {nnn[0]};
            for (int i = 1; i < nnn.Length; i++)
            {
                if (nnn[i].Compare(nnn[i], nn[nn.Count - 1]) != 0)
                    nn.Add(nnn[i]);
            }

            leafs = new Leaf[n + 1];
            for (int i = 0; i < leafs.Length; i++)
            {
                leafs[i] = new Leaf {max = i, next = i + 1, parent = i, left = i};
            }
            leafs[n].next = -1;

            for (int i = nn.Count - 1; i >= 0; i--)
            {
                int maxl = -1;
                int maxr = -1;
                int minr = int.MaxValue;
                int leftl = -1;
                int rigthl = -1;
                int lastgood = i;
                for (int j = i; j >= 0; j--)
                {
                    if (nn[j].n != nn[i].n)
                        break;

                    int pi = Parent(nn[j].m);
                    if (nn[j].k > 0)
                    {
                        if (maxr < leafs[pi].max)
                        {
                            maxr = leafs[pi].max;
                            rigthl = leafs[pi].left;
                        }

                        minr = Math.Min(minr, pi);
                    }
                    else
                    {
                        if (maxl < leafs[pi].max)
                        {
                            maxl = leafs[pi].max;
                            leftl = leafs[pi].left;
                        }
                    }
                    lastgood = j;
                }

                for (; i >= lastgood; i--)
                {
                    int pi = Parent(nn[i].m);
                    if (pi != nn[i].n)
                        leafs[pi].parent = nn[i].n;
                }
                i++;

                if (minr <= maxl)
                {
                    writer.WriteLine("IMPOSSIBLE");
                    writer.Flush();
                    return;
                }

                leafs[nn[i].n].max = Math.Max(maxr, maxl);

                for (int j = nn[i].n + 1; j <= leafs[nn[i].n].max; j++)
                {
                    if (Parent(j) != nn[i].n)
                    {
                        leafs[j].parent = nn[i].n;
                    }
                    else
                    {
                        j = leafs[j].max;
                    }
                }

                if (maxl != -1)
                {
                    leafs[nn[i].n - 1].next = leafs[nn[i].n].next;
                    leafs[nn[i].n].next = leafs[leftl].next;
                    leafs[leftl].next = nn[i].n;
                }

                if (maxr != -1)
                    leafs[nn[i].n].left = rigthl;
            }

            int index = leafs[0].next;
            while (index != -1)
            {
                writer.Write(index);
                writer.Write(' ');

                index = leafs[index].next;
            }
            writer.Flush();
        }

        private static int Parent(int index)
        {
            if (leafs[index].parent == index)
                return index;
            return leafs[index].parent = Parent(leafs[index].parent);
        }

        private static int Next()
        {
            int c;
            do
            {
                c = reader.Read();
                if (c == 'L')
                    return -1;
                if (c == 'R')
                    return 1;
            } while (c < '0' || c > '9');
            int res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return res;
                res *= 10;
                res += c - '0';
            }
        }

        #region Nested type: Leaf

        private struct Leaf
        {
            public int left;
            public int max;
            public int next;
            public int parent;
        }

        #endregion

        #region Nested type: point

        private struct point : IComparer<point>
        {
            public readonly int k;
            public readonly int m;
            public readonly int n;

            public point(int n, int m, int k)
            {
                this.n = n;
                this.m = m;
                this.k = k;
            }

            #region IComparer<point> Members

            public int Compare(point x, point y)
            {
                int c = x.n.CompareTo(y.n);
                if (c == 0)
                {
                    c = x.k.CompareTo(y.k);
                    if (c == 0)
                        c = x.m.CompareTo(y.m);
                }
                return c;
            }

            #endregion
        }

        #endregion
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Constrained_Tree
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);
        private static Leaf[] leafs;

        private static void Main(string[] args)
        {
            int n = Next();
            int c = Next();

            var nnn = new point[c];
            for (int i = 0; i < c; i++)
            {
                nnn[i] = new point(Next(), Next(), Next());
                if (nnn[i].n >= nnn[i].m)
                {
                    writer.WriteLine("IMPOSSIBLE");
                    writer.Flush();
                    return;
                }
            }

            Array.Sort(nnn, nnn[0]);

            var nn = new List<point>();
            nn.Add(nnn[0]);
            for (int i = 1; i < nnn.Length; i++)
            {
                if (nnn[i].Compare(nnn[i], nn[nn.Count - 1]) != 0)
                    nn.Add(nnn[i]);
            }

            leafs = new Leaf[n + 1];
            for (int i = 0; i < leafs.Length; i++)
            {
                leafs[i] = new Leaf {max = i, next = i + 1, parent = i, left = i};
            }
            leafs[n].next = -1;

            for (int i = nn.Count - 1; i >= 0; i--)
            {
                int maxl = -1;
                int maxr = -1;
                int minr = int.MaxValue;
                int leftl = -1;
                int rigthl = -1;
                int lastgood = i;
                for (int j = i; j >= 0; j--)
                {
                    if (nn[j].n != nn[i].n)
                        break;

                    int pi = Parent(nn[j].m);
                    if (nn[j].k > 0)
                    {
                        if (maxr < leafs[pi].max)
                        {
                            maxr = leafs[pi].max;
                            rigthl = leafs[pi].left;
                        }

                        minr = Math.Min(minr, pi);
                    }
                    else
                    {
                        if (maxl < leafs[pi].max)
                        {
                            maxl = leafs[pi].max;
                            leftl = leafs[pi].left;
                        }
                    }
                    lastgood = j;
                }

                for (; i >= lastgood; i--)
                {
                    int pi = Parent(nn[i].m);
                    if (pi != nn[i].n)
                        leafs[pi].parent = nn[i].n;
                }
                i++;

                if (minr <= maxl)
                {
                    writer.WriteLine("IMPOSSIBLE");
                    writer.Flush();
                    return;
                }

                leafs[nn[i].n].max = Math.Max(maxr, maxl);

                for (int j = nn[i].n + 1; j <= leafs[nn[i].n].max; j++)
                {
                    if (Parent(j) != nn[i].n)
                    {
                        leafs[j].parent = nn[i].n;
                    }
                    else
                    {
                        j = leafs[j].max;
                    }
                }

                if (maxl != -1)
                {
                    leafs[nn[i].n - 1].next = leafs[nn[i].n].next;
                    leafs[nn[i].n].next = leafs[leftl].next;
                    leafs[leftl].next = nn[i].n;
                }

                if (maxr != -1)
                    leafs[nn[i].n].left = rigthl;
            }

            int index = leafs[0].next;
            while (index != -1)
            {
                writer.Write(index);
                writer.Write(' ');

                index = leafs[index].next;
            }
            writer.Flush();
        }

        private static int Parent(int index)
        {
            if (leafs[index].parent == index)
                return index;
            return leafs[index].parent = Parent(leafs[index].parent);
        }

        private static int Next()
        {
            int c;
            int res = 0;
            do
            {
                c = reader.Read();
                if (c == 'L')
                    return -1;
                if (c == 'R')
                    return 1;
                if (c == -1)
                    return res;
            } while (c < '0' || c > '9');
            res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return res;
                res *= 10;
                res += c - '0';
            }
        }

        #region Nested type: Leaf

        private struct Leaf
        {
            public int left;
            public int max;
            public int next;
            public int parent;
        }

        #endregion

        #region Nested type: point

        private class point : IComparer<point>
        {
            public readonly int k;
            public readonly int m;
            public readonly int n;

            public point(int n, int m, int k)
            {
                this.n = n;
                this.m = m;
                this.k = k;
            }

            #region IComparer<point> Members

            public int Compare(point x, point y)
            {
                int c = x.n.CompareTo(y.n);
                if (c == 0)
                {
                    c = x.k.CompareTo(y.k);
                    if (c == 0)
                        c = x.m.CompareTo(y.m);
                }
                return c;
            }

            #endregion
        }

        #endregion
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Constrained_Tree
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);
        private static Leaf[] leafs;

        private static void Main(string[] args)
        {
            int n = Next();
            int c = Next();

            var nnn = new point[c];
            for (int i = 0; i < c; i++)
            {
                nnn[i] = new point(Next(), Next(), Next());
                if (nnn[i].n >= nnn[i].m)
                {
                    writer.WriteLine("IMPOSSIBLE");
                    writer.Flush();
                    return;
                }
            }

            Array.Sort(nnn, nnn[0]);

            var nn = new List<point>(c) {nnn[0]};
            for (int i = 1; i < nnn.Length; i++)
            {
                if (nnn[i].Compare(nnn[i], nn[nn.Count - 1]) != 0)
                    nn.Add(nnn[i]);
            }

            leafs = new Leaf[n + 1];
            for (int i = 0; i < leafs.Length; i++)
            {
                leafs[i] = new Leaf {max = i, next = i + 1, parent = i, left = i};
            }
            leafs[n].next = -1;

            for (int i = nn.Count - 1; i >= 0; i--)
            {
                int maxl = -1;
                int maxr = -1;
                int minr = int.MaxValue;
                int leftl = -1;
                int rigthl = -1;
                int lastgood = i;
                for (int j = i; j >= 0; j--)
                {
                    if (nn[j].n != nn[i].n)
                        break;

                    int pi = Parent(nn[j].m);
                    if (nn[j].k > 0)
                    {
                        if (maxr < leafs[pi].max)
                        {
                            maxr = leafs[pi].max;
                            rigthl = leafs[pi].left;
                        }

                        minr = Math.Min(minr, pi);
                    }
                    else
                    {
                        if (maxl < leafs[pi].max)
                        {
                            maxl = leafs[pi].max;
                            leftl = leafs[pi].left;
                        }
                    }
                    lastgood = j;
                }

                for (; i >= lastgood; i--)
                {
                    int pi = Parent(nn[i].m);
                    if (pi != nn[i].n)
                        leafs[pi].parent = nn[i].n;
                }
                i++;

                if (minr <= maxl)
                {
                    writer.WriteLine("IMPOSSIBLE");
                    writer.Flush();
                    return;
                }

                leafs[nn[i].n].max = Math.Max(maxr, maxl);

                for (int j = nn[i].n + 1; j <= leafs[nn[i].n].max; j++)
                {
                    if (Parent(j) != nn[i].n)
                    {
                        leafs[j].parent = nn[i].n;
                    }
                    else
                    {
                        j = leafs[j].max;
                    }
                }

                if (maxl != -1)
                {
                    leafs[nn[i].n - 1].next = leafs[nn[i].n].next;
                    leafs[nn[i].n].next = leafs[leftl].next;
                    leafs[leftl].next = nn[i].n;
                }

                if (maxr != -1)
                    leafs[nn[i].n].left = rigthl;
            }

            int index = leafs[0].next;
            while (index != -1)
            {
                writer.Write(index);
                writer.Write(' ');

                index = leafs[index].next;
            }
            writer.Flush();
        }

        private static int Parent(int index)
        {
            if (leafs[index].parent == index)
                return index;
            return leafs[index].parent = Parent(leafs[index].parent);
        }

        private static int Next()
        {
            int c;
            do
            {
                c = reader.Read();
                if (c == 'L')
                    return -1;
                if (c == 'R')
                    return 1;
            } while (c < '0' || c > '9');
            int res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return res;
                res *= 10;
                res += c - '0';
            }
        }

        #region Nested type: Leaf

        private struct Leaf
        {
            public int left;
            public int max;
            public int next;
            public int parent;
        }

        #endregion

        #region Nested type: point

        private class point : IComparer<point>
        {
            public readonly int k;
            public readonly int m;
            public readonly int n;

            public point(int n, int m, int k)
            {
                this.n = n;
                this.m = m;
                this.k = k;
            }

            #region IComparer<point> Members

            public int Compare(point x, point y)
            {
                int c = x.n.CompareTo(y.n);
                if (c == 0)
                {
                    c = x.k.CompareTo(y.k);
                    if (c == 0)
                        c = x.m.CompareTo(y.m);
                }
                return c;
            }

            #endregion
        }

        #endregion
    }
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define INF 1000000000
#define pb push_back
#define itr iterator
#define sz size()
#define mp make_pair

int maxl[1100000];
int minr[1100000];
int maxr[1100000];
vector<int> ans;
int n,c;

int build(int root, int maxChild) {
    int maxLeft = root;
    if (maxl[root] > root) {
        maxLeft = build(root+1, maxl[root]);
    }

    ans.push_back(root);

    if (maxLeft >= minr[root]) {
        printf("IMPOSSIBLE\n");
        exit(0);
    }

    int maxRight = max(maxLeft, max(maxr[root], maxChild));
    if (maxRight > maxLeft) {
        maxRight = build(maxLeft+1, maxRight);
    }
    return maxRight;
}

int main() {

    scanf("%d %d", &n, &c);
    for (int i = 0; i <= n; i++) {
        maxl[i] = maxr[i] = -1;
        minr[i] = 1000000000;
    }

    for (int i = 0; i < c; i++) {
        int a, b;
        char loc[100];
        scanf("%d %d %s", &a, &b, loc);

        if (b <= a) {
            printf("IMPOSSIBLE\n");
            return 0;
        }

        if (loc[0] == 'L') {
            maxl[a] = max(b, maxl[a]);
        }
        else {
            minr[a] = min(b, minr[a]);
            maxr[a] = max(b, maxr[a]);
        }
    }

    build(1, n);

    for (int i = 0; i < n; i++) printf("%d ", ans[i]);
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define INF 1000000000
#define pb push_back
#define itr iterator
#define sz size()
#define mp make_pair

int maxl[1100000];
int minr[1100000];
int maxr[1100000];
vector<int> ans;
int n,c;

int build(int root, int maxChild) {
    //printf("build %d\n", root);
    int maxLeft = root;
    if (maxl[root] > root) {
        //printf("build left\n");
        maxLeft = build(root+1, maxl[root]);
    }

    ans.push_back(root);

    //printf("root %d maxL %d\n", root, maxLeft);

    if (maxLeft >= minr[root]) {
        printf("IMPOSSIBLE\n");
        exit(0);
    }

    int maxRight = max(root, max(maxr[root], maxChild));
    if (maxRight > root) {
        //printf("build right\n");
        maxRight = build(maxLeft+1, maxRight);
    }
    return maxRight;
}

int main() {

    scanf("%d %d", &n, &c);
    for (int i = 0; i <= n; i++) {
        maxl[i] = maxr[i] = -1;
        minr[i] = 1000000000;
    }

    for (int i = 0; i < c; i++) {
        int a, b;
        char loc[100];
        scanf("%d %d %s", &a, &b, loc);

        if (b < a) {
            printf("IMPOSSIBLE\n");
            return 0;
        }

        if (loc[0] == 'L') {
            maxl[a] = max(b, maxl[a]);
        }
        else {
            minr[a] = min(b, minr[a]);
            maxr[a] = max(b, maxr[a]);
        }
    }

    build(1, n);

    for (int i = 0; i < n; i++) printf("%d ", ans[i]);
}


#include <bits/stdc++.h>
#include <cassert>
using namespace std;
typedef long long ll; typedef long double ld;
#define rep(i,s,e) for (int i=(s),__ee=(e);i<__ee;++i)
#define all(x) begin(x),end(x)
#define clr(x,y) memset(x,y,sizeof x)
#define contains(x,y) ((x).find(y)!=end(x))
#define pb push_back
#define Tu tuple
#define mk make_pair
#define mkt make_tuple
#define fst first
#define snd second
#define _1 get<0>
#define _2 get<1>
#define _3 get<2>
#define _4 get<3>
#define sz(x) ((int)(x).size())
#define tn typename
#define umap unordered_map
#define uset unordered_map
template <tn T> T read() { T x; cin >> x; return x; }
int dx[]={0,0,1,-1,1,-1,1,-1}, dy[]={-1,1,0,0,1,-1,-1,1};
void run();
int main() {
#ifdef LOCAL
#  define dbg(s, ...) printf(s "\n", __VA_ARGS__)
#else
#  define endl "\n"
#  define dbg(s,...)
#  define FILE "x"
	//freopen(FILE ".in", "r", stdin), freopen(FILE ".out", "w", stdout);
	ios::sync_with_stdio(0);
#endif
	cout << fixed << setprecision(16);
	run();
}

int n, c;
vector<tuple<int,int,int>> cs;
int nxt[1002003][2];

int res[1002003], rev[1002003], pos=1;
void fail() {
	cout << "IMPOSSIBLE" << endl;
	exit(0);
}

int reconstruct(int x, int y, int ind=0) {
	//rep(_,0,ind) cout << "  "; cout << "reconstruct " << x << " " << y << endl;
	assert(x <= y);
	//if (x == y) {
		//res[pos++] = x;
		//return x + 1;
	//}
	int mid = x + 1;
	if (nxt[x][0] > x)
		mid = reconstruct(x + 1, nxt[x][0], ind+1);
	res[pos++] = x;
	int r = max(y, nxt[x][1]);
	if (r >= mid)
		return reconstruct(mid, r, ind+1);
	else
		return mid;
}

void run() {
	cin >> n >> c;
	while(c--) {
		string s;
		int a, b; cin >> a >> b >> s;
		if (a >= b) fail();
		int t = s == "RIGHT";
		cs.pb(mkt(a, b, t));
		nxt[a][t] = max(nxt[a][t], b);
	}
	reconstruct(1, n);
	rep(i,1,n+1) rev[res[i]] = i;
	//rep(i,1,n+1) cerr << res[i] << " "; cerr << endl;
	for (auto& c: cs) {
		int x = get<0>(c), y = get<1>(c), t = get<2>(c);
		if (t == 0 && rev[y] >= rev[x] || t == 1 && rev[y] <= rev[x]) {
			//cerr << x << " " << y << " " << t << endl;
			fail();
		}
	}
	rep(i,1,n+1) cout << res[i] << " "; cout << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,s,e) for (int i=(s),__ee=(e);i<__ee;++i)
int n, c, res[1002003], rev[1002003], pos=1, nxt[1002003][2];
vector<tuple<int,int,int>> cs;
void fail() {
	cout << "IMPOSSIBLE" << endl;
	exit(0);
}
int reconstruct(int x, int y) {
	int i = x + 1;
	if (nxt[x][0] > x)
		i = reconstruct(x + 1, nxt[x][0]);
	res[pos++] = x;
	int r = max(y, nxt[x][1]);
	if (r >= i) i = reconstruct(i, r);
	return i;
}
int main() {
	cin >> n >> c;
	while(c--) {
		string s;
		int a, b; cin >> a >> b >> s;
		if (a >= b) fail();
		int t = s == "RIGHT";
		cs.emplace_back(a, b, t);
		nxt[a][t] = max(nxt[a][t], b);
	}
	reconstruct(1, n);
	rep(i,1,n+1) rev[res[i]] = i;
	for (auto& c: cs) {
		int x = get<0>(c), y = get<1>(c), t = get<2>(c);
		if (t == 0 && rev[y] >= rev[x] || t == 1 && rev[y] <= rev[x])
			fail();
	}
	rep(i,1,n+1) cout << res[i] << " "; cout << endl;
}


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

typedef pair<int, int> pii;

int n, c;
int l[1000010], r[1000010];
vector<pii> ancestor[1000010];

pair<int, int> tree[1000010];
int cur;
int build(int lim) {
    if (cur >= lim) return 0;

    int ret = ++cur;
    if (l[ret])
        tree[ret].first = build(l[ret]);
    if (max(r[ret], lim))
        tree[ret].second = build(max(r[ret], lim));
    return ret;
}

int dir[1000010];
bool check(int i) {
    if (!i) return true;

    bool ret = true;
    for (auto p: ancestor[i])
        ret &= dir[p.first] == p.second;

    dir[i] = 0;
    ret &= check(tree[i].first);
    dir[i] = 1;
    ret &= check(tree[i].second);
    dir[i] = -1;

    return ret;
}

void print(int i) {
    if (!i) return;
    print(tree[i].first);
    printf("%d ", i);
    print(tree[i].second);
}

int main() {
    // freopen("D.txt", "r", stdin);

    scanf("%d%d", &n, &c);
    for (int i = 1; i <= c; i++) {
        int x, y;
        char d[10];
        scanf("%d%d%s", &x, &y, d);

        if (x >= y) {
            printf("IMPOSSIBLE\n");
            return 0;
        }

        if (d[0] == 'L') {
            l[x] = max(l[x], y);
            ancestor[y].push_back({x, 0});
        } else {
            r[x] = max(r[x], y);
            ancestor[y].push_back({x, 1});
        }
    }

    cur = 0;
    build(n);

    if (!check(1)) {
        printf("IMPOSSIBLE\n");
        return 0;
    } else print(1);

    return 0;
}


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

const int MAXN = 1000005;
int N, C, A, B;
int L[MAXN], R[MAXN], Rmin[MAXN];
string S;
vector<int> res;

int dfs(int v, int r){
  if(L[v] >= 0){
    L[v] = max(dfs(v + 1, L[v]), L[v]);
  }
  L[v] = max(L[v], v);
  if(L[v] >= Rmin[v]){
    cout << "IMPOSSIBLE\n";
    exit(0);
  }
  res.push_back(v);
  R[v] = max(R[v], r);
  if(R[v] > L[v]){
    return dfs(L[v] + 1, R[v]);
  }
  return L[v];
}

int main(){
  ios::sync_with_stdio(0);
  cin >> N >> C;
  memset(L, -1, sizeof(L));
  memset(R, -1, sizeof(R));
  memset(Rmin, 63, sizeof(Rmin));
  for(int i = 0; i < C; i++){
    cin >> A >> B >> S;
    A -= 1, B -= 1;
    if(A >= B){
      cout << "IMPOSSIBLE\n";
      return 0;
    }
    if(S == "LEFT"){
      L[A] = max(L[A], B);
    } else {
      R[A] = max(R[A], B);
      Rmin[A] = min(Rmin[A], B);
    }
  }
  dfs(0, N - 1);
  for(int v : res){
    cout << v + 1 << ' ';
  }
  cout << '\n';
}


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

const int MAXN = 105;
int N, K, L[MAXN][2], R[MAXN][2];

int main(){
  cin >> N >> K;
  for(int i = 0; i < N; i++){
    L[i][1] = N;
  }
  for(int i = 0; i < K; i++){
    int a, b;
    string s;
    cin >> a >> b >> s;
    if(a >= b){
      cout << "IMPOSSIBLE\n";
      return 0;
    }
    a--, b--;
    if(s == "LEFT"){
      L[a][0] = max(L[a][0], b);
    } else {
      L[a][1] = min(L[a][1], b - 1);
      R[a][0] = max(R[a][0], b);
    }
  }
  for(int i = N - 1; i >= 0; i--){
    for(int j = i + 1; j <= L[i][0]; j++){
      L[i][0] = max(L[i][0], max(L[j][0], R[j][0]));
    }
    if(L[i][0] > L[i][1]){
      cout << "IMPOSSIBLE\n";
      return 0;
    }
  }
  vector<int> V;
  for(int i = 0; i < N; i++){
    V.push_back(i);
    while(V.size() && i >= L[V.back()][0]){
      cout << V.back() + 1 << " ";
      V.pop_back();
    }
  }
  cout << '\n';
}


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

const int MAXN = 1000005;
int N, K, L[MAXN][2], R[MAXN][2];

int main(){
  cin >> N >> K;
  for(int i = 0; i < N; i++){
    L[i][1] = N;
  }
  for(int i = 0; i < K; i++){
    int a, b;
    string s;
    cin >> a >> b >> s;
    if(a >= b){
      cout << "IMPOSSIBLE\n";
      return 0;
    }
    a--, b--;
    if(s == "LEFT"){
      L[a][0] = max(L[a][0], b);
    } else {
      L[a][1] = min(L[a][1], b - 1);
      R[a][0] = max(R[a][0], b);
    }
  }
  for(int i = N - 1; i >= 0; i--){
    for(int j = i + 1; j <= L[i][0]; j++){
      L[i][0] = max(L[i][0], max(L[j][0], R[j][0]));
    }
    if(L[i][0] > L[i][1]){
      cout << "IMPOSSIBLE\n";
      return 0;
    }
  }
  vector<int> V;
  for(int i = 0; i < N; i++){
    V.push_back(i);
    while(V.size() && i >= L[V.back()][0]){
      cout << V.back() + 1 << " ";
      V.pop_back();
    }
  }
  cout << '\n';
}


#include<bits/stdc++.h>
using namespace std;
int n,m,pos,x,y,i,nid,r[1000010];
vector<int> g[1000010][2];
char s[111];
bool q=true;
void dfs(int x) {
    int i=++nid;
    if (g[i][0].size()) {
        sort(g[i][0].begin(),g[i][0].end());
        if (g[i][0][0]<=nid) { q=false;return;}
        dfs(g[i][0].back());
        if (!q) return;
    }
    r[pos++]=i;
    if (g[i][1].size()) {
        sort(g[i][1].begin(),g[i][1].end());
        if (g[i][1][0]<=nid) { q=false;return;}
        dfs(max(x,g[i][1].back()));
    } else if (x>nid) dfs(x);
}
int main() {
    scanf("%d%d",&n,&m);
    for (i=0;i<m;i++){
        scanf("%d%d%s",&x,&y,s),g[x][int(s[0]=='R')].push_back(y);
        if(x>=y)q=0;
    }
    if(q)dfs(n);
    if(q){
        for (i=0;i<pos;i++) printf("%d%c",r[i],(i==pos-1)?'\n':' ');
    } else puts("IMPOSSIBLE");
    return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
int n, w[60], vis[60], P[60], UF[60];
long long D[60], INF = 1e18+123, F[60];
int Find(int a) {
	if (a == UF[a])return a;
	return UF[a] = Find(UF[a]);
}
long long Calc(int m) {
	int i, cnt = 0, j;
	for (i = 1; i <= n; i++)vis[i] = 0;
	for (i = 1; i <= m; i++) {
		if (vis[w[i]])return 0ll;
		vis[w[i]] = 1;
		if (w[P[cnt]] < w[i]) {
			P[++cnt] = i;
		}
	}
	for (i = 1; i < cnt; i++) {
		if (w[P[i]] != P[i + 1] - 1)return 0ll;
	}
	for (i = 1; i <= n; i++)vis[i] = 0;
	for (i = 1; i < P[cnt]; i++) vis[w[i]] = 1;
	for (i = 1; i < P[cnt]; i++)if (!vis[i])return 0ll;
	int b = P[cnt], e = w[P[cnt]];
	for (i = 1; i <= n; i++)vis[i] = 0;
	if (e == m) return D[n - m];

	if (Find(m) == Find(w[m]))return 0ll;

	long long z1 = F[e - m - 1];
	long long z2 = D[n - e];
	if (z1 > INF / z2)return INF;
	return z1 * z2;
}
void Solve() {
	int i, j;
	long long K;
	scanf("%d%lld", &n, &K);
	if (D[n] < K) {
		puts("-1");
		return;
	}
	for (i = 1; i <= n; i++)w[i] = 0, UF[i] = i;
	for (i = 1; i <= n; i++) {
		for (j = 1; j <= n; j++) {
			w[i] = j;
			long long t = Calc(i);
			if (K > t) {
				K -= t;
				w[i] = 0;
			}
			else break;
		}
		int a = Find(i), b = Find(w[i]);
		UF[a] = b;
	}
	for (i = 1; i <= n; i++)printf("%d ", w[i]);
	puts("");
}
int main() {
	int i, j, TC, k;
	D[0] = 1;
	F[0] = 1;
	for (i = 1; i <= 50; i++) {
		if (F[i - 1] > INF / i) F[i] = INF;
		else F[i] = F[i - 1] * i;
		for (j = 0; j < i; j++) {
			long long t = D[j];
			for (k = 1; k <= i - j - 2; k++) {
				if (t > INF / k)t = INF;
				else t *= k;
			}
			D[i] = min(D[i] + t,INF);
		}
	}
	scanf("%d", &TC);
	while (TC--) {
		Solve();
	}
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
const int maxn=50+10;
int cc,p[maxn],last[maxn],first[maxn];
ll dp_cycles[maxn],dp_ways[maxn],factorial[maxn];
ll fact(int x){

    if(x<=1)return 1;
    return factorial[x]=fact(x-1)*x;
}
ll cycles(int x){

    if(x<=3)return 1;
    if(dp_cycles[x]!=-1)return dp_cycles[x];

    return dp_cycles[x]=cycles(x-1)*(x-2);
}
ll ways(int x){

    if(x==0)return 1;
    if(dp_ways[x]!=-1)return dp_ways[x];

    dp_ways[x]=0;
    for(int i=1;i<=x;i++)
        dp_ways[x]+=cycles(i)*ways(x-i);

    return dp_ways[x];
}
void reset_dsu(int n){

    cc=n;
    for(int i=1;i<=n;i++){
        p[i]=i;
        first[i]=1;
        last[i]=1;
    }

}
int root(int x){
    if(p[x]==x)return x;
    return root(p[x]);
}
void join(int x,int y){

    p[x]=y;
    last[y]=0;
    first[x]=0;
    cc--;
}
ll check_join(int x,int y){

    if(first[x]==0 || last[y]==0)return 0;

    return fact(cc-1);
}
void construct_cycle(ll k,int n,vector<int> &vect){

    reset_dsu(n);

    vect.resize(n);
    vect[0]=n;

    join(1,n);

    for(int i=1;i<n;i++){

        for(int j=1;j<=n;j++){

            ll pom=check_join(i+1,j);

            if(pom<k)k-=pom;
            else{
                vect[i]=j;
                join(i+1,j);
                break;
            }
        }

    }
}
void go(int start,int n,ll k,vector<int> &rez){

    if(n==0)return;

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

        ///printf("%lld %lld %lld %d AAA\n",k,ways(n-i),cycles(i),i);

        if(ways(n-i)*cycles(i)<k)k-=ways(n-i)*cycles(i);
        else{
            block_size=i;
            break;
        }
    }
    if(block_size==-1){
        rez.pb(-1);
        return;
    }

    ll which_cycle=k/ways(n-block_size)+1-(k%ways(n-block_size)==0);

    k%=ways(n-block_size);
    if(k==0)k=ways(n-block_size);

    ///printf("%lld %lld %d HEHEH\n",which_cycle,k,block_size);

    vector<int>vect;
    construct_cycle(which_cycle,block_size,vect);

    for(int i=0;i<vect.size();i++){
        vect[i]+=start-1;
        rez.pb(vect[i]);
    }

    go(start+block_size,n-block_size,k,rez);
}
int main(){

    ///freopen("test.txt","r",stdin);

    memset(factorial,-1,sizeof(factorial));
    memset(dp_ways,-1,sizeof(dp_ways));
    memset(dp_cycles,-1,sizeof(dp_cycles));

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

        vector<int>rez;
        go(1,n,k,rez);

        for(int i=0;i<rez.size();i++)printf("%d ",rez[i]);
        printf("\n");
    }

return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
const int maxn=50+10;
int cc,p[maxn],last[maxn],first[maxn];
ll dp_cycles[maxn],dp_ways[maxn],factorial[maxn];
ll fact(int x){

    if(x<=1)return 1;
    return factorial[x]=fact(x-1)*x;
}
ll cycles(int x){

    if(x<=3)return 1;
    if(dp_cycles[x]!=-1)return dp_cycles[x];

    return dp_cycles[x]=cycles(x-1)*(x-2);
}
ll ways(int x){

    if(x==0)return 1;
    if(dp_ways[x]!=-1)return dp_ways[x];

    dp_ways[x]=0;
    for(int i=1;i<=x;i++)
        dp_ways[x]+=cycles(i)*ways(x-i);

    return dp_ways[x];
}
void reset_dsu(int n){

    cc=n;
    for(int i=1;i<=n;i++){
        p[i]=i;
        first[i]=1;
        last[i]=1;
    }

}
int root(int x){
    if(p[x]==x)return x;
    return root(p[x]);
}
void join(int x,int y){

    p[x]=y;
    last[y]=0;
    first[x]=0;
    cc--;
}
ll check_join(int x,int y){

    ///printf("%d %d %d %d CHECK\n",x,y,first[x],last[y]);
    if(first[x]==0 || last[y]==0)return 0;

    if(root(x)==root(y))return 0;

    return fact(cc-2);
}
void construct_cycle(ll k,int n,vector<int> &vect){

    reset_dsu(n);

    vect.resize(n);
    vect[0]=n;

    join(1,n);

    for(int i=1;i<n;i++){

        ///printf(" GOOTOOOOVVVV\n\n");

        if(i==n-1){

            for(int j=1;j<=n;j++)
            if(last[j]){
                vect[i]=j;
                break;
            }

            break;
        }

        for(int j=1;j<=n;j++){

            ll pom=check_join(i+1,j);

            ///printf("%d %d %d %d  ROOTS\n %lld %d %d %d hehe\n",root(1),root(2),root(3),root(4),pom,cc,i,j);

            if(pom<k)k-=pom;
            else{
                vect[i]=j;
                join(i+1,j);
                break;
            }
        }

    }
}
void go(int start,int n,ll k,vector<int> &rez){

    if(n==0)return;

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

        ///printf("%lld %lld %lld %d AAA\n",k,ways(n-i),cycles(i),i);

        if(ways(n-i)*cycles(i)<k)k-=ways(n-i)*cycles(i);
        else{
            block_size=i;
            break;
        }
    }
    if(block_size==-1){
        rez.pb(-1);
        return;
    }

    ll which_cycle=k/ways(n-block_size)+1-(k%ways(n-block_size)==0);

    k%=ways(n-block_size);
    if(k==0)k=ways(n-block_size);

    ///printf("%lld %lld %d HEHEH\n",which_cycle,k,block_size);

    vector<int>vect;
    construct_cycle(which_cycle,block_size,vect);

    for(int i=0;i<vect.size();i++){
        vect[i]+=start-1;
        rez.pb(vect[i]);
    }

    go(start+block_size,n-block_size,k,rez);
}
int main(){

    ///freopen("test.txt","r",stdin);

    memset(factorial,-1,sizeof(factorial));
    memset(dp_ways,-1,sizeof(dp_ways));
    memset(dp_cycles,-1,sizeof(dp_cycles));

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

        vector<int>rez;
        go(1,n,k,rez);

        for(int i=0;i<rez.size();i++)printf("%d ",rez[i]);
        printf("\n");
    }

return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
const int maxn=50+10;
int cc,p[maxn],last[maxn],first[maxn];
ll dp_cycles[maxn],dp_ways[maxn],factorial[maxn],inf=1e18+5;
ll mul(ll x,ll y){

    ll ret=inf;
    ret/=x;
    ret/=y;
    if(ret==0)return inf;

return x*y;
}
ll add(ll x,ll y){

    if(x+y>inf)return inf;
    return x+y;
}
ll fact(int x){

    if(x<=1)return 1;
    return factorial[x]=mul(fact(x-1),x);
}
ll cycles(int x){

    if(x<=3)return 1;
    if(dp_cycles[x]!=-1)return dp_cycles[x];

    return dp_cycles[x]=mul(cycles(x-1),(x-2));
}
ll ways(int x){

    if(x==0)return 1;
    if(dp_ways[x]!=-1)return dp_ways[x];

    dp_ways[x]=0;
    for(int i=1;i<=x;i++)
        dp_ways[x]=add(dp_ways[x],mul(cycles(i),ways(x-i)));

    return dp_ways[x];
}
void reset_dsu(int n){

    cc=n;
    for(int i=1;i<=n;i++){
        p[i]=i;
        first[i]=1;
        last[i]=1;
    }

}
int root(int x){
    if(p[x]==x)return x;
    return root(p[x]);
}
void join(int x,int y){

    p[x]=y;
    last[y]=0;
    first[x]=0;
    cc--;
}
ll check_join(int x,int y){

    if(first[x]==0 || last[y]==0)return 0;

    if(root(x)==root(y))return 0;

    return fact(cc-2);
}
void construct_cycle(ll k,int n,vector<int> &vect){

    reset_dsu(n);

    vect.resize(n);
    vect[0]=n;

    join(1,n);

    for(int i=1;i<n;i++){

        if(i==n-1){

            for(int j=1;j<=n;j++)
            if(last[j]){
                vect[i]=j;
                break;
            }

            break;
        }

        for(int j=1;j<=n;j++){

            ll pom=check_join(i+1,j);

            if(pom<k)k-=pom;
            else{
                vect[i]=j;
                join(i+1,j);
                break;
            }
        }

    }
}
void go(int start,int n,ll k,vector<int> &rez){

    if(n==0)return;

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

        if(mul(ways(n-i),cycles(i))<k)k-=ways(n-i)*cycles(i);
        else{
            block_size=i;
            break;
        }
    }
    if(block_size==-1){
        rez.pb(-1);
        return;
    }

    ll which_cycle=k/ways(n-block_size)+1-(k%ways(n-block_size)==0);

    k%=ways(n-block_size);
    if(k==0)k=ways(n-block_size);

    vector<int>vect;
    construct_cycle(which_cycle,block_size,vect);

    for(int i=0;i<vect.size();i++){
        vect[i]+=start-1;
        rez.pb(vect[i]);
    }

    go(start+block_size,n-block_size,k,rez);
}
int main(){

    ///freopen("test.txt","r",stdin);

    memset(factorial,-1,sizeof(factorial));
    memset(dp_ways,-1,sizeof(dp_ways));
    memset(dp_cycles,-1,sizeof(dp_cycles));

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

        vector<int>rez;
        go(1,n,k,rez);

        for(int i=0;i<rez.size();i++)printf("%d ",rez[i]);
        printf("\n");
    }

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define ll long long
#define pb push_back
typedef pair<int,int> pii;

int main(){

return 0;
}


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

using ll = long long;

const long double INF = 1e18;

long double fact[51];
long double dp[51];

vector<int> solve(int N, ll K) {
    if (N == 0) return {};
    long double sum = 0;
    int x;
    for (x = 1; x <= N; x++) {
        if (x == 1) {
            if (dp[N - 1] >= K) break;
            sum += dp[N - 1];
            continue;
        }
        long double tmp;
        if (fact[x - 2] == INF || dp[N - x] == INF)
            tmp = INF;
        else
            tmp = min(fact[x - 2] * dp[N - x], INF);
        if (sum + tmp >= K) break;
        sum += tmp;
    }
    vector<int> ans;
    vector<bool> used(x + 1);
    if (x > 1) {
        used[x] = 1;
        ans.push_back(x);
    }
    used[1] = 1;
    ans.push_back(1);
    for (int i = 3; i <= x; i++) {
        long double tmp;
        if (fact[x - i] == INF || dp[N - x] == INF)
            tmp = INF;
        else
            tmp = min(fact[x - i] * dp[N - x], INF);
        int j;
        for (j = 1; j <= x; j++) {
            if (used[j]) continue;
            if (sum + tmp >= K) break;
            sum += tmp;
        }
        used[j] = 1;
        ans.push_back(j);
    }
    vector<int> ret = solve(N - x, K - sum);
    for (int i = 0; i < ret.size(); i++) ans.push_back(ret[i] + x);
    return ans;
}

int main() {
    fact[0] = 1;
    for (int i = 1; i <= 50; i++) {
        if (fact[i - 1] == INF)
            fact[i] = INF;
        else
            fact[i] = min(fact[i - 1] * i, INF);
    }
    dp[0] = dp[1] = 1;
    for (int i = 2; i <= 50; i++) {
        ll sum = 0;
        if (dp[i - 1] == INF)
            sum = INF;
        else
            sum += dp[i - 1];
        for (int j = 2; j <= i; j++) {
            if (fact[j - 2] == INF || dp[i - j] == INF) {
                sum = INF;
                break;
            }
            long double tmp = min(fact[j - 2] * dp[i - j], INF);
            if (tmp == INF) {
                sum = INF;
                break;
            }
            sum = min(sum + tmp, INF);
        }
        dp[i] = sum;
    }

    int T;
    cin >> T;
    while (T--) {
        int n;
        ll k;
        cin >> n >> k;
        if (dp[n] < k) {
            cout << -1 << endl;
            continue;
        }
        vector<int> ans = solve(n, k);
        for (int i = 0; i < ans.size(); i++) cout << ans[i] << " ";
        cout << endl;
    }
}


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

using ll = long long;

const long double INF = 1e18;

long double fact[51];
long double dp[51];

vector<int> solve(int N, ll K) {
    if (N == 0) return {};
    long double sum = 0;
    int x;
    for (x = 1; x <= N; x++) {
        if (x == 1) {
            if (dp[N - 1] >= K) break;
            sum += dp[N - 1];
            continue;
        }
        long double tmp;
        if (fact[x - 2] == INF || dp[N - x] == INF)
            tmp = INF;
        else
            tmp = min(fact[x - 2] * dp[N - x], INF);
        if (sum + tmp >= K) break;
        sum += tmp;
    }
    vector<int> ans;
    vector<bool> used(x + 1);
    vector<int> to(x + 1, -1);
    to[1] = x;
    used[x] = 1;
    for (int i = 2; i < x; i++) {
        long double tmp;
        if (fact[x - i - 1] == INF || dp[N - x] == INF)
            tmp = INF;
        else
            tmp = min(fact[x - i - 1] * dp[N - x], INF);
        int j;
        for (j = 1; j < x; j++) {
            if (used[j]) continue;
            int now = j;
            while (to[now] != -1) now = to[now];
            if (now == i) continue;
            if (sum + tmp >= K) break;
            sum += tmp;
        }
        to[i] = j;
        used[j] = 1;
    }
    for (int i = 1; i < x; i++)
        if (!used[i]) to[x] = i;
    for (int i = 1; i <= x; i++) ans.push_back(to[i]);
    vector<int> ret = solve(N - x, K - sum);
    for (int i = 0; i < ret.size(); i++) ans.push_back(ret[i] + x);
    return ans;
}

int main() {
    fact[0] = 1;
    for (int i = 1; i <= 50; i++) {
        if (fact[i - 1] == INF)
            fact[i] = INF;
        else
            fact[i] = min(fact[i - 1] * i, INF);
    }
    dp[0] = dp[1] = 1;
    for (int i = 2; i <= 50; i++) {
        ll sum = 0;
        if (dp[i - 1] == INF)
            sum = INF;
        else
            sum += dp[i - 1];
        for (int j = 2; j <= i; j++) {
            if (fact[j - 2] == INF || dp[i - j] == INF) {
                sum = INF;
                break;
            }
            long double tmp = min(fact[j - 2] * dp[i - j], INF);
            if (tmp == INF) {
                sum = INF;
                break;
            }
            sum = min(sum + tmp, INF);
        }
        dp[i] = sum;
    }

    int T;
    cin >> T;
    while (T--) {
        int n;
        ll k;
        cin >> n >> k;
        if (dp[n] < k) {
            cout << -1 << endl;
            continue;
        }
        vector<int> ans = solve(n, k);
        for (int i = 0; i < ans.size(); i++) cout << ans[i] << " ";
        cout << endl;
    }
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define x first
#define y second

using namespace std;

typedef long long ll;

const ll INF = (ll) 1e18;

ll fact[55], dp[55];
bool used[55], head[55], tail[55];
int answer[55];

vector <int> get_permut(ll num, int len)
{
	for (int i = 1; i <= len; i++) used[i] = false, head[i] = false, tail[i] = false;
	vector <int> spis;
	spis.pb(len);
	used[len] = true;
	head[1] = true, tail[len] = true;

	//cout << num << ' ' << len << '\n';
	for (int i = 2; i <= len; i++)
	{
	 	for (int j = 1; j <= len; j++)
	 	{
	 	 	if (used[j]) continue;
	 	 	if (tail[i])
	 	 	{
	 	 		if (head[j])
	 	 		{
	 	 		 	int pr = j;
	 	 		 	while (pr <= (int) spis.size()) pr = spis[pr - 1];
	 	 		 	if (pr == i && i != len) continue;
	 	 		}
	 	 		if (num == 1 || fact[len - i - 1] >= num)
	 	 		{
	 	 		 	used[j] = true;
	 	 		 	spis.pb(j);
	 	 		 	tail[i] = false;
	 	 		 	if (!head[j]) tail[j] = true;
	 	 		 	else head[j] = false;
	 	 		 	break;
	 	 		}
	 	 		num -= fact[len - i - 1];

	 	 	}
	 	 	else
	 	 	{
	 	 		if (num == 1 || fact[len - i - 1] >= num)
	 	 		{
	 	 		 	used[j] = true;
	 	 		 	spis.pb(j);
	 	 		 	head[i] = true;
	 	 		 	if (!head[j]) tail[j] = true;
	 	 		 	else head[j] = false;
	 	 		 	break;
	 	 		}
	 	 		num -= fact[len - i - 1];
	 	 	}
	 	}
	}

   	return spis;
}

int main()
{
 	//freopen("input.txt", "r", stdin);
 	//freopen("output.txt", "w", stdout);
 	ios_base::sync_with_stdio(0); cin.tie(0);
 	int test;
 	cin >> test;

 	fact[0] = 1;
 	for (int i = 1; i <= 50; i++)
 		if (fact[i - 1] > INF / (ll) i) fact[i] = INF;
 		else fact[i] = fact[i - 1] * (ll) i;

 	dp[0] = 1;
 	for (int i = 1; i <= 50; i++)
 	{
 		for (int j = 0; j < i - 1; j++)
 		{
 			ll val = 0;
 			if (dp[j] > INF / fact[i - j - 2]) val = INF;
 			else val = dp[j] * fact[i - j - 2];
 			dp[i] += val;
 			dp[i] = min(dp[i], INF);
 		}
 		dp[i] += dp[i - 1];
 		dp[i] = min(dp[i], INF);
 	}

 	for (int rep = 1; rep <= test; rep++)
 	{
 	 	int n;
 	 	ll k;
 	 	cin >> n >> k;

 	 	if (dp[n] < k) {cout << "-1\n"; continue;}
 	 	int uk = 1, pos = 1;
 	 	while (uk <= n)
 	 	{
 	 	 	ll val = 0, umn = 1;
 	 	 	if (uk > pos) umn = fact[uk - pos - 1];
 	 	 	if (dp[n - uk] > INF / umn) val = INF;
 	 	 	else val = dp[n - uk] * umn;
 	 	 	if (val >= k)
 	 	 	{
 	 	 		//cout << uk << '\n';
 	 	 		ll tim = (k - 1) / dp[n - uk] + 1;
 	 	 		vector <int> P = get_permut(tim, uk - pos + 1);
 	 	 		for (int i = pos; i <= uk; i++) answer[i] = P[i - pos] + pos - 1;

 	 	 		k -= (tim - 1) * dp[n - uk];
 	 	 		pos = uk + 1;
 	 	 	}
 	 	 	else k -= val;
 	 	 	uk++;
 	 	}
 	 	for (int i = 1; i <= n; i++) cout << answer[i] << ' ';
 	 	cout << '\n';
 	}
 	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define x first
#define y second

using namespace std;

typedef long long ll;

const ll INF = (ll) 1e18;

ll fact[55], dp[55];
bool used[55], head[55], tail[55];
int answer[55];

vector <int> get_permut(ll num, int len)
{
	for (int i = 1; i <= len; i++) used[i] = false, head[i] = false, tail[i] = false;
	vector <int> spis;
	spis.pb(len);
	used[len] = true;
	head[1] = true, tail[len] = true;

	//cout << num << ' ' << len << '\n';
	for (int i = 2; i <= len; i++)
	{
	 	for (int j = 1; j <= len; j++)
	 	{
	 	 	if (used[j] || i == j) continue;
	 	 	if (tail[i])
	 	 	{
	 	 		if (head[j])
	 	 		{
	 	 		 	int pr = j;
	 	 		 	while (pr <= (int) spis.size()) pr = spis[pr - 1];
	 	 		 	if (pr == i && i != len) continue;
	 	 		}
	 	 		if (num == 1 || fact[len - i - 1] >= num)
	 	 		{
	 	 		 	used[j] = true;
	 	 		 	spis.pb(j);
	 	 		 	tail[i] = false;
	 	 		 	if (!head[j]) tail[j] = true;
	 	 		 	else head[j] = false;
	 	 		 	break;
	 	 		}
	 	 		num -= fact[len - i - 1];

	 	 	}
	 	 	else
	 	 	{
	 	 		if (num == 1 || fact[len - i - 1] >= num)
	 	 		{
	 	 		 	used[j] = true;
	 	 		 	spis.pb(j);
	 	 		 	head[i] = true;
	 	 		 	if (!head[j]) tail[j] = true;
	 	 		 	else head[j] = false;
	 	 		 	break;
	 	 		}
	 	 		num -= fact[len - i - 1];
	 	 	}
	 	}
	}

   	return spis;
}

int main()
{
 	//freopen("input.txt", "r", stdin);
 	//freopen("output.txt", "w", stdout);
 	ios_base::sync_with_stdio(0); cin.tie(0);
 	int test;
 	cin >> test;

 	fact[0] = 1;
 	for (int i = 1; i <= 50; i++)
 		if (fact[i - 1] > INF / (ll) i) fact[i] = INF;
 		else fact[i] = fact[i - 1] * (ll) i;

 	dp[0] = 1;
 	for (int i = 1; i <= 50; i++)
 	{
 		for (int j = 0; j <= i - 2; j++)
 		{
 			ll val = 0;
 			if (dp[j] > INF / fact[i - j - 2]) val = INF;
 			else val = dp[j] * fact[i - j - 2];
 			dp[i] += val;
 			dp[i] = min(dp[i], INF);
 		}
 		dp[i] += dp[i - 1];
 		dp[i] = min(dp[i], INF);
 	}

 	for (int rep = 1; rep <= test; rep++)
 	{
 	 	int n;
 	 	ll k;
 	 	cin >> n >> k;

 	 	if (dp[n] < k) {cout << "-1\n"; continue;}
 	 	int uk = 1, pos = 1;
 	 	while (uk <= n)
 	 	{
 	 	 	ll val = 0, umn = 1;
 	 	 	if (uk > pos) umn = fact[uk - pos - 1];
 	 	 	if (dp[n - uk] > INF / umn) val = INF;
 	 	 	else val = dp[n - uk] * umn;
 	 	 	if (val >= k)
 	 	 	{
 	 	 		//cout << uk << '\n';
 	 	 		ll tim = (k - 1) / dp[n - uk] + 1;
 	 	 		vector <int> P = get_permut(tim, uk - pos + 1);
 	 	 		for (int i = pos; i <= uk; i++) answer[i] = P[i - pos] + pos - 1;

 	 	 		k -= (tim - 1) * dp[n - uk];
 	 	 		pos = uk + 1;
 	 	 	}
 	 	 	else k -= val;
 	 	 	uk++;
 	 	}
 	 	for (int i = 1; i <= n; i++) cout << answer[i] << ' ';
 	 	cout << '\n';
 	}
 	return 0;
}


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

const ll INF = 2e18;
const int MAX_N = 55;

ll dp[MAX_N], cycle[MAX_N];
ll mult(ll a, ll b) {
	long double tmp = a;
	tmp *= b;
	if ( tmp > INF) return INF;
	return a * b;
}

ll add(ll a, ll b) {
	long double tmp = a;
	tmp += b;
	if ( tmp > INF) return INF;
	return a + b;
}

bool visited[MAX_N];
int ans[MAX_N];
int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		int n;
		ll k;
		scanf("%d %lld", &n, &k);
		k--;
		dp[n] = 1;
		cycle[0] = cycle[1] = cycle[2] = 1;
		for (int i=3; i<=n ; i++) {
			cycle[i] = mult(cycle[i-1], i-2);
		}
		for (int i=n-1; i>=0; i--) {
			dp[i] = 0;
			for (int j=i; j<n; j++) {
				int size = j - i + 1;
				dp[i] = add (dp[i], mult(dp[j+1], cycle[size]));
			}
		}
		if (k >= dp[0]) {
			printf("-1\n");
			continue;
		}
		memset(visited, 0, sizeof(visited));
		memset(ans, -1, sizeof(ans));
		for (int i=0; i<n; i++) {
			for (int v=i; v<n; v++) {
				int sz = (v-i)+1;
				ll cnt = mult(cycle[sz],dp[i+sz]);
				if (k >= cnt) {
					k-=cnt;
					continue;
				}
				ans[i] = v;
				visited[v] = true;
				for(int j=i+1; j<=v; j++) {
					int sz2 = (v-j)+1;
					for (int v2=i; v2<v; v2++) {
						if (v2 == j or visited[v2])
							continue;
						if ( j != v ) {
							int tmp = ans[j];
							while(tmp != -1 && tmp != j) {
								tmp = ans[tmp];
							}
							if (tmp == j) continue;
						}
						ll cnt2 = mult(cycle[sz2], dp[v+1]);
						if (k >= cnt2) {
							k-=cnt2;
							continue;
						}
						ans[j] = v2;
						visited[v2] = true;
						break;
					}
				}
				i=v;
				break;
			}

		}
			for (int i=0; i<n; i++ ){
				printf("%d%c", ans[i]+1, " \n"[i==n-1]);
			}

	}
	return 0;
}


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

const ll INF = 2e18;
const int MAX_N = 55;

ll dp[MAX_N], cycle[MAX_N];
ll mult(ll a, ll b) {
	long double tmp = a;
	tmp *= b;
	if ( tmp > INF) return INF;
	return a * b;
}

ll add(ll a, ll b) {
	long double tmp = a;
	tmp += b;
	if ( tmp > INF) return INF;
	return a + b;
}

bool visited[MAX_N];
int ans[MAX_N];
int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		int n;
		ll k;
		scanf("%d %lld", &n, &k);
		k--;
		dp[n] = 1;
		cycle[0] = cycle[1] = cycle[2] = 1;
		for (int i=3; i<=n ; i++) {
			cycle[i] = mult(cycle[i-1], i-2);
		}
		for (int i=n-1; i>=0; i--) {
			dp[i] = 0;
			for (int j=i; j<n; j++) {
				int size = j - i + 1;
				dp[i] = add (dp[i], mult(dp[j+1], cycle[size]));
			}
		}
		if (k >= dp[0]) {
			printf("-1\n");
			continue;
		}
		memset(visited, 0, sizeof(visited));
		memset(ans, -1, sizeof(ans));
		for (int i=0; i<n; i++) {
			for (int v=i; v<n; v++) {
				int sz = (v-i)+1;
				ll cnt = mult(cycle[sz],dp[i+sz]);
				if (k >= cnt) {
					k-=cnt;
					continue;
				}
				ans[i] = v;
				visited[v] = true;
				for(int j=i+1; j<=v; j++) {
					int sz2 = (v-j)+1;
					for (int v2=i; v2<v; v2++) {
						if (v2 == j or visited[v2])
							continue;
						if ( j != v ) {
							int tmp = ans[v2];
							while(tmp != -1 && tmp != j) {
								tmp = ans[tmp];
							}
							if (tmp == j) continue;
						}
						ll cnt2 = mult(cycle[sz2], dp[v+1]);
						if (k >= cnt2) {
							k-=cnt2;
							continue;
						}
						ans[j] = v2;
						visited[v2] = true;
						break;
					}
				}
				i=v;
				break;
			}

		}
			for (int i=0; i<n; i++ ){
				printf("%d%c", ans[i]+1, " \n"[i==n-1]);
			}

	}
	return 0;
}


#include <bits/stdc++.h>

#define all(A) begin(A), end(A)
#define rall(A) rbegin(A), rend(A)
#define sz(A) int(A.size())
#define pb push_back
#define mp make_pair

using namespace std;

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

const int N = 55;
const ll INF = 2e18;
ll cycle[N];

inline ll add (ll a, ll b) {
  if (a > INF - b) return INF;
  return a + b;
}

inline ll mul (ll a, ll b) {
  if (b == 0) return 0;
  if (a > INF / b) return INF;
  return a * b;
}

void pre () {
  vll fact(N);
  fact[0] = fact[1] = 1;
  cycle[0] = cycle[1] = 1;
  for (int i = 2; i < N; i++) {
    fact[i] = mul(fact[i - 1], i);
    cycle[i] = fact[i - 2];
  }
}

// dp[i] = number of good permutations with [i:n]
// dp[i] = sum(j = i, n) dp[j + 1] * cycle(j - i)

void solve () {
  ll n, k;
  cin >> n >> k;
  vll dp(n + 2, 0);
  dp[n + 1] = 1;
  for (int i = n; i >= 1; i--) {
    for (int j = i; j <= n; j++) {
      dp[i] = add(dp[i], mul(dp[j + 1], cycle[j - i + 1]));
    }
  }
  if (dp[1] < k) {
    cout << -1 << '\n';
    return;
  }
  vector <bool> used(n + 1, false);
  vll ans(n + 1, -1);
  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j++) {
      ll len = j - i + 1; // [i, ..., j]
      ll contribution = mul(dp[j + 1], cycle[j - i + 1]);
      if (contribution < k) {
        k -= contribution;
        continue;
      }
      ans[i] = j;
      used[j] = true;
      for (int pos = i + 1; pos <= j; pos++) {
        for (int x = i; x <= j; x++) {
          if (used[x] or pos == x) continue;
          if (pos != j) { // pos = j should form a cycle
            int cur = ans[x];
            while (cur != -1 and cur != pos) cur = ans[cur];
            if (cur == pos) continue;
          }
          ll contribution = mul(dp[j + 1], cycle[j - pos + 1]);
          if (contribution < k) {
            k -= contribution;
            continue;
          }
          ans[pos] = x;
          used[x] = true;
          break;
        }
      }
      i = j;
      break;
    }
  }
  for (int i = 1; i <= n; i++) cout << ans[i] << " \n"[i == n];
}

int main () {
  ios::sync_with_stdio(false); cin.tie(0);
  pre();
  int tc;
  cin >> tc;
  while (tc--) solve();
  return (0);
}


/*
    Author: Nguyen Tan Bao
    Status:
    Idea:
*/

#include <bits/stdc++.h>
#define FI first
#define SE second
#define EPS 1e-9
#define ALL(a) a.begin(),a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORE(i,a,b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x

using namespace std;
using ll = long long;
using ld = double;
typedef pair<ll, ll> II;
typedef pair<II, ll> III;
typedef complex<ld> cd;
typedef vector<cd> vcd;

const ll MODBASE = 1000000007LL;
const int MAXN = 55;
const int MAXM = 1000;
const int MAXK = 16;
const int MAXQ = 200010;

int t, n, lab[MAXN], degIn[MAXN];
ll k, dp[MAXN], f[MAXN];

ll F(int u) {
    if (u <= 1) return 1;
    if (u > 21) return 2e18;
    if (f[u] != -1) return f[u];
    ll res = 1;
    FOR(i,1,u-2) res *= i;
    return f[u] = res;
}

ll DP(int u) {
    if (u <= 21) return dp[u];
    return 2e18;
}

bool canConnect(int u, int v) {
    if (u == v) return false;
    if (degIn[v]) return false;
    if (lab[u] == lab[v]) return false;
    return true;
}

bool connect(int u, int v, int l, int r) {
    if (!canConnect(u, v)) return false;
    int p = lab[u];
    FOR(i,l,r)
        if (lab[i] == p) lab[i] = lab[v];
    degIn[v] = true;
    return true;
}

void build(int st, int n, ll x, ll &remain) {
    int en = st + n - 1;
    cout << en << ' ';
    if (n == 1) return;
    FOR(i,st,en) {
        lab[i] = i;
        degIn[i] = false;
    }
    connect(st, en, st, en);
    int numComp = n - 1;
    FOR(i,st+1,en)
        FOR(j,st,en) {
            if (i == en) {
                if (!degIn[j]) {
                    cout << j << ' ';
                    break;
                }
                continue;
            }
            if (canConnect(i, j)) {
                if (x * F(numComp) < remain) {
                    remain -= x * F(numComp);
                    continue;
                }
                numComp--;
                connect(i, j, st, en);
                cout << j << ' ';
                break;
            }
        }
}

void solve(int n, ll remain, int st) {
    if (n <= 0) return;
    if (n == 1) {
        cout << st << ' ';
        return;
    }
    FOR(i,1,n)
        if (F(i) * DP(n-i) < remain) remain -= F(i) * DP(n-i);
        else {
            // cout << st << ' ' << n << ' ' << remain << endl;
            build(st, i, DP(n-i), remain);
            solve(n-i, remain, st+i);
            break;
        }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    FOR(i,1,50) f[i] = dp[i] = -1;
    dp[0] = 1;
    dp[1] = 1;
    FOR(i,2,21) {
        dp[i] = 0;
        FOR(j,1,i) dp[i] += F(j) * dp[i-j];
    }
    // FOR(i,1,21) cout << dp[i] << endl;
    cin >> t;
    while (t--) {
        cin >> n >> k;
        if (n <= 21 && k > DP(n)) {
            cout << -1 << "\n";
            continue;
        }
        solve(n, k, 1);
        cout << "\n";
    }
    return 0;
}


/*
    Author: Nguyen Tan Bao
    Status:
    Idea:
*/

#include <bits/stdc++.h>
#define FI first
#define SE second
#define EPS 1e-9
#define ALL(a) a.begin(),a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORE(i,a,b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x

using namespace std;
using ll = long long;
using ld = double;
typedef pair<ll, ll> II;
typedef pair<II, ll> III;
typedef complex<ld> cd;
typedef vector<cd> vcd;

const ll MODBASE = 1000000007LL;
const int MAXN = 55;
const int MAXM = 1000;
const int MAXK = 16;
const int MAXQ = 200010;

int t, n, lab[MAXN], degIn[MAXN];
ll k, dp[MAXN], f[MAXN];

ll F(int u) {
    if (u <= 1) return 1;
    if (u > 20) return 2e18;
    if (f[u] != -1) return f[u];
    ll res = 1;
    FOR(i,1,u-2) res *= i;
    return f[u] = res;
}

ll DP(int u) {
    if (u <= 20) return dp[u];
    return 2e18;
}

bool canConnect(int u, int v) {
    if (u == v) return false;
    if (degIn[v]) return false;
    if (lab[u] == lab[v]) return false;
    return true;
}

bool connect(int u, int v, int l, int r) {
    if (!canConnect(u, v)) return false;
    int p = lab[u];
    FOR(i,l,r)
        if (lab[i] == p) lab[i] = lab[v];
    degIn[v] = true;
    return true;
}

void build(int st, int n, ll x, ll &remain) {
    int en = st + n - 1;
    cout << en << ' ';
    if (n == 1) return;
    FOR(i,st,en) {
        lab[i] = i;
        degIn[i] = false;
    }
    connect(st, en, st, en);
    int numComp = n - 1;
    FOR(i,st+1,en)
        FOR(j,st,en) {
            if (i == en) {
                if (!degIn[j]) {
                    cout << j << ' ';
                    break;
                }
                continue;
            }
            if (canConnect(i, j)) {
                if (x * F(numComp) < remain) {
                    remain -= x * F(numComp);
                    continue;
                }
                numComp--;
                connect(i, j, st, en);
                cout << j << ' ';
                break;
            }
        }
}

void solve(int n, ll remain, int st) {
    if (n <= 0) return;
    if (n == 1) {
        cout << st << ' ';
        return;
    }
    FOR(i,1,n)
        if (F(i) * DP(n-i) < remain) remain -= F(i) * DP(n-i);
        else {
            // cout << st << ' ' << n << ' ' << remain << endl;
            build(st, i, DP(n-i), remain);
            solve(n-i, remain, st+i);
            break;
        }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    FOR(i,1,50) f[i] = dp[i] = -1;
    dp[0] = 1;
    dp[1] = 1;
    FOR(i,2,20) {
        dp[i] = 0;
        FOR(j,1,i) dp[i] += F(j) * dp[i-j];
    }
    // FOR(i,1,20) cout << dp[i] << endl;
    cin >> t;
    while (t--) {
        cin >> n >> k;
        if (n <= 20 && k > DP(n)) {
            cout << -1 << "\n";
            continue;
        }
        solve(n, k, 1);
        cout << "\n";
    }
    return 0;
}


/*
    Author: Nguyen Tan Bao
    Status:
    Idea:
*/

#include <bits/stdc++.h>
#define FI first
#define SE second
#define EPS 1e-9
#define ALL(a) a.begin(),a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORE(i,a,b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x

using namespace std;
using ll = long long;
using ld = double;
typedef pair<ll, ll> II;
typedef pair<II, ll> III;
typedef complex<ld> cd;
typedef vector<cd> vcd;

const ll MODBASE = 1000000007LL;
const int MAXN = 55;
const int MAXM = 1000;
const int MAXK = 16;
const int MAXQ = 200010;

int t, n, lab[MAXN], degIn[MAXN];
ll k, dp[MAXN], f[MAXN];

ll F(int u) {
    if (u <= 1) return 1;
    if (u > 20) return 2e18;
    if (f[u] != -1) return f[u];
    ll res = 1;
    FOR(i,1,u-2) res *= i;
    return f[u] = res;
}

ll DP(int u) {
    if (u <= 20) return dp[u];
    return 2e18;
}

bool canConnect(int u, int v) {
    if (u == v) return false;
    if (degIn[v]) return false;
    if (lab[u] == lab[v]) return false;
    return true;
}

bool connect(int u, int v, int l, int r) {
    if (!canConnect(u, v)) return false;
    int p = lab[u];
    FOR(i,l,r)
        if (lab[i] == p) lab[u] = lab[v];
    degIn[v] = true;
    return true;
}

void build(int st, int n, ll x, ll &remain) {
    int en = st + n - 1;
    cout << en << ' ';
    if (n == 1) return;
    FOR(i,st,en) {
        lab[i] = i;
        degIn[i] = false;
    }
    connect(st, en, st, en);
    int numComp = n - 1;
    FOR(i,st+1,en)
        FOR(j,st,en) {
            if (i == en) {
                if (!degIn[j]) {
                    cout << j << ' ';
                    break;
                }
                continue;
            }
            if (canConnect(i, j)) {
                if (x * F(numComp) < remain) {
                    remain -= x * F(numComp);
                    continue;
                }
                numComp--;
                connect(i, j, st, en);
                cout << j << ' ';
                break;
            }
        }
}

void solve(int n, ll remain, int st) {
    if (n <= 0) return;
    if (n == 1) {
        cout << st << ' ';
        return;
    }
    FOR(i,1,n)
        if (F(i) * DP(n-i) < remain) remain -= F(i) * DP(n-i);
        else {
            // cout << st << ' ' << n << ' ' << remain << endl;
            build(st, i, DP(n-i), remain);
            solve(n-i, remain, st+i);
            break;
        }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    FOR(i,1,50) f[i] = dp[i] = -1;
    dp[0] = 1;
    dp[1] = 1;
    FOR(i,2,20) {
        dp[i] = 0;
        FOR(j,1,i) dp[i] += F(j) * dp[i-j];
    }
    // FOR(i,1,20) cout << dp[i] << endl;
    cin >> t;
    while (t--) {
        cin >> n >> k;
        if (n <= 20 && k > DP(n)) {
            cout << -1 << "\n";
            continue;
        }
        solve(n, k, 1);
        cout << "\n";
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#undef LOCAL
string to_string(string s) { return '"' + s + '"'; }
string to_string(bool x) { return (x ? "T" : "F"); }
string to_string(const char* s) { return to_string((string)s); }
template <typename A, typename B>
string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename V>
string to_string(V v) {
    bool f = true; string res = "[";
    for (const auto& x: v) {
        if (!f) res += ", ";
        f = false;
        res += to_string(x); } res += "]";
    return res;
}
void debug() { cerr << endl; }
template <typename H, typename... T>
void debug(H h, T... t) { cerr << " " << to_string(h); debug(t...); }
#ifdef LOCAL
#define dbg(args...) cerr<<"("<<#args<<") =", debug(args)
#else
#define dbg(args...) 2019
#endif
using ll=long long;
const long long INF = 0x3f3f3f3f3f3f3f3f;

const int N = 51;
ll dp[N][N];
ll f[N], g[N];
void prep() {
    f[0]=f[1]=f[2]=1;
    for (int i = 3; i < N; i++) {
        if (f[i-1] >= INF/(i-1)) f[i] = INF;
        else f[i] = (i-1) * f[i-1];
        dbg(f[i]);
    }
    g[0] = 1;
    for (int i = 1; i < N; i++) {
        for (int j = 1; j <= i; j++) {
            // max is 1st
            if (f[j-1] > INF/g[i-j]) dp[i][j] = INF;
            else dp[i][j] += f[j-1] * g[i-j];
            if (dp[i][j] > INF) dp[i][j] = INF;
            // sum
            g[i] += dp[i][j];
            if (g[i] > INF) g[i] = INF;
        }
        dbg(g[i]);
    }
}
void solve() {
    int n; ll k;
    cin >> n >> k;
    if (k > g[n]) {
        cout << "-1\n"; return;
    }

    vector<int> p(n);
    iota(p.begin(), p.end(), 0);
    vector<int> a(n);
    iota(a.begin(), a.end(), 0);

    vector<bool> trk(n);
    auto sw = [&](int i, int x){
        trk[x] = true;
        int j = a[x];
        //assert(p[i] != p[j]);
        swap(p[i], p[j]);
        a[p[i]] = i;
        a[p[j]] = j;
    };
    for (int i = 0; i < n;) {
        int j = 1;
        while (k > dp[n-i][j]) {
            k -= dp[n-i][j];
            j++;
        }
        //cerr<<i<<endl;
        sw(i, i+j-1);
        int _i = i;
        i++; j--;
        while (j > 1) {
            int x = _i;
            while (true) {
                while (trk[x] || a[x]==i) {
                    x++;
                }
                if (k > dp[n-i][j]) {
                    x++; k -= dp[n-i][j];
                }else break;
            }
            //while (!trk[x] && (a[x]!=i?1:x+=1,0) && k > dp[n-i][j]) {
            //    x++; k -= dp[n-i][j];
            //}
            sw(i, x);
            i++; j--;
        }
        if (j>0) i++,j--;
    }
    for (int x: p) {
        cout << x+1 << ' ';
    }cout << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    prep();
    int t; cin >> t;
    while(t--)solve();
    cout << endl;
}


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

const long long INF = (1e18) + 1e17;
struct InfNum {

	long long v;
	InfNum() : v(0) {}
	InfNum(long long v_) : v(v_) { if (v > INF) v = INF; }
};

InfNum operator + (InfNum a, InfNum b) {
	return min(INF, a.v + b.v);
}

InfNum operator * (InfNum a, InfNum b) {
	if (a.v == 0 || b.v == 0)
		return 0;
	if (a.v > (INF + b.v - 1) / b.v)
		return INF;
	else
		return min(INF, a.v * b.v);
}

InfNum mi[110], dp[110];
InfNum cycle[110][110];

int t, n;
long long k;

int compute(int a[51], int l) {
	bool used[51];

	memset(used, false, sizeof used);

	// if (a[1] == 4 && a[2] == 2)
	// 	printf("hehehe\n");
	int ans = 0;
	for (int i = 1; i <= l; i++)
		if (!used[i]) {
			int x = i;
			while (true) {

				if (x > l) {
					ans++;
					break;
				}
				if (used[x] && x == i)
					return 0;
				if (used[x])
					break;

				used[x] = true;
				x = a[x];
			}
		}
	return ans;
}

vector <int> generate2(int n, long long k) {
	// printf("generate2 %d %lld\n", n, k);

	if (n == 1) {
		assert(k == 0);
		return vector <int> ({1});
	}
	int a[51];
	bool used[51];
	memset(a, 0, sizeof a);

	a[1] = n;
	memset(used, 0, sizeof used);
	used[n] = true;

	assert(k < cycle[n - 1][1].v);

	for (int i = 2; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (used[j])
				continue;
			a[i] = j;

			int nc = compute(a, i);
			if (nc == 0)
				continue;

			// if (i == 2)
			// 	printf("!! %d %d %lld\n", j, nc, cycle[n - i][nc].v);

			if (k < cycle[n - i][nc].v) {
				used[j] = true;
				break;
			}else {
				k -= cycle[n - i][nc].v;
			}
		}
	}

	vector <int> ans;
	for (int i = 1; i <= n; i++)
		ans.push_back(a[i]);
	return ans;
}

vector <int> generate1(int n, long long k) {

	// printf("generate1 %d %lld\n", n, k);

	if (n == 0) {
		assert(k == 0);
		return vector <int>();
	}
	for (int j = 1; j <= n; j++) {
		if (k < (mi[max(0, j - 2)] * dp[n - j]).v) {

			auto first = generate2(j, k / dp[n - j].v);
			auto second = generate1(n - j, k % dp[n - j].v);

			for (auto v: second)
				first.push_back(v + j);
			return first;
		}else {
			k -= (mi[max(0, j - 2)] * dp[n - j]).v;
		}
	}

	assert(false);
}

int main() {

	mi[0] = 1;
	for (int i = 1; i <= 50; i++) {
		mi[i] = mi[i - 1] * i;
	}

	dp[0] = 1;
	for (int i = 1; i <= 50; i++) {
		for (int j = 1; j <= i; j++) {
			dp[i] = dp[i] + mi[max(0, j - 2)] * dp[i - j];
		}
	}

	cycle[0][0] = 1;
	for (int i = 1; i <= 50; i++) {
		for (int j = 1; j <= 50; j++) {
			cycle[i][j] = cycle[i - 1][j] * (i - 1) + cycle[i - 1][j - 1] * (j - 1);
			if (j == 1)
				cycle[i][j] = cycle[i][j] + cycle[i - 1][0];
		}
	}

	// for (int i = 1; i <= 50; i++)
	// 	printf("%lld %lld\n", dp[i].v, mi[i].v);

	scanf("%d", &t);
	while (t--) {
		scanf("%d%lld", &n, &k);
		if (k > dp[n].v) {
			printf("-1\n");
			continue;
		}

		k--;
		auto ans = generate1(n, k);

		for (auto v: ans) {
			printf("%d ", v);
		}
		printf("\n");
	}
}


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

const long long INF = (1e18) + 1e17;
struct InfNum {

	long long v;
	InfNum() : v(0) {}
	InfNum(long long v_) : v(v_) { if (v > INF) v = INF; }
};

InfNum operator + (InfNum a, InfNum b) {
	return min(INF, a.v + b.v);
}

InfNum operator * (InfNum a, InfNum b) {
	if (a.v == 0 || b.v == 0)
		return 0;
	if (a.v > (INF + b.v - 1) / b.v)
		return INF;
	else
		return min(INF, a.v * b.v);
}

InfNum mi[110], dp[110];
InfNum cycle[110][110];

int t, n;
long long k;

int compute(int a[51], int l) {
	bool used[51];

	memset(used, false, sizeof used);

	// if (a[1] == 4 && a[2] == 2)
	// 	printf("hehehe\n");
	int ans = 0;
	for (int i = 1; i <= l; i++)
		if (!used[i]) {
			int x = i;
			while (true) {

				if (x > l) {
					ans++;
					break;
				}
				if (used[x] && x == i)
					return 0;
				if (used[x])
					break;

				used[x] = true;
				x = a[x];
			}
		}
	return ans;
}

vector <int> generate2(int n, long long k) {
	// printf("generate2 %d %lld\n", n, k);

	if (n == 1) {
		assert(k == 0);
		return vector <int> ({1});
	}
	int a[51];
	bool used[51];
	memset(a, 0, sizeof a);

	a[1] = n;
	memset(used, 0, sizeof used);
	used[n] = true;

	assert(k < cycle[n - 1][1].v);

	for (int i = 2; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (used[j])
				continue;

			a[i] = j;

			// for (int tt = 1; tt <= i; tt++)
			// 	printf("%d ", a[tt]);
			// printf("\n");

			int nc = compute(a, i);
			if (nc == 0)
				continue;

			// printf("!! %d %d %lld\n", j, nc, cycle[n - i][nc].v);

			if (k < cycle[n - i][nc].v) {
				used[j] = true;
				break;
			}else {
				k -= cycle[n - i][nc].v;
			}
		}
	}

	vector <int> ans;
	for (int i = 1; i <= n; i++)
		ans.push_back(a[i]);
	return ans;
}

vector <int> generate1(int n, long long k) {

	// printf("generate1 %d %lld\n", n, k);

	if (n == 0) {
		assert(k == 0);
		return vector <int>();
	}
	for (int j = 1; j <= n; j++) {
		if (k < (mi[max(0, j - 2)] * dp[n - j]).v) {

			auto first = generate2(j, k / dp[n - j].v);
			auto second = generate1(n - j, k % dp[n - j].v);

			for (auto v: second)
				first.push_back(v + j);
			return first;
		}else {
			k -= (mi[max(0, j - 2)] * dp[n - j]).v;
		}
	}

	assert(false);
}

int main() {

	mi[0] = 1;
	for (int i = 1; i <= 50; i++) {
		mi[i] = mi[i - 1] * i;
	}

	dp[0] = 1;
	for (int i = 1; i <= 50; i++) {
		for (int j = 1; j <= i; j++) {
			dp[i] = dp[i] + mi[max(0, j - 2)] * dp[i - j];
		}
	}

	cycle[0][0] = 1;
	for (int i = 1; i <= 50; i++) {
		for (int j = 1; j <= 50; j++) {
			cycle[i][j] = cycle[i - 1][j] * max(0, i - j) + cycle[i - 1][j - 1] * (j - 1);
			if (j == 1)
				cycle[i][j] = cycle[i][j] + cycle[i - 1][0];
		}
	}

	// printf("%lld\n", cycle[3][2].v);
	// for (int i = 1; i <= 50; i++)
	// 	printf("%lld %lld\n", dp[i].v, mi[i].v);

	scanf("%d", &t);
	while (t--) {
		scanf("%d%lld", &n, &k);
		if (k > dp[n].v) {
			printf("-1\n");
			continue;
		}

		k--;
		auto ans = generate1(n, k);

		for (auto v: ans) {
			printf("%d ", v);
		}
		printf("\n");
	}
}


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

const long long INF = (1e18) + 1e17;
struct InfNum {

	long long v;
	InfNum() : v(0) {}
	InfNum(long long v_) : v(v_) { if (v > INF) v = INF; }
};

InfNum operator + (InfNum a, InfNum b) {
	return min(INF, a.v + b.v);
}

InfNum operator * (InfNum a, InfNum b) {
	if (a.v == 0 || b.v == 0)
		return 0;
	if (a.v > (INF + b.v - 1) / b.v)
		return INF;
	else
		return min(INF, a.v * b.v);
}

InfNum mi[110], dp[110];
InfNum cycle[110][110];

int t, n;
long long k;

int compute(int a[51], int l) {
	bool used[51];

	memset(used, false, sizeof used);

	int ans = 0;
	for (int i = 1; i <= l; i++)
		if (!used[i]) {
			int x = i;
			while (!used[x]) {

				if (x > l) {
					ans++;
					break;
				}
				if (used[x] && x == i)
					return 0;
				if (used[x])
					break;

				used[x] = true;
				x = a[x];
			}
		}
	return ans;
}

vector <int> generate2(int n, long long k) {
	// printf("generate2 %d %lld\n", n, k);

	if (n == 1) {
		assert(k == 0);
		return vector <int> ({1});
	}
	int a[51];
	bool used[51];
	memset(a, 0, sizeof a);

	a[1] = n;
	memset(used, 0, sizeof used);
	used[n] = true;

	assert(k < cycle[n - 1][1].v);

	for (int i = 2; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (used[j])
				continue;
			a[i] = j;

			int nc = compute(a, i);
			if (nc == 0)
				continue;

			// if (j == 1)
			// 	printf("!! %d %lld\n", nc, cycle[n - i][nc].v);

			if (k < cycle[n - i][nc].v) {
				used[j] = true;
				break;
			}else {
				k -= cycle[n - i][nc].v;
			}
		}
	}

	vector <int> ans;
	for (int i = 1; i <= n; i++)
		ans.push_back(a[i]);
	return ans;
}

vector <int> generate1(int n, long long k) {

	// printf("generate1 %d %lld\n", n, k);

	if (n == 0) {
		assert(k == 0);
		return vector <int>();
	}
	for (int j = 1; j <= n; j++) {
		if (k < (mi[max(0, j - 2)] * dp[n - j]).v) {

			auto first = generate2(j, k / dp[n - j].v);
			auto second = generate1(n - j, k % dp[n - j].v);

			for (auto v: second)
				first.push_back(v + j);
			return first;
		}else {
			k -= (mi[max(0, j - 2)] * dp[n - j]).v;
		}
	}

	assert(false);
}

int main() {

	mi[0] = 1;
	for (int i = 1; i <= 50; i++) {
		mi[i] = mi[i - 1] * i;
	}

	dp[0] = 1;
	for (int i = 1; i <= 50; i++) {
		for (int j = 1; j <= i; j++) {
			dp[i] = dp[i] + mi[max(0, j - 2)] * dp[i - j];
		}
	}

	cycle[0][0] = 1;
	for (int i = 1; i <= 50; i++) {
		for (int j = 1; j <= 50; j++) {
			cycle[i][j] = cycle[i - 1][j] * (i - 1) + cycle[i - 1][j - 1] * (j - 1);
			if (j == 1)
				cycle[i][j] = cycle[i][j] + cycle[i - 1][0];
		}
	}

	// for (int i = 1; i <= 50; i++)
	// 	printf("%lld %lld\n", dp[i].v, mi[i].v);

	scanf("%d", &t);
	while (t--) {
		scanf("%d%lld", &n, &k);
		if (k > dp[n].v) {
			printf("-1\n");
			continue;
		}

		k--;
		auto ans = generate1(n, k);

		for (auto v: ans) {
			printf("%d ", v);
		}
		printf("\n");
	}
}