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

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

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

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

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

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

i.p = f
k.z = i
k.q = f
f.p = i
assert k.q.p.p.txt == "f"