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

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

n = A("n")
d = B("d")
h = A("h")
e = B("e")
a = A("a")
f = A("f")
g = B("g")
j = A("j")

n.y = j
d.q = j
h.y = a
e.q = j
a.y = h
f.y = n
g.q = a
j.y = n
assert d.q.y.y.txt == "
class A:
    def __init__(self, txt: str):
        self.y: A = None
        self.txt = txt

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

n = A("n")
d = B("d")
h = A("h")
e = B("e")
a = A("a")
f = A("f")
g = B("g")
j = A("j")

n.y = j
d.q = j
h.y = a
e.q = j
a.y = h
f.y = n
g.q = a
j.y = n
assert d.q.y.y.txt == "j"