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

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

g = A("g")
m = B("m")
c = B("c")
h = A("h")
b = A("b")
i = B("i")

g.t = b
g.r = i
m.w = c
c.w = m
h.t = b
h.r = m
b.t = h
b.r = i
i.w = m
assert g.r.w.w.w.txt == "
class A:
    def __init__(self, txt: str):
        self.t: A = None
        self.r: B = None
        self.txt = txt

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

g = A("g")
m = B("m")
c = B("c")
h = A("h")
b = A("b")
i = B("i")

g.t = b
g.r = i
m.w = c
c.w = m
h.t = b
h.r = m
b.t = h
b.r = i
i.w = m
assert g.r.w.w.w.txt == "m"