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

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

d = A("d")
j = B("j")
e = B("e")
m = A("m")
b = B("b")
f = A("f")
a = A("a")

d.x = m
d.y = f
j.q = m
e.q = a
m.x = d
m.y = d
b.q = a
f.x = a
f.y = d
a.x = d
a.y = m
assert f.y.y.y.y.y.y.txt == "
class A:
    def __init__(self, txt: str):
        self.x: A = None
        self.y: A = None
        self.txt = txt

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

d = A("d")
j = B("j")
e = B("e")
m = A("m")
b = B("b")
f = A("f")
a = A("a")

d.x = m
d.y = f
j.q = m
e.q = a
m.x = d
m.y = d
b.q = a
f.x = a
f.y = d
a.x = d
a.y = m
assert f.y.y.y.y.y.y.txt == "f"