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

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

a = A("a")
m = B("m")
h = A("h")
k = A("k")
d = B("d")

a.t = h
m.x = k
m.w = a
h.t = a
k.t = a
d.x = a
d.w = a
assert d.w.t.t.t.txt == "
class A:
    def __init__(self, txt: str):
        self.t: A = None
        self.txt = txt

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

a = A("a")
m = B("m")
h = A("h")
k = A("k")
d = B("d")

a.t = h
m.x = k
m.w = a
h.t = a
k.t = a
d.x = a
d.w = a
assert d.w.t.t.t.txt == "h"