m[x_,n_]:=Module[{u}, ReplaceRepeated[u, u->u**x, MaxIterations->n-1]/. u->x]; gprel = {m[a,5]->1, m[w,3]->1, m[c,31]->1, w**a-> a**w**c, c**a->a**m[c,2], c**w->w**m[c,25], 1**a->a, 1**w->w, 1**c->c}; s[x_]:=x //. gprel;