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

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

l = A("l")
d = B("d")
b = B("b")
g = B("g")
a = A("a")
f = B("f")

l.p = f
l.z = a
d.q = a
d.r = g
b.q = l
b.r = d
g.q = l
g.r = f
a.p = b
a.z = l
f.q = l
f.r = d
assert b.r.r.r.txt == "
class A:
    def __init__(self, txt: str):
        self.p: B = None
        self.z: A = None
        self.txt = txt

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

l = A("l")
d = B("d")
b = B("b")
g = B("g")
a = A("a")
f = B("f")

l.p = f
l.z = a
d.q = a
d.r = g
b.q = l
b.r = d
g.q = l
g.r = f
a.p = b
a.z = l
f.q = l
f.r = d
assert b.r.r.r.txt == "f"