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

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

k = A("k")
b = B("b")
d = B("d")
h = B("h")
i = A("i")
j = A("j")
c = A("c")
n = A("n")
f = B("f")

k.q = h
b.w = h
d.w = h
h.w = f
i.q = h
j.q = d
c.q = b
n.q = f
f.w = d
assert k.q.w.w.txt == "
class A:
    def __init__(self, txt: str):
        self.q: B = None
        self.txt = txt

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

k = A("k")
b = B("b")
d = B("d")
h = B("h")
i = A("i")
j = A("j")
c = A("c")
n = A("n")
f = B("f")

k.q = h
b.w = h
d.w = h
h.w = f
i.q = h
j.q = d
c.q = b
n.q = f
f.w = d
assert k.q.w.w.txt == "d"