Ferdinand the Faithful and Ferdinand the Unfaithful
It is Aarne-Thompson type 531. Other tales of this type include The Firebird and Princess Vasilisa, Corvetto, King Fortunatus's Golden Wig, and The Mermaid and the Boy. Another, literary variant is Madame d'Aulnoy's La Belle aux cheveux d'or, or The Story of Pretty Goldilocks.
A couple had no children while they were rich, but when they became poor, they had a son, and the father could find no one for a godfather except a beggar. The beggar named the boy Ferdinand the Faithful, gave him nothing, and took nothing, but he gave the nurse a key and said that when the boy was fourteen, he should go to a castle on the heath and unlock it. Then all it contained would be his.
When the boy was seven, all the other boys boasted of what their godfathers had given them. Ferdinand went to his father for his gift and heard of the key, but there was no castle on the heath. When he was fourteen, he went again, and found a castle. Inside there was nothing but a white horse, but he took the horse home and decided to travel. He saw a pen on the road, passed it, but heard a voice telling him to take it, so he picked it up. Then he rescued a fish from the shore; it gave him a flute to summon him and promised to get for him anything dropped in the water.
Then he met another man, Ferdinand the Unfaithful, who had learned everything about him by wicked magic, and they went on to an inn. A girl there fell in love with Ferdinand the Faithful and told him he should stay and take service with the king; then she got him a place, as an outrider. Ferdinand the Unfaithful also got her to get him a place, because she did not trust him and wanted to keep an eye on him.
The king lamented that he did not have his love. Ferdinand the Unfaithful persuaded the king to send Ferdinand the Faithful for her. Ferdinand the Faithful thought he could not and lamented, but the horse said he needed a ship full of bread and a ship full of meat and to get them from the king. When he had, the horse and Ferdinand the Faithful set out. He appeased birds along the way with the bread and giants with the meat, and with the help of the giants, he carried off the sleeping princess to the king.
The princess declared that she could not live without her magical writings, from the castle, so the king sent Ferdinand the Faithful for them, but with the horse's help, he got them by the same way. On the way back, he dropped his pen into the water. The horse said it could no longer help him. Ferdinand the Faithful played the flute and had the fish bring back the pen.
The princess married the king and became queen, but she did not love the king. One day, she said she knew magical arts and could cut off someone's head and put it back on again. Ferdinand the Unfaithful suggested Ferdinand the Faithful, and she cut off his head and put it back on again. Then the king said she could do it to him as well, and she cut off his head, pretended she could not put it back on, and married Ferdinand the Faithful.