#!/usr/bin/python3 import itertools N,K=map(int,input().split()) l=input().split() mx=max(list(map(lambda x:int(x[0]),l))) def sort_weight(_n): if len(_n)==2: return float(_n) if len(_n)==1: return float(_n)*10+mx+0.5 l2=sorted(l, key=sort_weight,reverse=True) count=0 for i in itertools.permutations(l2): print("".join(i)) count=count+1 if count==K: break