The problem of searching for data through the pipeline

Colleagues, good afternoon! I am writing a web application for a small university using the Neo4j database. I faced a problem when it is necessary to create a discipline with reference to different educational programs, semesters and teachers. The request looks something like this:

MATCH (d:Department {shortName: "DepShortName"})
MERGE (s:Subject {idSubject: randomUuid()})-[:ASSIGNED_TO]->(d)
SET s.code = "B1.О.1.1.", s.nameSubject = "NameOfSubject", s.commonZET = 4, s.type = "discipline"
MATCH (t0:Teacher {idUser: "b1021cd5-..."})
MERGE (t0)-[:TEACHES]->(sem0:Semester {idSubjectSemester: randomUuid()})-[:SEMESTER_IN]->(s)
SET sem0.semester = "1", sem0.zet = toInteger(2), sem0.lecturesHours = toInteger(10), sem0.semenarsHours = toInteger(22), sem0.independentHours = toInteger(32), sem0.controlHours = toInteger(0), sem0.control = "credit with an assessment"
MATCH (t1:Teacher {idUser: "b1021cd5-..."})
MERGE (t1)-[:TEACHES]->(sem1:Semester {idSubjectSemester: randomUuid()})-[:SEMESTER_IN]->(s)
SET sem1.semester = "2", sem1.zet = toInteger(2), sem1.lecturesHours = toInteger(10), sem1.semenarsHours = toInteger(22), sem1.independentHours = toInteger(32), sem1.controlHours = toInteger(0), sem1.control = "credit with an assessment"
MATCH (b0:Bep {idBep: "c025232a-..."})
MERGE (s)-[:IN_BEP]-(b0)
MATCH (b1:Bep {idBep: "514e6eb6-...."})
MERGE (s)-[:IN_BEP]-(b1)

Such code refuses to work without WITH. If you put them down, then there will be no execution. If I figured it out correctly, it's because the system works like a pipeline, and therefore the next query works with the results of the previous one. At the same time, I need to find quite a few nodes before I start adding relationships between us. EXPLAIN showed that each MATCH finds the necessary data, but then they are combined, which leads to 0 results.
How can this situation be avoided? Convulsively, both at night and in the morning, I search through the documentation for at least something on this subject, but I do not find it in any way... Thank you in advance for the answer and a kick in the right direction.

I solved the problem, thank you all!