gets li = gets.chomp abort unless li=~/^[A-Za-z]+$/ abort if li.chars.count('Q') > 1 if li=~/^[^HQ]*Q[^HQ]*$/ puts li else p -1 end