Latin Americans know by now that picking sides in a trade war is a fool’s errand. Not for nothing did World Trade Organization Director-General Roberto Azevedo, a Brazilian, call this the worst moment for free trade since 1947.

So there was widespread relief when the United States and China, who represent a third of global economic output and a lifeline for struggling Latin American markets, seemed to table their conflict at the G-20 Summit of the world’s advanced economies in Buenos Aires last weekend.

Whether this message reached Brazilian President-elect Jair Bolsonaro is less clear. More than any of his regional peers, Bolsonaro shares President Donald Trump’s flair for bilious populism, nationalistic rants, a soft spot for military Caesarism, and impatience for multilateral bodies such as the WTO.

Bolsonaro, who takes office January 1, passed up sitting President Michel Temer’s invitation to listen in on the G-20 consort. That may be just as well: Bolsonaro’s take on world trade – like his views on foreign affairs, the global climate, and congress – is still anyone’s guess. Yet Brazil has much to gain – or to lose – by the course it charts through the treacherous waters of a U.S.-China trade war.

The one-time paratrooper has already retreated from his campaign vitriol over China as an economic predator, thanks in part to a house call and presumed scolding by the Chinese ambassador. China is not just Brazil’s biggest customer and investor; its appetite for Latin America goods and assets reveals interest that U.S. policymakers rarely have shown – least of all under Trump’s wandering diplomatic eye.

Thanks to Chinese demand, Brazil’s soybean exports leaped nearly 137 percent in the year to November, according to Bloomberg. And even if China lowers its 25 percent tariff on U.S. soy, most Asian buyers have locked in their contracts with South American suppliers for the next few months, according to Alvin Tai of Bloomberg Intelligence.

But since trade battles cut both ways, the windfall could prove elusive. Consider that the sudden boon for Latin American soy sellers has been the bane of its meat exporters, who’ve been forced to fatten their livestock on pricier feed grain. Enter the G-20 truce, which once again could favor North American suppliers, and the coming Brazilian bumper crop, which will shrink the premium on its beans, and Latin America’s collateral trade advantage “looks to be fading,” Bloomberg News projected on Monday. “In a trade war, even the winners can be losers,” said Marcos Jank, a Brazilian agribusiness expert with the Asia Brazil Agro Alliance.

Given that Brazil’s historical ties to the U.S. are now competing with its newfound, if uneasy, reliance on Chinese capital and customers, the stakes are huge. “China and the U.S. are both critical partners, and in critically different ways,” said Margaret Myers, who studies China’s involvement in Latin America at the Inter-American Dialogue. “The problem is the trade war is not just about trade. This also is the Thucydides Trap playing out, with both economic powers battling to promote different models of development.”

The challenge for Brazil, as for its neighbors, is to choose the right battles without picking hegemons. “To take sides with the U.S. is dangerous and could distance us from China, which buys 30 percent of our agricultural exports, while the U.S. buys just 10 percent,” said Jank.

Such numbers ought to discourage precipitous talk, such as Bolsonaro’s Trump-inspired announcement to transfer Brazil’s embassy in Israel from Tel Aviv to Jerusalem, a suggestion that rankled Brazil’s many Middle Eastern customers and allies. “Jerusalem is not our battle,” Jank said.

The prickly environmental agenda most certainly is. Although rainforest destruction has spiked this year, the government’s recent record in reducing the felling together with its longtime success in distilling sugar into clean-burning fuel have bought Brazil green cachet. “We’ve become a player in the global environment,” said Jank. To abandon the Paris Agreement on climate, as Bolsonaro has threatened to do, “would mean that we relinquish that accomplishment, and that could be dangerous for Brazil’s reputation and our brands,” he said.

Perhaps the best way to survive a trade war would be to diversify the economy. Brazil until now has prospered by meeting China’s demand for raw materials, but that was last century’s tune. Fortunately, China’s evolving economy offers an opportunity for economic rebalancing. “[T]he rising purchasing power of the Chinese consumer will require a reliable source of imports that go beyond natural resources,” the Economist Intelligence Unit recently concluded. Hence Beijing’s ambitions to roll out its Belt and Road initiative across the Americas, with huge investments in infrastructure, logistics, manufacturing and technology.

That initiative is welcome, but it won’t give Brazil a fast pass to the emerging Chinese consumer. As Brazil has raised its game selling packaged meat, sugar and other industrialized farm products, “we’ve seen an explosion in anti-dumping measures, tariffs and safeguards from China on value-added agricultural goods this year,” said Jank. To lock in the gains of higher-end trade, Brazil needs to wage a policy war of its own - on waste, inefficiency, stagnated productivity and bureaucratic torpor. “More than 90 percent or our goods are shipped by truck over highways,” Mauro Laviola, vice president of the Brazilian Foreign Trade Association, said. “That’s inefficient, wasteful and hugely expensive.” Without better infrastructure, a technological upgrade and heavier investment in research and development, Brazilian productivity could fall as much as 45 percent by 2030, according to McKinsey.

Another speed bump: Brazil’s convoluted tax system, which strangles enterprise. While tax legislation ostensibly exempts exports, “in reality that doesn’t happen, because the fiscal deficit is out of control,” Laviola said. “Counting levies on intermediate goods, we pay six to 15 percent of the value of exports, and the reimbursement for those taxes is negligible.”

Aerating trade would also help. While Brazil has traditionally relied on multilateral institutions – such as the World Trade Organization’s conflict resolution tribunal - it also needs new strategic partners and bilateral agreements. “Mercosur [the South American trade bloc] has gone nowhere. We indulged dysfunctional allies, like Venezuela, and distanced ourselves from the U.S. for ideological reasons,” said Jank. “We haven’t had a real trade policy for the last 20 years.”

There is a way to survive the gathering trade turmoil, but it takes reforms at home, a broader economic base, and smarter alliances without playing favorites. “Any form of diversification is helpful,” said Myers. “The more diverse economies fare better in hard times.”How to manage the complicated relationship with China? “The next Brazilian government should send a representative to Beijing to meet with the agriculture minister, customs officials and state buyers,” said Andre Nassar, president of Abiove, the Brazilian soybean industry association. “That would send a positive signal that we want to strengthen ties and reaffirm our trade agenda. I’d do that right now.”

As for those perks of being Washington’s best Latin friend? Look no further than Colombia, once the U.S.’s closest hemispheric ally. Trump twice canceled visits, and threatened to decertify it over drug trafficking. Or Argentina, where last weekend he turned heel on President Mauricio Macri, leaving his long-time family friend alone on an empty stage.

That’s a scene Brazil’s Bolsonaro might want to keep in mind now that the hegemons have gone home.