I have Road Network which was selected based on some criteria. Below is image representing it. Each beginning and end of Road Segment is marked with cross:

[![enter image description here][1]][1]


Selection led to fragmentation of this network. How can I merge (ArcGIS, QGIS, PostGIS) all lines that form consistent continuity, like in image below (colors represent 3 lines which should result after merging). Result of merging – based on below example – should be one layer, with 3 rows, where each row represents grouped lines. Road Network has several attributes, but I am not interested in having them in final, merged output. My goal is to group connected lines:

[![enter image description here][2]][2]


  [1]: https://i.sstatic.net/adhJ1.png
  [2]: https://i.sstatic.net/wJHXx.png

I have tried few approaches e.g.: https://gis.stackexchange.com/questions/311608/using-dissolve-and-multipart-to-singlepart-in-qgis https://gis.stackexchange.com/questions/198317/join-merge-lines-that-touch-and-intersect-in-qgis and none of it worked.