import java.util.*;
import java.io.*;
public class Main implements Runnable {
static class Pair<T, K>{
public T first;
public K second;
public Pair(T t, K k){
this.first = t;
this.second = k;
}
}
public void run(){
try{
//Scanner is too slow for sports programming
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(stdin.readLine());
ArrayList<Pair<Pair<Integer, Integer>, Boolean>> rules = new ArrayList<>();
for(int i = 0; i < n; ++i){
String[] input = stdin.readLine().split(" from ");
Pair<Integer, Integer> obj = new Pair<>(0,0);
String[] ipAndMask = input[1].split("/");
String[] ipStrArr = ipAndMask[0].split("\\.");
for(int j = 0; j<4; ++j){
obj.first += Integer.parseInt(ipStrArr[j]) << 8*(3-j);
}
if(ipAndMask.length > 1){
int k = Integer.parseInt(ipAndMask[1]);
if(k == 32){
obj.second = ~0;
}else{
int mask = 1<<31;
for(int j =0; j<k; ++j){
obj.second |= mask;
mask>>= 1;
}
}
}else{
obj.second = ~0;
}
obj.first &= obj.second;
obj.second = ~obj.second;
rules.add(new Pair(obj, input[0].equals("allow")));
}
/*for(Pair<Pair<Integer, Integer>, Boolean> rule : rules){
System.out.println(
String.format("%32s %32s %s",
Integer.toBinaryString(rule.first.first),
Integer.toBinaryString(rule.first.second),
rule.second
)
);
}*/
int m = Integer.parseInt(stdin.readLine());
for(int i = 0; i < m; ++i){
String[] ipStr = stdin.readLine().split("\\.");
int ip = 0;
for(int j = 0; j<4; ++j){
ip += Integer.parseInt(ipStr[j]) << 8*(3-j);
}
boolean allow = true;
for(Pair<Pair<Integer, Integer>, Boolean> rule: rules){
if(ip >= rule.first.first && ip <= (rule.first.first|rule.first.second))
{
allow = rule.second;
break;
}
}
System.out.println(allow?"GRANTED":"DENIED");
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
new Main().run();
}
}