Optimization of a mercury jet target for a neutrino factory or a muon collider