Example 5 The union of sets $A$ and $B$, $A \cup B=\left\{a_{1}, a_{2}, a_{3}\right\}$, when $A \neq B$, $(A$, $B)$ and $(B, A)$ are considered different pairs. How many such $(A, B)$ pairs are there?
Solution: If $A=\left\{a_{1}, a_{2}, a_{3}\right\}$, then there are $2^{3}$ sets $B$ that satisfy the condition, i.e., any subset of $\left\{a_{1}, a_{2}, a_{3}\right\}$ can be $B$, so the number of pairings in this case is 8.

If $A=\left\{a_{1}, a_{2}\right\}$, then $B$ must contain $a_{3}$. Essentially, $B$ can be considered as a subset of $\left\{a_{1}, a_{2}\right\}$ plus the element $a_{3}$. The number of pairings in this case is $2^{2}=4$; similarly, if $A=\left\{a_{2}, a_{3}\right\}$ or $\left\{a_{1}, a_{3}\right\}$, the number of pairings is also 4 each. Therefore, when $A$ has 2 elements, the number of pairings is $3 \times 4=12$.

If $A$ has one element, such as $A=\left\{a_{1}\right\}$, then $a_{2}, a_{3} \in B$. Thus, $B$ has only 2 possibilities (either $a_{1} \in B$ or $a_{1} \notin B$), so the number of pairings is $3 \times 2=6$.
If $A=\varnothing$, then $B=\left\{a_{1}, a_{2}, a_{3}\right\}$.
In summary, the total number of pairings is $8+12+6+1=27$.