//
// Written by Fyodor Menshikov 23.03.2007
// 12:54-13:19
//

import java.io.*;
import java.util.*;

class TableRecord {

   int net_addr;
   int net_mask;
   String gateway;
   int metric;

   TableRecord(int net_addr, int net_mask, String gateway, int metric) {
      this.net_addr = net_addr;
      this.net_mask = net_mask;
      this.gateway = gateway;
      this.metric = metric;
   }
}

public class Main {

   StreamTokenizer in;
   PrintWriter out;

   Main() {
      try {
         in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
         in.resetSyntax();
         in.wordChars('0', '9');
         in.whitespaceChars(0, 32);
         in.ordinaryChar('.');
         out = new PrintWriter(new OutputStreamWriter(System.out));
      } catch (Exception e) {
         throw new Error();
      }
   }

   void asserT(boolean e) {
      if (!e) {
         throw new Error();
      }
   }

   String nextWord() {
      try {
         in.nextToken();
         asserT(in.ttype == in.TT_WORD);
         return in.sval;
      } catch (IOException e) {
         throw new Error();
      }
   }

   int nextInt() {
      return Integer.parseInt(nextWord());
   }

   void skipDot() {
      try {
         in.nextToken();
         asserT(in.ttype == '.');
      } catch (IOException e) {
         throw new Error();
      }
   }

   int nextIP() {
      int b1 = nextInt();
      asserT(0 <= b1 && b1 <= 255);
      skipDot();
      int b2 = nextInt();
      asserT(0 <= b2 && b2 <= 255);
      skipDot();
      int b3 = nextInt();
      asserT(0 <= b3 && b3 <= 255);
      skipDot();
      int b4 = nextInt();
      asserT(0 <= b4 && b4 <= 255);
      return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
   }

   String nextIPstr() {
      int b1 = nextInt();
      asserT(0 <= b1 && b1 <= 255);
      skipDot();
      int b2 = nextInt();
      asserT(0 <= b2 && b2 <= 255);
      skipDot();
      int b3 = nextInt();
      asserT(0 <= b3 && b3 <= 255);
      skipDot();
      int b4 = nextInt();
      asserT(0 <= b4 && b4 <= 255);
      return "" + b1 + "." + b2 + "." + b3 + "." + b4;
   }

   void run() {
      int table_len = nextInt();
      TableRecord table[] = new TableRecord[table_len];
      for (int i = 0; i < table_len; i++) {
         table[i] = new TableRecord(nextIP(), nextIP(), nextIPstr(), nextInt());
      }
      int n_queries = nextInt();
      for (int i = 0; i < n_queries; i++) {
         int ip = nextIP();
         int best_metric = 10000;
         int best_mask = 0;
         String ans = null;
         for (int j = 0; j < table_len; j++) {
            if ((ip & table[j].net_mask) == (table[j].net_addr & table[j].net_mask)) {
               if (table[j].net_mask == best_mask) {
                  if (table[j].metric < best_metric) {
                     best_metric = table[j].metric;
                     best_mask = table[j].net_mask;
                     ans = table[j].gateway;
                  }
               } else {
                  // table[j].net_mask != best_mask
                  if ((table[j].net_mask & best_mask) == best_mask) {
                     best_metric = table[j].metric;
                     best_mask = table[j].net_mask;
                     ans = table[j].gateway;
                  }
               }
            }
         }
         if (ans == null) {
            out.println("NO ROUTE");
         } else {
            out.println(ans);
         }
      }
      out.close();
   }

   public static void main(String args[]) {
      new Main().run();
   }
}
