while true {
let char = readLine()!.split(separator: " ").map{Int(String($0))!}
guard char[0] != 0 else { break }
let n = char[0]
var visited = [Bool](repeating: false, count: n+1)
var result = ""
func dfs(_ start:Int,_ depth:Int,_ str:String) {
if depth == 6 {
result.append(str+"\n")
}
for i in start...n {
if !visited[i] {
visited[i] = true
dfs(i, depth+1, str + "\(char[i]) ")
visited[i] = false
}
}
}
dfs(1,0,"")
print(result)
}