The U.S. trade deficit unexpectedly narrowed in October amid declines in imports of pharmaceuticals and gold.