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

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

m = A("m")
f = B("f")
k = B("k")
a = A("a")

m.x = k
m.s = f
f.q = k
k.q = f
a.x = f
a.s = k
assert k.q.q.q.txt == "
class A:
    def __init__(self, txt: str):
        self.x: B = None
        self.s: B = None
        self.txt = txt

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

m = A("m")
f = B("f")
k = B("k")
a = A("a")

m.x = k
m.s = f
f.q = k
k.q = f
a.x = f
a.s = k
assert k.q.q.q.txt == "f"