N = gets.to_i LIST = Array.new(21) { {} } ans = [] N.times do |i| u = gets.chomp.split case u[0] when '0' _, n, _, *foods = u LIST[n.to_i] = foods.tally when '1' _, b = u ans << -1 1.upto(20) do |id| next if LIST[id][b].nil? ans[-1] = id LIST[id][b] -= 1 if LIST[id][b] == 0 LIST[id].delete(b) end break end when '2' _, n = u LIST[n.to_i] = {} end end puts ans