I wrote a relatively simple implementation of the Seifert van Kampen theorem, and I have a commutative 'cube', so to speak, meaning I want to apply it twice successively. The implementation is

```
# This function computes the fundamental group of a topological space X using
# the Seifert-van Kampen theorem. We decompose X into the union of two open
# connected subsets, U and V, whose fundamental groups are the free group on
# some generators along with relations alpha and beta, respectively.
# Additionally, we have the fundamental group of U \cap V, which is the free
# group on the generators gamma_i. We then have the inclusion maps
# I: \pi(U\cap V) -> \pi(U) and J: \pi(U\cap V) -> \pi(V) which gives
# us the categorical coproduct of \pi(U) * \pi(V).
def seifert_van_kampen(U, alpha, V, beta, I, J, intersection):
X = FreeGroup(U.gens() + V.gens())
relations = []
for generator in intersection.gens():
relations.append(X(I(generator))*X(J(generator))^-1)
relations = relations + alpha + beta
return X / relations
```

And it seems to work properly in simple cases. My problem arises when trying to apply it twice successively. My diagram looks like this:

```
A --- AB
/ X \
S -- B AG -- X
\ X /
G --- BG
```

Where S is the pairwise intersection between any of A, B, and G, and the X in the middle of the diagram represents crossing arrows. Through one application of SVK, I can get the fundamental group for AB, AG, or BG, and the problem occurs with the second step to compute the fundamental group for X.

If I have the fundamental groups for A, B, and G generated by alpha_i, beta_i, and gamma_i respectively, then the fundamental group for AB is generated by the alpha_i and beta_i, and similar for AG and BG. But if I try to use the same logic, say using AB and BG to get X, the generators would be alpha_i, beta_i from AB, and beta_i and gamma_i from BG. But my understanding is that those generators should be considered distinct, and I don't quite see how to make that happen in Sage.

In particular, the additional relations when we compute the coproduct (in the AB/BG case) come from the inclusion map of the generators for B into each of AB and BG respectively, which is exactly those duplicate generators. That part is fine, but if I want to consider those words as elements of the free group on (alpha_i, beta_i, beta_i, gamma_i), I don't know how to make those words use the 'correct' generators.

Would appreciate any help on possibly remapping the generators, or any other solutions!