Patients who spend longer in A&E departments waiting for non-immediate care could have a higher risk of dying within the first few weeks of being discharged, research suggests.