Ahmed homers twice, Diamondbacks beat Giants 9-3