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

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

a = A("a")
d = B("d")
k = A("k")
b = B("b")
g = B("g")
c = A("c")
j = A("j")

a.p = c
d.z = g
k.p = j
b.z = g
g.z = d
c.p = j
j.p = c
assert g.z.z.z.txt == "
class A:
    def __init__(self, txt: str):
        self.p: A = None
        self.txt = txt

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

a = A("a")
d = B("d")
k = A("k")
b = B("b")
g = B("g")
c = A("c")
j = A("j")

a.p = c
d.z = g
k.p = j
b.z = g
g.z = d
c.p = j
j.p = c
assert g.z.z.z.txt == "d"