rule = {"A" => ["B", "D"], "B" => ["A", "C", "E"], "C" => ["B", "F"], "D" => ["A", "E", "G"], "E" => ["B", "D", "F", "H"], "F" => ["C", "E", "I"], "G" => ["D", "H"], "H" => ["E", "G", "I"], "I" => ["F", "H"]}
1000.times{
    line = gets.chomp
    candidate = true
    c = line[0]
    1.upto(line.length - 1){|i|
        unless rule[c].include?(line[i])
            candidate = false
            break
        end
        c = line[i]
    }
    puts line if candidate
}
