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

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

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

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

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

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

j.s = d
d.q = j
h.s = d
assert j.s.q.s.txt == "d"