------
## 最终结论
```python
from collections import defaultdict, deque

def solve_cake_cutting(t, test_cases):
    results = []
    
    for case in range(t):
        n = test_cases[case][0]
        pieces = test_cases[case][1]

        # Step 1: Create adjacency list and degree count
        adj = defaultdict(set)
        degree = defaultdict(int)

        for (a, b, c) in pieces:
            adj[a].update([b, c])
            adj[b].update([a, c])
            adj[c].update([a, b])
            degree[a] += 1
            degree[b] += 1
            degree[c] += 1

        # Step 2: Find starting point (a vertex with degree 2)
        start_vertex = None
        for vertex in degree:
            if degree[vertex] == 2:
                start_vertex = vertex
                break

        # Step 3: Recover the order of vertices (p)
        p = [start_vertex]
        current = start_vertex
        visited_vertices = set(p)

        while len(p) < n:
            for neighbor in adj[current]:
                if neighbor not in visited_vertices:
                    p.append(neighbor)
                    visited_vertices.add(neighbor)
                    current = neighbor
                    break

        # Step 4: Determine the order of cutting pieces (q)
        q = []
        used_pieces = set()
        
        def is_valid(piece):
            a, b, c = piece
            return (a in visited_vertices and b in visited_vertices and c not in visited_vertices) or \
                   (b in visited_vertices and c in visited_vertices and a not in visited_vertices) or \
                   (c in visited_vertices and a in visited_vertices and b not in visited_vertices)

        while len(q) < n - 2:
            for i, piece in enumerate(pieces):
                if i not in used_pieces and is_valid(piece):
                    q.append(i + 1)  # Store the index of the piece (1-based)
                    used_pieces.add(i)
                    break
        
        results.append((p, q))
    
    return results

# Example usage:
t = 3
test_cases = [
    (6, [(3, 6, 5), (5, 2, 4), (5, 4, 6), (6, 3, 1)]),
    (6, [(2, 5, 6), (2, 5, 1), (4, 1, 2), (1, 3, 5)]),
    (3, [(1, 2, 3)])
]

results = solve_cake_cutting(t, test_cases)

for p_ordering, q_ordering in results:
    print(' '.join(map(str,p_ordering)))
    print(' '.join(map(str,q_ordering)))
```