fn main() { let mut n = String::new(); std::io::stdin().read_line(&mut n).ok(); let n: usize = n.trim().parse().unwrap(); let a = (0..n).map(|_| { let mut temp = String::new(); std::io::stdin().read_line(&mut temp).ok(); let temp = temp.trim(); temp.chars().collect::>() }) .collect::>(); let mut b = String::new(); std::io::stdin().read_line(&mut b).ok(); let m: usize = b.trim().parse().unwrap(); let b = (0..m).map(|_| { let mut temp = String::new(); std::io::stdin().read_line(&mut temp).ok(); let temp = temp.trim(); temp.chars().collect::>() }) .collect::>(); let length = a.iter().map(|v| v.len()).sum::(); let mut result = 0usize; let mut res = vec![]; for i in 0..n { let mut left = vec![]; let mut right = vec![]; a[i].iter().for_each(|&c| left.push(c)); while left.len() < length || right.len() < length { if left.len() < right.len() { let idx = left.len(); if let Some(x) = a.iter().filter(|&v| v[0] == right[idx]).nth(0) { x.iter().for_each(|&c| left.push(c)); } else { break; } } else if left.len() > right.len() { let idx = right.len(); if let Some(x) = b.iter().filter(|&v| v[0] == left[idx]).nth(0) { x.iter().for_each(|&c| right.push(c)); } else { break; } } else { println!("-1"); return; } } if left.len() == length && right.len() == length { result += 1; res = left.clone().to_owned(); } } if result == 1 { println!("{}", res.iter().map(|c| c.to_string()).collect::>().join("")); } else { println!("-1"); } }