← Home
```go
package main

import (
	"bufio"
	"fmt"
	"os"
)

var dp [32][470]int

func init() {
	// Precompute the DP table.
	// dp[i][j] stores the length of the last block used to achieve a total length i
	// and total sorted subsegment count j.
	// Constraints: n <= 30. Max sorted subsegments = 30*31/2 = 465.
	// We iterate forward to fill reachable states.

	for i := 0; i < 30; i++ {
		for j := 0; j <= 465; j++ {
			// Skip unreachable states, except the base case (0, 0)
			if i != 0 || j != 0 {
				if dp[i][j] == 0 {
					continue
				}
			}

			// Try adding a block of length l
			for l := 1; l <= 30-i; l++ {
				cost := l * (l + 1) / 2
				nextScore := j + cost
				if nextScore <= 465 {
					// If the next state is not visited yet, record the transition
					if dp[i+l][nextScore] == 0 {
						dp[i+l][nextScore] = l
					}
				}
			}
		}
	}
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var t int
	if _, err := fmt.Fscan(in, &t); err != nil {
		return
	}

	for caseIdx := 0; caseIdx < t; caseIdx++ {
		var n, k int
		fmt.Fscan(in, &n, &k)

		totalSubsegments := n * (n + 1) / 2
		// We need the number of sorted subsegments to be exactly target
		target := totalSubsegments - k

		// Check if a valid partition exists
		if target < 0 || target > 465 || dp[n][target] == 0 {
			fmt.Fprintln(out, 0)
			continue
		}

		// Reconstruct the partition lengths
		var lengths []int
		currN := n
		currScore := target
		for currN > 0 {
			l := dp[currN][currScore]
			lengths = append(lengths, l)
			currN -= l
			currScore -= l * (l + 1) / 2
		}

		// Construct the permutation
		// We assign largest available values to the first block, next largest to the second, etc.
		// Within each block, values are sorted in increasing order.
		// This ensures descents occur exactly between blocks.
		perm := make([]int, 0, n)
		currentMax := n
		for _, l := range lengths {
			start := currentMax - l + 1
			for val := start; val <= currentMax; val++ {
				perm = append(perm, val)
			}
			currentMax -= l
		}

		// Output the permutation
		for i, val := range perm {
			if i > 0 {
				fmt.Fprint(out, " ")
			}
			fmt.Fprint(out, val)
		}
		fmt.Fprintln(out)
	}
}
```