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

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

b = A("b")
h = B("h")
f = B("f")
g = A("g")

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

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

b = A("b")
h = B("h")
f = B("f")
g = A("g")

b.s = f
h.q = f
f.q = h
g.s = h
assert b.s.q.q.txt == "f"