Get Leaf Nodes for all Nodes in directed graph

Hi Team,

I have a simple graph that has Nodes which represent duplicate record id in the below form
Duplicate Id, Original Id
A,B
B,C
C,D
X,Y
Y,Z

The directed graph looks like A -> B ->C ->D and I want CSV result that looks like below that will represent the ultimate Original Id
A,D
B,D
C,D
X,Z
Y,Z

Have been struggling for more than a day being newbie to graph databases, any help will be highly appreciated.

Thanks

Here ya go:

image

Cypher:

MATCH p=(a:Ident)-[:ORIGID*]->(b:Ident)
WHERE NOT (b)-[:ORIGID]->(:Ident)
WITH a, LAST(nodes(p)) as b
RETURN a.name, b.name
ORDER BY a.name

image