SAN FRANCISCO (AP) — Thairo Estrada hit a two-run homer with one out in the ninth inning and the San Francisco Giants edged the Pittsburgh Pirates 8-7 in a back-and-forth game.

Evan Longoria, whose wild throw gave Pittsburgh the go-ahead run in the top of the ninth, started the rally in the bottom half with a leadoff single against Wil Crowe.

After pinch-runner J.D. Davis was forced out at second on a grounder by Mike Yastrzemski, Estrada cleared the wall in left-center for his 11th homer.

Bryan Reynolds homered, doubled and drove in five runs for the Pirates, who lost their their fourth in a row.

They went 2-8 on a road trip that covered nearly 6,000 miles from Baltimore to Arizona to San Francisco.