#include <bits/stdc++.h>
using namespace std;
struct node{
string op;
string ad;
int con;
};
vector<node> v;
int cnt = 0;
vector<string> spilt(string s,const string c){
int now = 0;
vector<string> a;
now = s.find(c.c_str());
while(now != -1) {
string temp = s.substr(0, now);
a.push_back(temp);
s = s.substr(now + 1);
now = s.find(c.c_str());
}
a.push_back(s);
return a;
}
int check(string op,string ad){
cnt++;
if (op == "Server"){
int n = ad.size();
vector<string> a = spilt(ad,":");
if (a.size() != 2) return 0;
vector<string> b = spilt(a[0],".");
if (b.size() != 4) return 0;
int e = stoi(a[1]);
if (e > 65535) return 0;
for (auto i : b){
int x = stoi(i);
if (x > 255) return 0;
}
for (auto i : v){
if (i.ad == ad) return -1;
}
node x;
x.ad = ad;
x.op = op;
x.con = cnt;
v.push_back(x);
return -10;
} else {
int n = ad.size();
vector<string> a = spilt(ad,":");
if (a.size() != 2) return 0;
vector<string> b = spilt(a[0],".");
if (b.size() != 4) return 0;
int e = stoi(a[1]);
if (e > 65535) return 0;
for (auto i : b){
int x = stoi(i);
if (x > 255) return 0;
}
for (int i = 0;i < v.size();i++){
if (v[i].ad == ad) return v[i].con;
}
return -1;
}
}
signed main(){
int n;
cin >> n;
while(n--){
string op,ad;
cin >> op >> ad;
for (int i = 0;i < op.size();i++){
if (op[i] == ' ') op[i] = '#';
}
for (int i = 0;i < ad.size();i++){
if (ad[i] == ' ') ad[i] = '#';
}
int x = check(op,ad);
if (x == -1) cout << "FAIL\n";
else if (x == 0) cout << "ERR\n";
else if (x == -10) cout << "OK\n";
else cout << x << "\n";
}
return 0;
}