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

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

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

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

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

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

b.p = f
b.q = l
l.p = b
d.p = g
d.q = j
m.p = d
a.p = d
a.q = j
f.p = a
f.q = m
j.p = b
g.p = f
g.q = m
assert d.p.q.p.p.p.p.txt == "a"