The creators of Medicare, Social Security, and other long-term spending programs had a handle on demographics - or at least on the political realities of the time - so they structured those programs to initially take in more money than they needed in order to build up "trust funds" to cover the eventual retirement of the massive Baby Boomer generation.
The alternative to trust-fund entitlements is "pay as you go," which makes such programs nice and cheap in the beginning and vastly more expensive later on. This prospect used to be considered political poison, so