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

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

g = A("g")
j = B("j")
f = B("f")
m = B("m")
h = A("h")
i = B("i")

g.w = f
g.r = h
j.x = m
j.p = i
f.x = j
f.p = m
m.x = f
m.p = j
h.w = m
h.r = g
i.x = f
i.p = j
assert h.w.p.p.x.p.p.txt == "
class A:
    def __init__(self, txt: str):
        self.w: B = None
        self.r: A = None
        self.txt = txt

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

g = A("g")
j = B("j")
f = B("f")
m = B("m")
h = A("h")
i = B("i")

g.w = f
g.r = h
j.x = m
j.p = i
f.x = j
f.p = m
m.x = f
m.p = j
h.w = m
h.r = g
i.x = f
i.p = j
assert h.w.p.p.x.p.p.txt == "j"