Lara Croft, the fiercely independent daughter of a missing adventurer, must push herself beyond her limits when she discovers the island where her father disappeared. In this mysterious island, Lara finds a tomb with a very heavy door. To open the door, Lara must input the password at the stone keyboard on the door. But what is the password? After reading the research notes written in her father's notebook, Lara finds out that the key is on the statue beside the door.

The statue is wearing many arm rings on which some letters are carved. So there is a string on each ring. Because the letters are carved on a circle and the spaces between any adjacent letters are all equal, any letter can be the starting letter of the string. The longest common subsequence (let's call it "LCS") of the strings on all rings is the password. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.

For example, there are two strings on two arm rings: <var>s1</var> = "abcdefg" and <var>s2</var> = "zaxcdkgb". Then "acdg" is a LCS if you consider 'a' as the starting letter of <var>s1</var>, and consider 'z' or 'a' as the starting letter of <var>s2</var>. But if you consider 'd' as the starting letter of <var>s1</var> and <var>s2</var>, you can get "dgac" as a LCS. If there are more than one LCS, the password is the one which is the smallest in lexicographical order.

Please find the password for Lara.

Input

There are no more than 10 test cases.

In each case:

The first line is an integer <var>n</var>, meaning there are <var>n</var> (0 < <var>n</var> ≤ 10) arm rings.

Then <var>n</var> lines follow. Each line is a string on an arm ring consisting of only lowercase letters. The length of the string is no more than 8.

Output

For each case, print the password. If there is no LCS, print 0 instead.

Sample Input

2
abcdefg
zaxcdkgb
5
abcdef
kedajceu
adbac
abcdef
abcdafc
2
abc
def

Sample Output

acdg
acd
0

题意:
每一组数据给你一个整数N,然后N个字符串,每一个字符串可以以字符串中的任意一个字符为起始字符(循环连接)
例如 asdb 可以变成 sdba
现在你可以任意变换这N这个字符串,求他们的最长的公共子序列LCS ,如果长度相等,请输出字典序最小的。

题意:
二进制枚举+暴力
我们对每一个字符串,我们枚举它的所有可能变成的字符串(只有字符串的长度数量的可能)。
然后对于每一个字符串我们二进制枚举它的所有可能子序列,然后用一个map<string,set<int> > m; 来维护。
对于每一个枚举出的子序列,把它加入map中,映射的set中加入当前子序列的编号,如果某一个子序列的set的大小是N,
那么说明 N 个字符串都有这个子序列,即N个字符串的公共子序列,然后选择长度最大,字典序最小的即可。

细节见code
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
string str[13];
string temp[15];
int n;
map<string,set<int> > m;
string ans="";
void dfs(int id)
{
    int len=str[id].length();
    rep(i,0,len)
    {
        temp[id]=str[id].substr(i,len-i)+str[id].substr(0,i);
        int xlen=temp[id].size();
        string lcs="";
        for(int i=1;i<=(1<<xlen)-1;i++)
        {
            lcs="";
            for(int j=0;j<xlen;j++)
            {
                 if((bool)(i&(1<<j)))
                 {
                    lcs+=temp[id][j];
                 }
            }
            // db(lcs);
            m[lcs].insert(id);
            if(m[lcs].size()==n)
            {
                if(sz(lcs)>sz(ans))
                {
                    ans=lcs;
                }else if(sz(lcs)==sz(ans))
                {
                    ans=min(ans,lcs);
                }
            }
        }
    }
    if(id<n)
        dfs(id+1);
}
int main()
{
//    freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//    freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
    gbtb;
//    cout<<(1<<8)<<endl;
    // 8*256*10
    while(cin>>n)
    {
        repd(i,1,n)
        {
            cin>>str[i];
        }
        ans="";
        m.clear();
        dfs(1);
        if(!sz(ans))
        {
            cout<<0<<endl;
        }else
        {
            cout<<ans<<endl;
        }
    }



    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}