# How is the dimensionality of the volume in ConvNets determined for a general case?

In ConvNets, I understand how the dimensionality of a flat image changes after convolving it with a single filter. For example, if you convolve a P x P x 1 image (assume no padding) with a filter with dimensionality FxF, with stride S, the output dimensionality D can be computed from a simple formula $$\frac{P - F}{S} + 1$$. For example, for a 28 x 28 x 1 image convolved with a 4x4 filter with stride 1, the output we get has dimensionality 12 x 12 because:

$$D = \frac{P - F}{S} + 1 = \frac{28-4}{2} + 1 = 12$$

Typically, however, input images have dimensionality P x P x 3 (3 because of the three RGB color channels). More generally, within the ConvNet, we are taking a convolutional layer with dimensionality P x P x Q, where Q is arbitrary, and convolving it with a filter F x F x G and stride S. I understand how the P, F and S can be used to compute the output dimensionality D (by the formula above); but I don't understand how the Q and G interact to produce an output depth. In particular, can Q and G be entirely arbitrary, or does one have to be a multiple of the other?