Solution (source code)

= Solution

For a worldline winding once around a noncontractible torus cycle, a large gauge transformation can satisfy
$$
\Delta\Lambda^I=2\pi\hbar n^I,\qquad n^I\in\mathbb Z.
$$
The source action changes by
$$
\Delta S_c=-q_I\oint d\Lambda^I
=-2\pi\hbar q_In^I.
$$
Gauge invariance of the path-integral phase for every integer vector $n$ requires
$$
\exp\left(\frac{i\Delta S_c}{\hbar}\right)
=e^{-2\pi i q\cdot n}=1.
$$
Therefore
$$
\boxed{q\in\mathbb Z^n}.
$$