package main import ( "bufio" "fmt" "os" "strconv" "strings" ) func configure(scanner *bufio.Scanner) { scanner.Split(bufio.ScanWords) scanner.Buffer(make([]byte, 1000005), 1000005) } func getNextString(scanner *bufio.Scanner) string { scanned := scanner.Scan() if !scanned { panic("scan failed") } return scanner.Text() } func getNextInt(scanner *bufio.Scanner) int { i, _ := strconv.Atoi(getNextString(scanner)) return i } func getNextInt64(scanner *bufio.Scanner) int64 { i, _ := strconv.ParseInt(getNextString(scanner), 10, 64) return i } func getNextFloat64(scanner *bufio.Scanner) float64 { i, _ := strconv.ParseFloat(getNextString(scanner), 64) return i } func main() { fp := os.Stdin wfp := os.Stdout extra := 0 if os.Getenv("I") == "IronMan" { fp, _ = os.Open(os.Getenv("END_GAME")) extra = 100 } scanner := bufio.NewScanner(fp) configure(scanner) writer := bufio.NewWriter(wfp) defer func() { r := recover() if r != nil { fmt.Fprintln(writer, r) } writer.Flush() }() solve(scanner, writer) for i := 0; i < extra; i++ { fmt.Fprintln(writer, "-----------------------------------") solve(scanner, writer) } } func solve(scanner *bufio.Scanner, writer *bufio.Writer) { n := getNextInt(scanner) aa := newAccessary() for i := 0; i < n; i++ { a := getNextString(scanner) for j := 0; j < len(a); j++ { aa.used[a[j]-'A'] = true if j > 0 { aa.prev[a[j]-'A'] = int(a[j-1] - 'A') } if j+1 < len(a) { aa.next[a[j]-'A'] = int(a[j+1] - 'A') } } } m := getNextInt(scanner) bb := newAccessary() for i := 0; i < m; i++ { a := getNextString(scanner) for j := 0; j < len(a); j++ { bb.used[a[j]-'A'] = true if j > 0 { bb.prev[a[j]-'A'] = int(a[j-1] - 'A') } if j+1 < len(a) { bb.next[a[j]-'A'] = int(a[j+1] - 'A') } } } for i := 0; i < 26; i++ { if aa.prev[i] != bb.prev[i] { if aa.prev[i] == -1 { aa.prev[i] = bb.prev[i] } else { bb.prev[i] = aa.prev[i] } } if aa.next[i] != bb.next[i] { if aa.next[i] == -1 { aa.next[i] = bb.next[i] } else { bb.next[i] = aa.next[i] } } } head := -1 for i := 0; i < 26; i++ { if aa.used[i] && bb.used[i] && aa.prev[i] == -1 && bb.prev[i] == -1 { if head != -1 { fmt.Fprintln(writer, -1) return } head = i } } ans := make([]string, 0) for head != -1 { ans = append(ans, fmt.Sprintf("%c", 'A'+head)) head = aa.next[head] } fmt.Fprintln(writer, strings.Join(ans, "")) } type accessary struct { used [26]bool prev, next [26]int } func newAccessary() *accessary { a := &accessary{} for i := 0; i < 26; i++ { a.prev[i] = -1 a.next[i] = -1 } return a }