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

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

b = A("b")
m = B("m")
d = B("d")
k = A("k")
f = B("f")
j = B("j")
g = B("g")
a = B("a")
c = A("c")

b.r = f
m.r = k
m.q = b
d.r = c
d.q = b
k.r = f
f.r = k
f.q = b
j.r = b
j.q = c
g.r = b
g.q = k
a.r = b
a.q = c
c.r = m
assert f.r.r.q.r.q.txt == "
class A:
    def __init__(self, txt: str):
        self.r: B = None
        self.txt = txt

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

b = A("b")
m = B("m")
d = B("d")
k = A("k")
f = B("f")
j = B("j")
g = B("g")
a = B("a")
c = A("c")

b.r = f
m.r = k
m.q = b
d.r = c
d.q = b
k.r = f
f.r = k
f.q = b
j.r = b
j.q = c
g.r = b
g.q = k
a.r = b
a.q = c
c.r = m
assert f.r.r.q.r.q.txt == "b"