
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 = nil, end_node = nil)
        @head = head
        @end_node = end_node
    end

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

    def splice(s)
        @end_node.next = s.head
        s.head.prev = @end_node
        @end_node = s.end_node
        s.head = nil
        s.end_node = nil
    end

    def dump
        node = @head
        while(node != @end_node)
            puts node.value
            node = node.next
        end
    end
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(t)
    end
}
