← Home
```go
package main

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

func writeInt(out *bufio.Writer, v int) {
	if v == 0 {
		out.WriteByte('0')
		return
	}
	var buf [10]byte
	i := 9
	for v > 0 {
		buf[i] = byte(v%10 + '0')
		v /= 10
		i--
	}
	out.Write(buf[i+1:])
}

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

	var t int
	fmt.Fscan(in, &t)

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

		q := 1
		p := 1
		for p < n {
			q++
			p *= 3
		}

		c := byte((2 * q) % 3)

		S := make([]byte, q)
		if q%3 == 0 {
		} else if q%3 == 1 {
			S[0] = 1
			S[1] = 1
		} else if q%3 == 2 {
			S[0] = 1
		}

		complement := func(w []byte) []byte {
			cw := make([]byte, q)
			for i, x := range w {
				if x == 2 {
					cw[i] = 2
				} else {
					cw[i] = 1 - x
				}
			}
			return cw
		}

		encode := func(w []byte) int {
			res := 0
			for _, x := range w {
				res = res*3 + int(x)
			}
			return res
		}

		decode := func(val int) []byte {
			w := make([]byte, q)
			for i := q - 1; i >= 0; i-- {
				w[i] = byte(val % 3)
				val /= 3
			}
			return w
		}

		maxVal := 1
		for i := 0; i < q; i++ {
			maxVal *= 3
		}
		used := make([]bool, maxVal)

		chosen := make([][]byte, 0, n)

		chosen = append(chosen, S, complement(S))
		used[encode(S)] = true
		used[encode(complement(S))] = true

		if n%2 == 1 {
			twos := make([]byte, q)
			for i := range twos {
				twos[i] = 2
			}
			chosen = append(chosen, twos)
			used[encode(twos)] = true
		}

		neededPairs := n / 2
		pairsFound := 1

		for i := 0; i < maxVal && pairsFound < neededPairs; i++ {
			if used[i] {
				continue
			}
			w := decode(i)
			sum := 0
			for _, x := range w {
				sum += int(x)
			}
			if sum%3 != int(c) {
				continue
			}
			cw := complement(w)
			encC := encode(cw)
			if i == encC {
				continue
			}
			chosen = append(chosen, w, cw)
			used[i] = true
			used[encC] = true
			pairsFound++
		}

		writeInt(out, q)
		out.WriteByte('\n')
		for i := 0; i < q; i++ {
			var L_cands, R_cands []int
			for idx, w := range chosen {
				cand := idx + 1
				if w[i] == 0 {
					L_cands = append(L_cands, cand)
				} else if w[i] == 1 {
					R_cands = append(R_cands, cand)
				}
			}
			k := len(L_cands) + len(R_cands)
			writeInt(out, k)
			for _, cand := range L_cands {
				out.WriteByte(' ')
				writeInt(out, cand)
			}
			for _, cand := range R_cands {
				out.WriteByte(' ')
				writeInt(out, cand)
			}
			out.WriteByte('\n')
		}
		out.Flush()

		var s string
		fmt.Fscan(in, &s)

		for idx, w := range chosen {
			cand := idx + 1
			match := true
			for i := 0; i < q; i++ {
				if s[i] == '?' {
					continue
				}
				var expected byte
				if w[i] == 0 {
					expected = 'L'
				} else if w[i] == 1 {
					expected = 'R'
				} else {
					expected = 'N'
				}
				if s[i] != expected {
					match = false
					break
				}
			}
			if match {
				writeInt(out, cand)
				out.WriteByte('\n')
				out.Flush()
				break
			}
		}
	}
}
```