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

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

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

f = A("f")
d = B("d")
g = C("g")
h = A("h")

f.q = d
d.x = f
d.z = h
g.z = f
h.q = d
assert d.x.q.x.txt == "
class A:
    def __init__(self, txt: str):
        self.q: B = None
        self.txt = txt

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

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

f = A("f")
d = B("d")
g = C("g")
h = A("h")

f.q = d
d.x = f
d.z = h
g.z = f
h.q = d
assert d.x.q.x.txt == "f"