#include<bits/stdc++.h>
using namespace std;
int n,cnt;
string s[1005];
int num[1005];
bool hg(string x){
long long dot=0,m=0,sum=-1,f=0,tmp=1;
for(int i=0;i<x.length();i++){
sum=-1,tmp=1,f=0;
if(x[i]!=':'&&x[i]!='.'&&(x[i]<'0'||x[i]>'9')) return false;
if(x[i]==':'){
m++;
int j=i-1;
while(j>=0&&'0'<=x[j]&&x[j]<='9'){
if(sum==-1) sum+=1+x[j]-'0';
else sum+=(x[j]-'0')*pow(10,tmp),tmp++;
j--;
}
if(sum==0||x[j+1]>'0') f=1;
if(0>sum||sum>255||f==0) return false;
sum=-1,tmp=1,f=0;
for(j=i+1;j<x.length();j++)
if(x[j]!=':'&&x[j]!='.'&&(x[j]<'0'||x[j]>'9')) return false;
j=x.length()-1;
while(j>i&&'0'<=x[j]&&x[j]<='9'){
if(sum==-1) sum+=1+x[j]-'0';
else sum+=(x[j]-'0')*pow(10,tmp),tmp++;
j--;
}
if(sum==0||x[j+1]>'0') f=1;
if(0>sum||sum>65535||f==0) return false;
}
if(x[i]=='.'){
if(m>0) return false;
dot++;
int j=i-1;
while(j>=0&&'0'<=x[j]&&x[j]<='9'){
if(sum==-1) sum+=1+x[j]-'0';
else sum+=(x[j]-'0')*pow(10,tmp),tmp++;
j--;
}
if(sum==0||x[j+1]>'0') f=1;
if(0>sum||sum>255||f==0) return false;
}
}
if(dot!=3||m!=1) return false;
return true;
}
bool cf(string x){
for(int i=1;i<=cnt;i++)
if(s[i]==x) return false;
return true;
}
int get(string x){
for(int i=1;i<=cnt;i++)
if(s[i]==x) return num[i];
return 0;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
string opt,x; cin>>opt>>x;
if(opt[0]=='S'){
if(!hg(x)) cout<<"ERR"<<'\n';
else if(!cf(x)) cout<<"FAIL"<<'\n';
else{
cout<<"OK"<<'\n';
s[++cnt]=x; num[cnt]=i;
}
}
else{
if(!hg(x)) cout<<"ERR"<<'\n';
else if(!get(x)) cout<<"FAIL"<<'\n';
else cout<<get(x)<<'\n';
}
}
return 0;
}