def f(a, b) cnt = 0 r = "" a.each_with_index{|aa, i| used_a = [false] * A.size used_b = [false] * B.size g = lambda{|a, b| # p [a, b] # p [used_a, used_b] if used_a.all?{|v|v} && used_b.all?{|v|v} return a == b && a end if a.size > b.size B.each_with_index{|bb, i| next if used_b[i] new_b = b + bb len = [new_b.size, a.size].min if new_b[0...len] == a[0...len] used_b[i] = true r = g.(a, new_b) return r if r used_b[i] = false end } else A.each_with_index{|aa, i| next if used_a[i] new_a = a + aa len = [new_a.size, b.size].min if new_a[0...len] == b[0...len] used_a[i] = true r = g.(new_a, b) return r if r used_a[i] = false end } end false } used_a[i] = true if r = g.(aa, "") cnt += 1 end return false if cnt > 1 } r end N = gets.to_i A = N.times.map{gets.chomp} M = gets.to_i B = M.times.map{gets.chomp} r = f(A, B) puts r ? r : -1