An end-to-end simulation of the beam dynamics in the system of extraction, beam transportation and the first section of the accelerator (RFQ) is carried out. The proposed method of modeling the dynamics of ions from the source to the exit from the RFQ made it possible to quickly and efficiently change the parameters of the matching system depending on the final results of the beam passing into the RFQ. A system for matching a beam of negative hydrogen ions has been developed and optimized, which makes it possible to rotate the beam to block the propagation of cesium into the accelerator channel and to match the beam with RFQ. As a result of optimization of the matching channel, it is possible to obtain an emittance increase in RFQ of no more than 25 % for 90 % of the beam fraction, while the capture of beam particles in the acceleration mode is 98 % of the injected beam. The increased drifts in the matching system leave enough space for the placement of diagnostic equipment, a vacuum system and a beam correction system. The dependence of the beam dynamics in the RFQ on the charge distribution over the beam cross section at the output of the matching channel is established.