package main import ( "bufio" "fmt" "log" "os" "strconv" ) func main() { log.SetFlags(log.Lshortfile) // log.Println("hello") sc := newScanner() N := sc.nextInt() A := make([]string, N) for i := 0; i < N; i++ { A[i] = sc.nextLine() } M := sc.nextInt() B := make([]string, M) for i := 0; i < M; i++ { B[i] = sc.nextLine() } var c int var s, t string for i := 0; i < N; i++ { for j := 0; j < M; j++ { if A[i][0] == B[j][0] { c++ s += A[i] t += B[j] } } } if c != 1 { fmt.Println("-1") } else { for len(s) != len(t) { // 短い方に足していく if len(s) > len(t) { d := s[len(t)] for j := 0; j < M; j++ { if B[j][0] == d { t += B[j] break } } } else { d := t[len(s)] for i := 0; i < N; i++ { if A[i][0] == d { s += A[i] break } } } } fmt.Println(s) } return } type scanner struct { r *bufio.Reader buf []byte p int } func newScanner() *scanner { rdr := bufio.NewReaderSize(os.Stdin, 1000) return &scanner{r: rdr} } func (s *scanner) next() string { s.pre() start := s.p for ; s.p < len(s.buf); s.p++ { if s.buf[s.p] == ' ' { break } } result := string(s.buf[start:s.p]) s.p++ return result } func (s *scanner) nextLine() string { s.pre() start := s.p s.p = len(s.buf) return string(s.buf[start:]) } func (s *scanner) nextInt() int { v, _ := strconv.Atoi(s.next()) return v } func (s *scanner) pre() { if s.p >= len(s.buf) { s.readLine() s.p = 0 } } func (s *scanner) readLine() { s.buf = make([]byte, 0) for { l, p, e := s.r.ReadLine() if e != nil { panic(e) } s.buf = append(s.buf, l...) if !p { break } } }