Parent_ID lookup field but more advance

I have 3 bases (let’s call them small, joker, and big)

The entire relationship is built on ParentID and ChildID

  • Small is connected to a joker (small is the child of the joker)

  • Big is connected to joker (big is the parent of the joker)

  • Joker can be connected both to Big and Small, but the tricky part is that it can also be connected to itself (A Joker can have a child that is a Joker and a Parent that is a Joker)

I want to create a lookup in the Small base that shows me the name of the Big, which means that I need a script that looks at the parentID of the Joker and keeps going up the child-parent chain until it gets to the Big_ID.

Can someone help me with this?

Thank you in advance!