use std::io::prelude::*; use std::io::stdin; use std::fmt::Debug; use std::str; #[derive(Debug, Clone)] struct List { pub prev: Option, pub next: Option, pub used: bool, } fn main() { let mut list: Vec = vec![List { prev: None, next: None, used: false }; 26]; let n: usize = { let mut s = String::new(); stdin().read_line(&mut s).unwrap(); s.trim().parse().unwrap() }; if n == 1 { let mut s = String::new(); stdin().read_line(&mut s).unwrap(); println!("{}", s.trim()); return; } for i in 0..n { let mut s = String::new(); stdin().read_line(&mut s).unwrap(); if s.trim().len() == 1 { let i = (s.as_bytes()[0] - 'A' as u8) as usize; list[i].used = true; } for b in s.trim().as_bytes().windows(2) { let i = (b[0] - 'A' as u8) as usize; let j = (b[1] - 'A' as u8) as usize; list[i].next = Some(j); list[j].prev = Some(i); list[i].used = true; list[j].used = true; } } let n: usize = { let mut s = String::new(); stdin().read_line(&mut s).unwrap(); s.trim().parse().unwrap() }; if n == 1 { let mut s = String::new(); stdin().read_line(&mut s).unwrap(); //println!("{}", s.trim()); return; } for i in 0..n { let mut s = String::new(); stdin().read_line(&mut s).unwrap(); if s.trim().len() == 1 { let i = (s.as_bytes()[0] - 'A' as u8) as usize; list[i].used = true; } for b in s.trim().as_bytes().windows(2) { let i = (b[0] - 'A' as u8) as usize; let j = (b[1] - 'A' as u8) as usize; list[i].next = Some(j); list[j].prev = Some(i); list[i].used = true; list[j].used = true; } } let mut cursor = None; let mut s = vec![]; let mut count = 0; for (i,c) in list.iter().enumerate() { if c.prev.is_none() && c.next.is_some() { assert!(cursor.is_none()); cursor = c.next.clone(); s.push(i as u8 + 'A' as u8); } if c.used { count += 1; println!("{}: {:?}", (i as u8 + 'A' as u8) as char, c); } } if cursor.is_none() { println!("-1"); } else { while cursor.is_some() { let n = *cursor.as_ref().unwrap(); s.push(n as u8 + 'A' as u8); cursor = list[n].next; } } if s.len() != count { println!("-1"); } else { println!("{}", str::from_utf8(&s).unwrap()); } }