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

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

class C:
    def __init__(self, txt: str):
        self.s: C = None
        self.p: C = None
        self.txt = txt

e = A("e")
c = B("c")
g = C("g")
b = A("b")
i = B("i")
d = C("d")
h = B("h")

e.q = b
c.r = d
c.x = e
g.s = d
g.p = d
b.q = e
i.r = g
i.x = e
d.s = g
d.p = g
h.r = g
h.x = b
assert e.q.q.q.q.q.txt == "
class A:
    def __init__(self, txt: str):
        self.q: A = None
        self.txt = txt

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

class C:
    def __init__(self, txt: str):
        self.s: C = None
        self.p: C = None
        self.txt = txt

e = A("e")
c = B("c")
g = C("g")
b = A("b")
i = B("i")
d = C("d")
h = B("h")

e.q = b
c.r = d
c.x = e
g.s = d
g.p = d
b.q = e
i.r = g
i.x = e
d.s = g
d.p = g
h.r = g
h.x = b
assert e.q.q.q.q.q.txt == "b"