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

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

g = A("g")
i = B("i")
n = B("n")
c = A("c")
h = A("h")
e = B("e")

g.w = c
g.r = h
i.p = h
n.p = c
c.w = h
c.r = g
h.w = c
h.r = c
e.p = c
assert e.p.w.w.w.r.txt == "
class A:
    def __init__(self, txt: str):
        self.w: A = None
        self.r: A = None
        self.txt = txt

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

g = A("g")
i = B("i")
n = B("n")
c = A("c")
h = A("h")
e = B("e")

g.w = c
g.r = h
i.p = h
n.p = c
c.w = h
c.r = g
h.w = c
h.r = c
e.p = c
assert e.p.w.w.w.r.txt == "c"