public static void main(String[] args) throws Throwable{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		TreeMap<String, TreeMap<Character, Integer>> palabras = new TreeMap<String, TreeMap<Character,Integer>>();
		for(String ln; !(ln = in.readLine().trim()).equals("XXXXXX");)
			palabras.put(ln, getMap(ln.toCharArray()));
		for(String ln; !(ln = in.readLine().trim()).equals("XXXXXX");){
			TreeMap<Character, Integer> mapa = getMap(ln.toCharArray());
			TreeSet<String> soluciones = new TreeSet<String>();
			for(Entry<String, TreeMap<Character, Integer>> palabra: palabras.entrySet()){
				boolean ws = palabra.getValue().size() == mapa.size();
				for(Entry<Character, Integer> entry: palabra.getValue().entrySet()){
					if(!ws)break;
					ws = mapa.containsKey(entry.getKey()) && entry.getValue() == mapa.get(entry.getKey());
				}
				if(ws)soluciones.add(palabra.getKey());
			}
			if(soluciones.isEmpty())sb.append("NOT A VALID WORD\n");
			else for(String str: soluciones)sb.append(str + "\n");
			sb.append("******\n");
		}
		System.out.print(new String(sb));
	}