On-Demand Sampling: Learning Optimally from Multiple Distributions