class Splice
  attr_accessor :start_node, :last_node

  def initialize(n)
    @start_node = Array.new(n)
    @last_node = Array.new(n)

    n.times do |i|
      @start_node[i] = List.new("START")
      @last_node[i] = @start_node[i]
    end
  end

  class List
    attr_accessor :data, :nnext

    def initialize(data=nil, nnext=nil)
      @data = data
      @nnext = nnext
    end
  end

  def insert(t, x)
    current_node = @last_node[t]
    new_node = List.new(x)
    current_node::nnext = new_node
    @last_node[t] = new_node
  end

  def dump(t)
    current_node = @start_node[t]

    if current_node::nnext.nil?
      puts ""
    else
      current_node = current_node::nnext
      while true
        print current_node::data
          if current_node::nnext.nil?
            print "\n"
            break
          end
        print " "
        current_node = current_node::nnext
      end
    end
  end

  def splice(s, x)
    join_node = @last_node[x]
    push_node = @start_node[s]
    join_node::nnext = push_node::nnext
    @last_node[x] = @last_node[s]
    push_node::nnext = nil
    @last_node[s] = @start_node[s]
  end
end

n, q = gets(chomp:true).split.map(&:to_i)
splice = Splice.new(n)

q.times do
  o, t, x = gets(chomp:true).split.map(&:to_i)
  case o
  when 0
    splice.insert(t, x)
  when 1
    splice.dump(t)
  when 2
    splice.splice(t, x)
  end
end
