How to do a loop inside a loop?

What if you save each query in a variable ?

<ul>
    <?php $query1 = query_posts( array( 'posts_per_page' => -1, 'post_type' => array('specialties') ));
        if($query1->have_posts()): while($query1->have_posts()): $query1->the_post();
    ?>
    <li>
        <!-- title from post type specialties -->
        <p><?php the_title(); ?></p>
        <ul>
            <?php $query2 = query_posts( array( 'posts_per_page' => -1, 'post_type' => array('team') ));
                if($query2->have_posts()): while($query2->have_posts()): $query2->the_post();
            ?>
            <!-- title from post type team -->
            <li><a href="" class=""><?php the_title() ?></a></li>
            <?php  
                endwhile; endif;
            ?>
        </ul>
    </li>
    <?php  
        endwhile; endif; wp_reset_query();
    ?>
</ul>