Observe that the fibres $X_s$ of $f$ are connected by our assumption that $H^0(X_s, \mathcal{O}_{X_s}) = \kappa(s)$. Thus the rank of $\mathcal{E}$ is constant on the fibres. Since $f$ is open (Morphisms, Lemma \ref{morphisms-lemma-fppf-open}) and closed we conclude that there is a decomposition $S = \coprod S_r$ of $S$ into open and closed subschemes such that $\mathcal{E}$ has constant rank $r$ on the inverse image of $S_r$. Thus we may assume $\mathcal{E}$ has constant rank $r$. We will denote $\mathcal{E}^\vee = \SheafHom(\mathcal{E}, \mathcal{O}_X)$ the dual rank $r$ module. \medskip\noindent By cohomology and base change (more precisely by Derived Categories of Schemes, Lemma \ref{perfect-lemma-flat-proper-perfect-direct-image-general}) we see that $E = Rf_*\mathcal{E}$ is a perfect object of the derived category of $S$ and that its formation commutes with arbitrary change of base. Similarly for $E' = Rf_*\mathcal{E}^\vee$. Since there is never any cohomology in degrees $< 0$, we see that $E$ and $E'$ have (locally) tor-amplitude in $[0, b]$ for some $b$. Observe that for any $g : T \to S$ we have $p_*(q^*\mathcal{E}) = H^0(Lg^*E)$ and $p_*(q^*\mathcal{E}^\vee) = H^0(Lg^*E')$. Let $j : Z \to S$ and $j' : Z' \to S$ be immersions of finite presentation constructed in Derived Categories of Schemes, Lemma \ref{perfect-lemma-locally-closed-where-H0-locally-free} for $E$ and $E'$ with $a = 0$ and $r = r$; these are roughly speaking characterized by the property that $H^0(Lj^*E)$ and $H^0((j')^*E')$ are finite locally free modules compatible with pullback. \medskip\noindent Let $g : T \to S$ be a morphism. If there exists an $\mathcal{N}$ as in the lemma, then, using the projection formula Cohomology, Lemma \ref{cohomology-lemma-projection-formula}, we see that the modules $$ p_*(q^*\mathcal{L}) \cong p_*(p^*\mathcal{N}) \cong \mathcal{N} \otimes_{\mathcal{O}_T} p_*\mathcal{O}_{X_T} \cong \mathcal{N}\quad\text{and similarly }\quad p_*(q^*\mathcal{E}^\vee) \cong \mathcal{N}^\vee $$ are finite locally free modules of rank $r$ and remain so after any further base change $T' \to T$. Hence in this case $T \to S$ factors through $j$ and through $j'$. Thus we may replace $S$ by $Z \times_S Z'$ and assume that $f_*\mathcal{E}$ and $f_*\mathcal{E}^\vee$ are finite locally free $\mathcal{O}_S$-modules of rank $r$ whose formation commutes with arbitrary change of base (small detail omitted). \medskip\noindent In this sitation if $g : T \to S$ be a morphism and there exists an $\mathcal{N}$ as in the lemma, then the map (cup product in degree $0$) $$ p_*(q^*\mathcal{E}) \otimes_{\mathcal{O}_T} p_*(q^*\mathcal{E}^\vee) \longrightarrow \mathcal{O}_T $$ is a perfect pairing. Conversely, if this cup product map is a perfect pairing, then we see that locally on $T$ we may choose a basis of sections $\sigma_1, \ldots, \sigma_r$ in $p_*(q^*\mathcal{E})$ and $\tau_1, \ldots, \tau_r$ in $p_*(q^*\mathcal{E}^\vee)$ whose products satisfy $\sigma_i \tau_j = \delta_{ij}$. Thinking of $\sigma_i$ as a section of $q^*\mathcal{E}$ on $X_T$ and $\tau_j$ as a section of $q^*\mathcal{E}^\vee$ on $X_T$, we conclude that $$ \sigma_1, \ldots, \sigma_r : \mathcal{O}_{X_T}^{\oplus r} \longrightarrow q^*\mathcal{E} $$ is an isomorphism with inverse given by $$ \tau_1, \ldots, \tau_r : q^*\mathcal{E} \longrightarrow \mathcal{O}_{X_T}^{\oplus r} $$ In other words, we see that $p^*p_*q^*\mathcal{E} \cong q^*\mathcal{E}$. But the condition that the cupproduct is nondegenerate picks out a retrocompact open subscheme (namely, the locus where a suitable determinant is nonzero) and the proof is complete.