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

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

h = A("h")
a = B("a")
k = A("k")
b = B("b")
f = A("f")
n = A("n")
j = B("j")
e = A("e")

h.r = a
h.q = k
a.p = j
k.r = a
k.q = h
b.p = a
f.r = j
f.q = h
n.r = b
n.q = k
j.p = a
e.r = b
e.q = f
assert a.p.p.p.txt == "
class A:
    def __init__(self, txt: str):
        self.r: B = None
        self.q: A = None
        self.txt = txt

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

h = A("h")
a = B("a")
k = A("k")
b = B("b")
f = A("f")
n = A("n")
j = B("j")
e = A("e")

h.r = a
h.q = k
a.p = j
k.r = a
k.q = h
b.p = a
f.r = j
f.q = h
n.r = b
n.q = k
j.p = a
e.r = b
e.q = f
assert a.p.p.p.txt == "j"