class A:
    def __init__(self, txt: str):
        self.r: B = None
        self.txt = txt

class B:
    def __init__(self, txt: str):
        self.t: B = None
        self.txt = txt

a = A("a")
g = B("g")
d = B("d")
m = A("m")
h = A("h")

a.r = d
g.t = d
d.t = g
m.r = d
h.r = g
assert g.t.t.t.txt == "
class A:
    def __init__(self, txt: str):
        self.r: B = None
        self.txt = txt

class B:
    def __init__(self, txt: str):
        self.t: B = None
        self.txt = txt

a = A("a")
g = B("g")
d = B("d")
m = A("m")
h = A("h")

a.r = d
g.t = d
d.t = g
m.r = d
h.r = g
assert g.t.t.t.txt == "d"