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

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

g = A("g")
m = B("m")
j = A("j")

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

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

g = A("g")
m = B("m")
j = A("j")

g.p = m
m.q = j
j.p = m
assert j.p.q.p.txt == "m"