function dispatch(trains, inOrder, outOrder) {
  if (outOrder.length === num) {
    res.push(parseInt(outOrder.join("")));
  }
  if (trains.length === num) {
    inOrder.push(trains.shift());
    dispatch(trains, inOrder, outOrder);
    return;
  }
  if (trains.length > 0) {
    let cur = trains.shift();
    inOrder.push(cur);
    dispatch(trains, inOrder, outOrder);
    inOrder.pop();
    trains.unshift(cur);
  }
  if (inOrder.length > 0) {
    let cur = inOrder.pop();
    outOrder.push(cur);
    dispatch(trains, inOrder, outOrder);
    outOrder.pop();
    inOrder.push(cur);
  }
}

let num;
let res = [];
while ((num = parseInt(readline()))) {
  let trains = readline().split(" ");
  let inOrder = [];
  let outOrder = [];
  dispatch(trains, inOrder, outOrder);
  res.sort((a, b) => a - b);
  res.forEach((el) => {
    print(el.toString().split("").join(" "));
  });
}