#include<iostream>
#include<list>
#include<string>
using namespace std;
class word{
private:
list<word*> nextWord_List;
list<int> nextLen;
string * str;
int count;
public:
word():count(0){}
void countEsc(){count++;}
void countDesc(){count--;}
bool count_In_Range(){return count<2;}
string * getStr()const{return str;}
void setStr(string *str){this->str=str;}
void clearStr(){
delete str;
str=nullptr;
}
list<word*>* getList(){return &nextWord_List;}
list<int>* getNextLen(){return &nextLen;}
void getAllWord_which_StartWithThisEnd(word * const allWord,int count){
for(int i=1;i<(int)str->size();i++){
for(int j=0;j<count;j++){
string *ortherStr=allWord[j].getStr();
string SubStr=str->substr(i);
if(
ortherStr->substr(0,SubStr.size())==SubStr
&& SubStr.size()!=ortherStr->size()
)
{
nextLen.push_back(ortherStr->size()-SubStr.size());
nextWord_List.push_back(allWord+j);
}
}
}
}
};
int MaxLength=0;
list<word*> dragon;
void creatDragon(list<word*>* newWords,list<int> *nextLen){
static int length=0;
auto it=newWords->begin();
auto itLen=nextLen->begin();
for(;it!=newWords->end();){
word *nextWord=*it;
bool FlagPush;
if(nextWord->count_In_Range()){
dragon.push_back(nextWord);
FlagPush=true;
nextWord->countEsc();
length+=(*itLen);
creatDragon(nextWord->getList(),nextWord->getNextLen());
}else FlagPush=false;
if(FlagPush){
dragon.pop_back();
(*it)->countDesc();
length-=(*itLen);
}
it++;
itLen++;
}
if(length>MaxLength)MaxLength=length;
};
int main(){
int N;
cin>>N;
word allWord[N];
string *str=new string[N];
for(int i=0;i<N;i++){
cin>>str[i];
allWord[i].setStr(str+i);
}
char beginChar;
cin>>beginChar;
for(int i=0;i<N;i++) allWord[i].getAllWord_which_StartWithThisEnd(allWord,N);
{
}
list<word*> BeginWord;
list<int> BeginLength;
for(int i=0;i<N;i++){
if(beginChar == allWord[i].getStr()->at(0))
{
BeginWord.push_back(allWord+i);
BeginLength.push_back(allWord[i].getStr()->size());
}
}
for(int i=0;i<N;i++) allWord[i].clearStr();
creatDragon(&BeginWord,&BeginLength);
cout<<MaxLength<<endl;
}