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.txt = txt

h = A("h")
m = B("m")
k = A("k")

h.q = m
m.x = k
k.q = m
assert h.q.x.q.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.txt = txt

h = A("h")
m = B("m")
k = A("k")

h.q = m
m.x = k
k.q = m
assert h.q.x.q.txt == "m"