
class Node
    attr_accessor :value
    attr_accessor :prev
    attr_accessor :next
    def initialize(value="END", prev_node=nil, next_node=nil)
        @value = value
        @prev = prev_node
        @next = next_node
    end
end


class MyList
    attr_accessor :head
    attr_accessor :end_node
    def initialize(head = Node.new("END") , end_node = nil)
        @head = head
        @end_node = @head
    end

    def insert(x)
        new_node = Node.new(x)
        unless @end_node.prev.nil? 
            @end_node.prev.next = new_node
            new_node.prev = @end_node.prev
            @end_node.prev = new_node
            new_node.next = @end_node
        else
            @end_node.prev = new_node
            new_node.next = @end_node
            @head = new_node
        end
    end

    def splice(s)
        if @end_node.prev.nil?
            @head = s.head
            @end_node = s.end_node
        else
            unless s.end_node.prev.nil?
                @end_node.prev.next = s.head
                s.head.prev = @end_node.prev
                @end_node = s.end_node
            end
        end
        s.head = Node.new("END")
        s.end_node = s.head
    end

    def dump
        node = @head
        unless node.next.nil?
            while(node.next.value != "END")
                print "#{node.value} "
                node = node.next
            end
            print node.value
        end
        puts
    end

end

# def test_print(n,b)
#     puts "*******"
#     (0...n).each { |x|
#         print "List #{x}: "
#         b[x].dump
#     }
#     puts "*******"
#     puts "\n"
# end


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

A=[]
n.times{
    A.push(MyList.new())
}

q.times{
    choice, t, x = gets.chomp.split(" ").map &:to_i
    case choice
    when 0
        A[t].insert(x)
    when 1
        A[t].dump
    when 2 
        A[x].splice(A[t])
    end
    #test_print(n,A)
}
