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

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

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

d.p = g
d.r = c
g.w = j
g.p = c
j.w = g
j.p = c
c.p = g
c.r = d
assert c.p.w.p.txt == "
class A:
    def __init__(self, txt: str):
        self.p: B = None
        self.r: A = None
        self.txt = txt

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

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

d.p = g
d.r = c
g.w = j
g.p = c
j.w = g
j.p = c
c.p = g
c.r = d
assert c.p.w.p.txt == "c"