A DPDT relay with an AC coil connected to input A. Connect the common terminals to the output and connect the transfer contacts to the inputs.
Does why really matter?Why would your source have to be switched from A to B? The only way I can figure is if you're running lights with a genny, and when the genny runs out of gas you'd switch to line power, but I can't see why anyone would want to do that.