Pirates beat Giants 4-1, break 5-game losing skid