using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("6 4");
WillReturn.Add("356 migoro");
WillReturn.Add("461 yoroi");
WillReturn.Add("2 ni");
WillReturn.Add("12 ini");
}
else if (InputPattern == "Input2") {
WillReturn.Add("3 4");
WillReturn.Add("21 aaa");
WillReturn.Add("12 aaa");
WillReturn.Add("123 aaaaaa");
WillReturn.Add("13 aaaa");
}
else if (InputPattern == "Input3") {
WillReturn.Add("2 3");
WillReturn.Add("12211 abcaaaaabcabc");
WillReturn.Add("2121 aaabcaaabc");
WillReturn.Add("222221 aaaaaaaaaaabc");
}
else if (InputPattern == "Input4") {
WillReturn.Add("2 1");
WillReturn.Add("12 abcab");
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
struct VWInfoDef
{
internal string V;
internal string W;
}
static List<VWInfoDef> mVWInfoList = new List<VWInfoDef>();
static void Main()
{
List<string> InputList = GetInputList();
int[] wkArr = InputList[0].Split(' ').Select(pX => int.Parse(pX)).ToArray();
int K = wkArr[0];
foreach (string EachStr in InputList.Skip(1)) {
string[] SplitArr = EachStr.Split(' ');
VWInfoDef WillAdd;
WillAdd.V = SplitArr[0];
WillAdd.W = SplitArr[1];
mVWInfoList.Add(WillAdd);
}
IEnumerable<int[]> Query =
RubyPatternClass<int>.RepeatedPermutation(new int[] { 1, 2, 3 }, K);
foreach (int[] EachLenArr in Query) {
bool HasAnswer;
Dictionary<char, string> StrDict;
DeriveAnswer(EachLenArr, out HasAnswer, out StrDict);
if (HasAnswer) {
foreach (var EachPair in StrDict.OrderBy(pX => pX.Key)) {
Console.WriteLine(EachPair.Value);
}
break;
}
}
}
// 1から9に対応する長さの配列を引数として、解の有無と解を返す
static void DeriveAnswer(int[] pLenArr, out bool pHasAnswer, out Dictionary<char, string> pStrDict)
{
pHasAnswer = false;
// 確定した文字列[数字]なDict
pStrDict = new Dictionary<char, string>();
foreach (VWInfoDef EachVWInfo in mVWInfoList) {
string WArr = EachVWInfo.W;
foreach (char EachNum in EachVWInfo.V) {
int StrLen = pLenArr[EachNum - '1'];
// 長さ不足ならNG
if (WArr.Length < StrLen) return;
// 既出だが、異なる文字ならNG
if (pStrDict.ContainsKey(EachNum)) {
if (WArr.StartsWith(pStrDict[EachNum]) == false) {
return;
}
}
pStrDict[EachNum] = WArr.Substring(0, StrLen);
WArr = WArr.Substring(StrLen);
}
// 文字が残ってたらNG
if (WArr.Length > 0) {
return;
}
}
pHasAnswer = true;
}
}
#region RubyPatternClass
// Rubyの場合の数
internal static class RubyPatternClass<Type>
{
// 重複順列を返す
private struct JyoutaiDef_RepeatedPermutation
{
internal List<int> SelectedIndList;
}
internal static IEnumerable<Type[]> RepeatedPermutation(IEnumerable<Type> pEnum, int pR)
{
if (pR == 0) yield break;
Type[] pArr = pEnum.ToArray();
var Stk = new Stack<JyoutaiDef_RepeatedPermutation>();
JyoutaiDef_RepeatedPermutation WillPush;
for (int I = pArr.GetUpperBound(0); 0 <= I; I--) {
WillPush.SelectedIndList = new List<int>() { I };
Stk.Push(WillPush);
}
while (Stk.Count > 0) {
JyoutaiDef_RepeatedPermutation Popped = Stk.Pop();
// クリア判定
if (Popped.SelectedIndList.Count == pR) {
var WillReturn = new List<Type>();
Popped.SelectedIndList.ForEach(X => WillReturn.Add(pArr[X]));
yield return WillReturn.ToArray();
continue;
}
for (int I = pArr.GetUpperBound(0); 0 <= I; I--) {
WillPush.SelectedIndList = new List<int>(Popped.SelectedIndList) { I };
Stk.Push(WillPush);
}
}
}
}
#endregion