Before Báthory's election to the throne of the Commonwealth, Ivan the Terrible of Russia had begun encroaching on its sphere of interest in the northeast, eventually invading the Commonwealth borderlands in Livonia; the conflict would grow to involve a number of nearby powers . Each of them was vying for control of Livonia, and the resulting conflict, lasting for several years, became known as the Livonian War. By 1577 Ivan was in control of most of the disputed territory, but his conquest was short-lived. In 1578 Commonwealth forces scored a number of victories in Liviona and begun pushing Ivan's forces back; this marked the turning point in the war. Báthory, together with his chancellor Zamoyski, led the army of the Commonwealth in a series of decisive campaigns taking Polotsk in 1579 and Velikiye Luki in 1580. In 1581 Stephen penetrated once again into Russia and, on 22 August, laid siege to the city of Pskov. While the city held, on 13 December 1581 Ivan the Terrible began negotiations that concluded with the Truce of Jam Zapolski on 15 January 1582. The treaty was favorable to the Commonwealth, as Ivan ceded Polatsk, Veliz and most of the Duchy of Livonia in exchange for regaining Velikiye Luki and Nevel.
Question: Which happened first, the invasion of the Commonwealth borderlands in Livonia, or Báthory's election to the throne?
Answer: invading the Commonwealth borderlands