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

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

h = A("h")
b = B("b")
g = A("g")
a = B("a")
c = B("c")
k = B("k")
f = A("f")

h.w = g
h.p = k
b.r = h
g.w = f
g.p = a
a.r = g
c.r = f
k.r = h
f.w = g
f.p = b
assert g.w.p.r.txt == "
class A:
    def __init__(self, txt: str):
        self.w: A = None
        self.p: B = None
        self.txt = txt

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

h = A("h")
b = B("b")
g = A("g")
a = B("a")
c = B("c")
k = B("k")
f = A("f")

h.w = g
h.p = k
b.r = h
g.w = f
g.p = a
a.r = g
c.r = f
k.r = h
f.w = g
f.p = b
assert g.w.p.r.txt == "h"