# frozen_string_literal: true def solve(t_and_s) l = [] r = [] t_and_s.each do |t, s| case t when '0' r << s when '1' l << s end end (l.reverse + r).join end N = gets.to_i T = N.times.map { gets.chomp.split } RESULT = solve(T) puts RESULT