Next number sequence is overwriting old ID after conversion

Can someone tell me where the “next numbers” for prescriptions and encounters are stored?
After converting from V4.2 to V5.0.1 we are loosing data because of the “replace into” statement in prescriptions.
See also attached document with screenshots.
ID issue after conversion OpenEMR.docx (175.0 KB)

Thanks in advance for the help.

Sorry for this post, I already found the table “sequences”