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

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

d = A("d")
i = B("i")
a = B("a")
f = A("f")
j = A("j")
h = B("h")

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

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

d = A("d")
i = B("i")
a = B("a")
f = A("f")
j = A("j")
h = B("h")

d.p = h
d.q = j
i.p = d
i.r = d
a.p = d
a.r = j
f.p = h
f.q = j
j.p = i
j.q = f
h.p = d
h.r = j
assert h.r.p.r.p.r.q.txt == "f"