If g1 or g2 are not trivial (1 or N), they should be either `p` or `q`. Try different values of `a` until you can find one, the likelihood is pretty high in practice.