# Pairwise chi-squared tests with Bonferroni correction

I have two categorical datasets, say, $A$ and $B$, which are sparse. I would like to apply pairwise $\chi^2$ tests to a certain categories, which are sufficiently populated (say, have expected values $>5$) in the following way. Let $k_{A}$ be number of elements of $A$ which are $k$, and $\neg k_{A}$ be number of elements which are not $k$. So, I have a contingency table of the form:

$$\begin{array}{|r|r|} \hline k_{A}&k_{B}\\ \hline \neg k_{A}&\neg k_{B}\\ \hline \end{array}$$

After applying $\chi^2$ test, I multiply each $p$ by a number of tests I run, and this is a Bonferroni correction. And then I can conclude, that certain categories (having $p<0.05$ after applying correction) are significantly different in datasets $A$ and $B$.

Is this a correct? If, for example, I would simply collapse categories with low expected values into a catch-all category 'others', and run a single $\chi^2$ test, then I don't need to apply any corrections?