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

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

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

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

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

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

n.q = f
n.r = l
f.x = b
d.x = f
h.x = d
l.q = b
l.r = n
k.x = f
c.q = b
c.r = n
b.x = k
assert c.r.q.x.x.x.x.x.x.txt == "f"