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

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

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

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

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

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

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