Sure.

Pardon the long explanation.

Let's say this is our n relations (here 7) relations

```
+-----------------------------------------------------------------------------------------------+
| m.tagId | n.tagId | rel |
+-----------------------------------------------------------------------------------------------+
| m.tagId | n.tagId | rel |
+-----------------------------------------------------------------------------------------------+
| 2253 | 2266 | [:CONTACTED {StartTime: 1605284000, Duration: 1500, EndTime: 1605285000}] |
| 2253 | 2266 | [:CONTACTED {StartTime: 1605284100, Duration: 1500, EndTime: 1605284500}] |
| 2253 | 2266 | [:CONTACTED {StartTime: 1605285000, Duration: 1500, EndTime: 1605285100}] |
| 2253 | 2266 | [:CONTACTED {StartTime: 1605286000, Duration: 1500, EndTime: 1605287000}] |
| 2253 | 2266 | [:CONTACTED {StartTime: 1605285800, Duration: 1500, EndTime: 1605287200}] |
| 2253 | 2266 | [:CONTACTED {StartTime: 1605287500, Duration: 1500, EndTime: 1605288000}] |
| 2253 | 2266 | [:CONTACTED {StartTime: 1605287800, Duration: 1500, EndTime: 1605288200}] |
+-----------------------------------------------------------------------------------------------+
```

For the first 3 relations the code will be this

```
MATCH
(m:Tag)-[rel:CONTACTED]->(n:Tag)
WHERE
((m.tagId=2266 AND n.tagId=2253) OR (m.tagId=2253 AND n.tagId=2266))
WITH
rel
ORDER BY
rel.StartTime
WITH
COLLECT(rel) AS relations
WITH
apoc.coll.pairsMin(relations) AS r
MATCH
(m:Tag)-[rel:CONTACTED]->(n:Tag)
WHERE
((m.tagId=2266 AND n.tagId=2253) OR (m.tagId=2253 AND n.tagId=2266) AND (r[0][0].StartTime<=r[0][1].EndTime AND r[0][1].StartTime<=r[0][0].EndTime))
WITH
[r[0][0].StartTime,r[0][1].StartTime] AS startarray, [r[0][0].EndTime,r[0][1].EndTime] AS endarray,r[0][0] AS rel1,r[0][1] AS rel2
UNWIND
startarray AS minstart
UNWIND
endarray AS maxend
WITH
rel1 AS r1, rel2 AS r2,MIN(minstart) AS minn, MAX(maxend) AS maxx
SET
r1.StartTime=minn,r1.EndTime=maxx,r1.Duration=ABS(minn-maxx) DELETE r2;
```

For the next two relations code will look like this

```
MATCH
(m:Tag)-[rel:CONTACTED]->(n:Tag)
WHERE
((m.tagId=2266 AND n.tagId=2253) OR (m.tagId=2253 AND n.tagId=2266))
WITH
rel
ORDER BY
rel.StartTime
WITH
COLLECT(rel) AS relations
WITH
apoc.coll.pairsMin(relations) AS r
MATCH
(m:Tag)-[rel:CONTACTED]->(n:Tag)
WHERE
((m.tagId=2266 AND n.tagId=2253) OR (m.tagId=2253 AND n.tagId=2266) AND (r[1][0].StartTime<=r[1][1].EndTime AND r[1][1].StartTime<=r[1][0].EndTime))
WITH
[r[1][0].StartTime,r[1][1].StartTime] AS startarray, [r[1][0].EndTime,r[1][1].EndTime] AS endarray,r[1][0] AS rel1,r[1][1] AS rel2
UNWIND
startarray AS minstart
UNWIND
endarray AS maxend
WITH
rel1 as r1, rel2 as r2,MIN(minstart) AS minn, MAX(maxend) AS maxx
SET
r1.StartTime=minn,r1.EndTime=maxx,r1.Duration=ABS(minn-maxx) DELETE r2;
```

And for the last two relations the code will look like this

```
MATCH
(m:Tag)-[rel:CONTACTED]->(n:Tag)
WHERE
((m.tagId=2266 AND n.tagId=2253) OR (m.tagId=2253 AND n.tagId=2266))
WITH
rel
ORDER BY
rel.StartTime
WITH
COLLECT(rel) AS relations
WITH
apoc.coll.pairsMin(relations) AS r
MATCH
(m:Tag)-[rel:CONTACTED]->(n:Tag)
WHERE
((m.tagId=2266 AND n.tagId=2253) OR (m.tagId=2253 AND n.tagId=2266) AND (r[2][0].StartTime<=r[2][1].EndTime AND r[2][1].StartTime<=r[2][0].EndTime))
WITH
[r[2][0].StartTime,r[2][1].StartTime] AS startarray, [r[2][0].EndTime,r[2][1].EndTime] AS endarray,r[2][0] AS rel1,r[2][1] AS rel2
UNWIND
startarray AS minstart
UNWIND
endarray AS maxend
WITH
rel1 as r1, rel2 as r2,MIN(minstart) AS minn, MAX(maxend) AS maxx
SET
r1.StartTime=minn,r1.EndTime=maxx,r1.Duration=ABS(minn-maxx) DELETE r2;
```

And above three codes should be iterated multiple times to get our final db(which is the below one)

```
+-----------------------------------------------------------------------------------------------+
| m.tagId | n.tagId | rel |
+-----------------------------------------------------------------------------------------------+
| 2253 | 2266 | [:CONTACTED {StartTime: 1605284000, Duration: 1100, EndTime: 1605285100}] |
| 2253 | 2266 | [:CONTACTED {StartTime: 1605285800, Duration: 1400, EndTime: 1605287200}] |
| 2253 | 2266 | [:CONTACTED {StartTime: 1605287500, Duration: 700, EndTime: 1605288200}] |
+-----------------------------------------------------------------------------------------------+
```

What I am looking for is a general way to do the above queries( like iterating n times )through these relations.