class Splice

  class List
    attr_accessor :data, :nnext

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

  def create(i)
    i.times do |i|
      eval("@start_node_#{i} = List.new(\"START\")")
      eval("@last_node_#{i} = @start_node_#{i}")
    end
  end

  def list_(num)
    eval("@list_#{num}")
  end

  def list_crt_node(i)
    eval("@list_#{i}_crt_node")
  end

  def insert(t, x)
    current_node = eval("@last_node_#{t}")
    new_node = List.new(x)
    current_node::nnext = new_node
    eval("@last_node_#{t} = new_node")
  end

  def dump(t)
    current_node = eval("@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 = eval("@last_node_#{x}")
    join_node::nnext = eval("@start_node_#{s}::nnext")
    eval("@last_node_#{x} = @last_node_#{s}")
    eval("@start_node_#{s}::nnext = nil")
    eval("@last_node_#{s} = @start_node_#{s}")
  end

end

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

splice = Splice.new
splice.create(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
