def send_query(str) STDOUT.puts(str) STDOUT.flush res = gets.chomp if res == "Merry Christmas!" -1 else res.to_i end end v = gets.to_i v.times do res = send_query("F") exit if res == -1 end send_query("R") step_cnt = 0 loop do res = send_query("L") if res > 0 && step_cnt > 1000 res.times do f = send_query("F") exit if f == -1 end send_query("R") step_cnt = 0 next end if res > 0 f = send_query("F") step_cnt += 1 if f < 0 exit end else f = send_query("R") if f > 0 r = send_query("F") step_cnt += 1 if r < 0 exit end else send_query("R") end end end