atcoder#ABC268D. [ABC268D] Unique Username

[ABC268D] Unique Username

Score : 400400 points

Problem Statement

Takahashi is having trouble with deciding a username for a service. Write a code to help him.

Find a string XX that satisfies all of the following conditions:

  • XX is obtained by the following procedure:- Let S1,S2,,SNS_1', S_2', \ldots,S_N' be a permutation of S1,S2,,SNS_1, S_2, \ldots,S_N. Let XX be the concatenation of S1S_1', (11 or more copies of _), S2S_2', (11 or more copies of _), \ldots, (11 or more copies of _), and SNS_N', in this order.
  • Let S1,S2,,SNS_1', S_2', \ldots,S_N' be a permutation of S1,S2,,SNS_1, S_2, \ldots,S_N. Let XX be the concatenation of S1S_1', (11 or more copies of _), S2S_2', (11 or more copies of _), \ldots, (11 or more copies of _), and SNS_N', in this order.
  • The length of XX is between 33 and 1616, inclusive.
  • XX does not coincide with any of MM strings T1,T2,,TMT_1,T_2,\ldots,T_M.

If there is no XX that satisfies all of the conditions, print -1 instead.

Constraints

  • 1N81 \leq N \leq 8
  • 0M1050 \leq M \leq 10^5
  • NN and MM are integers.
  • 1Si161 \leq |S_i| \leq 16
  • N1+Si16N-1+\sum{|S_i|} \leq 16
  • SiSjS_i \neq S_j if iji \neq j.
  • SiS_i is a string consisting of lowercase English letters.
  • 3Ti163 \leq |T_i| \leq 16
  • TiTjT_i \neq T_j if iji \neq j.
  • TiT_i is a string consisting of lowercase English letters and _.

Input

Input is given from Standard Input in the following format:

NN MM

S1S_1

S2S_2

\vdots

SNS_N

T1T_1

T2T_2

\vdots

TMT_M

Output

Print a string XX that satisfies all of the conditions. If there is no XX that satisfies all of the conditions, print -1 instead. If there are multiple solutions, print any of them.

1 1
chokudai
chokudai
-1

The only string that satisfies the first and second conditions is X=X= chokudai, but it coincides with T1T_1. Thus, there is no XX that satisfies all of the conditions, so -1 should be printed.

2 2
choku
dai
chokudai
choku_dai
dai_choku

Strings like choku__dai (which has two _'s between choku and dai) also satisfy all of the conditions.

2 2
chokudai
atcoder
chokudai_atcoder
atcoder_chokudai
-1

chokudai__atcoder and atcoder__chokudai (which have two _'s between chokudai and atcoder) have a length of 1717, which violates the second condition.

4 4
ab
cd
ef
gh
hoge
fuga
____
_ab_cd_ef_gh_
ab__ef___cd_gh

The given TiT_i may contain a string that cannot be obtained by the procedure described in the first condition.