lib C fun strtoll(s : UInt8*, p : UInt8**, b : Int32) : Int64 end class String def to_i64 C.strtoll(self, nil, 10) end end n = read_line.to_i a = read_line.split.map(&.to_i.pred) b = read_line.split.map(&.to_i.pred) bi = [-1] * n b.each_with_index { |val, ind| bi[val] = ind } c = a.map { |i| bi[i] } min = n puts a.map { |i| bi[i] }.reverse.select { |i| res = min > i min = {min, i}.min res }.map { |i| b[i] + 1 }.sort.join('\n')