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

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

b = A("b")
f = B("f")
k = A("k")

b.q = k
b.z = k
f.r = k
k.q = b
k.z = b
assert k.q.q.z.txt == "
class A:
    def __init__(self, txt: str):
        self.q: A = None
        self.z: A = None
        self.txt = txt

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

b = A("b")
f = B("f")
k = A("k")

b.q = k
b.z = k
f.r = k
k.q = b
k.z = b
assert k.q.q.z.txt == "b"