#include <bits/stdc++.h>
using namespace std;
int n;
string op,ad;
map<string,int>add;
int maxm[5]={255,255,255,255,65535};//abcde的大小
bool check(string ad){
int p=0,tp=0,ccf=0,num=0;//p是点,tp是冒号,ccf是记录到第几个数,num是当前的数的大小
bool zero1=false,zero2=false;//第一、二个前导零
for(int i=0;i<ad.length();i++){
if(ad[i]=='.')p++;
if(ad[i]==':'){
if(p!=3)return false;
tp++;
}
}
if(p!=3&&tp!=1)return false;
ad+='.';//方便后面判定
for(int i=0;i<ad.length();i++){
if(ad[i]!='.'&&ad[i]!=':'){
num=num*10+ad[i]-'0';//逐位读入num
if(!num){
if(zero1)zero2=true;
else zero1=true;
}//num读到现在是0说明有前导0
if(num>maxm[ccf])return false;
}
else{
if(i<ad.length()-1){
if((ad[i+1]=='.'||ad[i+1]==':'))return false;//判定有无相邻的点和冒号
}
if(i==0)return false;//判定第一位
if(zero1&&(zero2||num))return false;//有前导0且非0或者有两个前导0
num=0;ccf++;zero1=false;zero2=false;
}
}
return true;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>op>>ad;
if(!check(ad)){
cout<<"ERR"<<endl;
continue;
}
if(op=="Server"){
if(add.find(ad)!=add.end())cout<<"FAIL"<<endl;
else{
add.insert(make_pair(ad,i));
cout<<"OK"<<endl;
}
}
else{
if(add.find(ad)==add.end())cout<<"FAIL"<<endl;
else cout<<add[ad]<<endl;
}
}
return 0;
}