#include using namespace std; namespace { int N; int M; char A[ 27 ][ 27 ]; char B[ 27 ][ 27 ]; char S[ 300 ]; int SearchA( int &iPos, const char C ) { int i; for( i = 0; i < N; i++ ) { iPos = 0; while( A[ i ][ iPos ] != '\0' ) { if( A[ i ][ iPos ] == C ) { return i; } iPos++; } } return - 1; } int SearchB( int &iPos, const char C ) { int i; for( i = 0; i < M; i++ ) { iPos = 0; while( B[ i ][ iPos ] != '\0' ) { if( B[ i ][ iPos ] == C ) { return i; } iPos++; } } return - 1; } int SetSet( char *X, const char *Y ) { int i; i = 0; while( Y[ i ] != 0 ) { X[ i ] = Y[ i ]; i++; } return i; } } // namespace int main( int argc, char *argv[] ) { int i; int j; int k; int iStart; int iEnd; int iLen; int iNum; int iPos; int iFlag; ios::sync_with_stdio( false ); cin.tie( 0 ); cin >> N; iLen = 0; for( i = 0; i < N; i++ ) { cin >> A[ i ]; iLen += strlen( A[ i ] ); } cin >> M; j = 0; for( i = 0; i < M; i++ ) { cin >> B[ i ]; j += strlen( B[ i ] ); } // Debug if( j != iLen ) { cout << "Err" << endl; return 0; } iFlag = 0; iNum = 0; iStart = 100; iEnd = 100 + strlen( A[ iNum ] ); strcpy( S + iStart, A[ iNum ] ); j = SearchB( iPos, S[ iStart ] ); while( j >= 0 && iPos > 0 ) { iStart -= iPos; SetSet( S + iStart, B[ j ] ); j = SearchA( iPos, S[ iStart ] ); if( j < 0 || iPos <= 0 ) break; iStart -= iPos; SetSet( S + iStart, A[ j ] ); j = SearchB( iPos, S[ iStart ] ); } j = SearchB( iPos, S[ iEnd - 1 ] ); while( j >= 0 ) { k = strlen( B[ j ] ); if( k <= 1 || iPos >= k - 1 ) break; strcpy( S + iEnd - iPos - 1, B[ j ] ); iEnd += k - iPos - 1; j = SearchA( iPos, S[ iEnd - 1 ] ); k = strlen( A[ j ] ); if( j < 0 || k <= 1 || iPos >= k - 1 ) break; strcpy( S + iEnd - iPos - 1, A[ j ] ); iEnd += k - iPos - 1; j = SearchB( iPos, S[ iEnd - 1 ] ); } S[ iEnd ] = '\0'; if( iEnd - iStart == iLen ) cout << S + iStart << endl; else cout << - 1 << endl; return 0; }