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

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

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

h.q = d
a.w = d
a.s = d
d.q = h
assert a.s.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.w: A = None
        self.s: A = None
        self.txt = txt

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

h.q = d
a.w = d
a.s = d
d.q = h
assert a.s.q.q.txt == "d"