Cooling and contamination of mafic and ultramafic magmas during ascent through continental crust