#include <iostream>
#include <cstdio>
#include <string>
#include <set>
#include <map>
#include <vector>
using namespace std;
//英语 看博友分析 抄博友程序 模拟 文件输入
void fen(string s,string &mta,string &user)
{
int p=s.find('@');
user=s.substr(0,p);
mta=s.substr(p+1);
}
int main()
{
//freopen("1876.txt","r",stdin);
string t;
set<string> addr;
while(cin>>t && t!="*")
{
string mta;
int n;
cin>>mta>>n;
while(n--)
{
string user;
cin>>user;
addr.insert(user+"@"+mta);//所有存在的用户和地址
}
}
while(cin>>t && t!="*")
{
string mta,user;//发件地址 发件人
fen(t,mta,user);
//cout<<t<<" "<<mta<<" "<<user<<endl;
set<string> shou;//收件地址和收件人
vector<string> sd;//收件地址
map<string,vector<string> > mp;//相同收件地址的不同收件人及其收件地址
while(cin>>t && t!="*")
{
if(shou.count(t))
{
continue;
}
shou.insert(t);
string mta2,user2;//收件地址和收件人
fen(t,mta2,user2);
if(mp.count(mta2)==0)
{
sd.push_back(mta2);
mp[mta2]=vector<string>();
}
mp[mta2].push_back(t);
}
getline(cin,t);
string mes;//文本
while(getline(cin,t) && t!="*")
{
mes+=" "+t+"\n";//抄博友程序
}
for(int i=0;i<sd.size();i++)//遍历收件人地址
{
cout<<"Connection between "<<mta<<" and "<<sd[i]<<endl;//抄博友程序
cout<<" HELO "<<mta<<endl;
cout<<" 250\n";
cout<<" MAIL FROM:<"<<user<<'@'<<mta<<">\n";
cout<<" 250\n";
int mark=0;
vector<string> tv=mp[sd[i]];//方便程序书写
for(int j=0;j<tv.size();j++)
{
cout<<" RCPT TO:<"<<tv[j]<<">\n";//抄博友程序
if(addr.count(tv[j])) { mark = 1;cout<<" 250\n";}//收件信息准确
else cout<<" 550\n";
}
if(mark)
{
cout<<" DATA\n 354\n";//抄博友程序
cout<<mes<<" .\n 250\n";
}
cout<<" QUIT\n 221\n";
}
}
return 0;
}