using System; using System.Collections.Generic; using System.Text; using System.Linq; class Program { public void Proc() { List ac1 = new List(); int cnt = int.Parse(Reader.ReadLine()); for (int i = 0; i < cnt; i++) { ac1.Add(Reader.ReadLine()); } List ac2 = new List(); cnt = int.Parse(Reader.ReadLine()); for (int i = 0; i < cnt; i++) { ac2.Add(Reader.ReadLine()); } string ans = this.GetAns(ac1, ac2); Console.WriteLine(ans); } private string GetAns(List ac1, List ac2) { if (ac1.Count == 1) { return ac1[0]; } if (ac2.Count == 1) { return ac2[0]; } if(ac1.Count <= 0 || ac2.Count <= 0) { return "-1"; } for (int i = 0; i < ac1.Count; i++) { char target = ac1[i].First(); string tmp = ac2.Where(a => a.Contains(target)).First(); int idx = tmp.IndexOf(target); if (idx > 0) { char prevChar = tmp[idx - 1]; int idxPrev = ac1.IndexOf(ac1.Find(a => a.Last() == prevChar)); return GetAns(Join(ac1, idxPrev, i), ac2); } target = ac1[i].Last(); tmp = ac2.Where(a => a.Contains(target)).First(); idx = tmp.IndexOf(target); if (tmp.Last() != target) { char nextChar = tmp[idx + 1]; int idxNext = ac1.IndexOf(ac1.Find(a => a.First() == nextChar)); return GetAns(Join(ac1, i, idxNext), ac2); } } for (int i = 0; i < ac2.Count; i++) { char target = ac2[i].First(); string tmp = ac1.Where(a => a.Contains(target)).First(); int idx = tmp.IndexOf(target); if (idx > 0) { char prevChar = tmp[idx - 1]; int idxPrev = ac2.IndexOf(ac2.Find(a => a.Last() == prevChar)); return GetAns(ac1, Join(ac2, idxPrev, i)); } target = ac2[i].Last(); tmp = ac1.Where(a => a.Contains(target)).First(); idx = tmp.IndexOf(target); if (tmp.Last() != target) { char nextChar = tmp[idx + 1]; int idxNext = ac2.IndexOf(ac2.Find(a => a.First() == nextChar)); return GetAns(ac1, Join(ac2, i, idxNext)); } } return "-1"; } private List Join(List list, int idx1, int idx2) { List ret = new List(); ret.AddRange(list); ret.RemoveAt(Math.Max(idx1, idx2)); ret.RemoveAt(Math.Min(idx1, idx2)); ret.Add(list[idx1] + list[idx2]); return ret; } public class Reader { public static bool IsDebug = false; private static String PlainInput = @" 2 EA T 2 T EA "; private static System.IO.StringReader Sr = null; public static string ReadLine() { if (IsDebug) { if (Sr == null) { Sr = new System.IO.StringReader(PlainInput.Trim()); } return Sr.ReadLine(); } else { return Console.ReadLine(); } } } static void Main() { #if DEBUG Reader.IsDebug = true; #endif Program prg = new Program(); prg.Proc(); } }