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

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

b = A("b")
g = B("g")
h = B("h")
n = B("n")
d = A("d")
f = B("f")
l = A("l")
k = A("k")

b.y = h
b.r = h
g.q = h
g.z = n
h.q = n
h.z = n
n.q = f
n.z = g
d.y = h
d.r = h
f.q = n
f.z = h
l.y = h
l.r = g
k.y = h
k.r = n
assert g.z.q.q.z.z.z.txt == "
class A:
    def __init__(self, txt: str):
        self.y: B = None
        self.r: B = None
        self.txt = txt

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

b = A("b")
g = B("g")
h = B("h")
n = B("n")
d = A("d")
f = B("f")
l = A("l")
k = A("k")

b.y = h
b.r = h
g.q = h
g.z = n
h.q = n
h.z = n
n.q = f
n.z = g
d.y = h
d.r = h
f.q = n
f.z = h
l.y = h
l.r = g
k.y = h
k.r = n
assert g.z.q.q.z.z.z.txt == "g"