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

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

j = A("j")
c = B("c")
e = B("e")
d = A("d")

j.p = c
j.q = d
c.z = j
c.p = j
e.z = j
e.p = j
d.p = e
d.q = j
assert j.q.p.p.p.txt == "
class A:
    def __init__(self, txt: str):
        self.p: B = None
        self.q: A = None
        self.txt = txt

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

j = A("j")
c = B("c")
e = B("e")
d = A("d")

j.p = c
j.q = d
c.z = j
c.p = j
e.z = j
e.p = j
d.p = e
d.q = j
assert j.q.p.p.p.txt == "c"