What the government giveth, the government can taketh away.

It's an unhappy lesson that many of the firms that dot the Beltway learn as they hitch their stars to a couple of multimillion-dollar government contracts, enjoy overnight success, then watch that success slip away when the contracts run out and the companies are unable to drum up enough new business to replace them.

These firms offer brain power -- instead of manufacturing ability -- to the government, and thus can spring up seemingly overnight. While some have managed to balance the cyclical nature of government contracts, others have suffered through a roller-coaster existence as they struggled with the end of major contracts, lost bids and costly auditing. Recent changes in the government procurement process have made it an even riskier ride for small companies.

"When a smaller company loses a major contract, to them the whole world ends. I've been there myself," said Howard C. Mills, president of Halifax Engineering Inc., a professional and technical services firm in Alexandria.

Part of the reason that a small company can find itself in this fix is also part of the allure for going into the government contracting business: A very small company, particularly in the services area, can win multimillion-dollar awards that suddenly put the firm on the map.

"That rarely happens in any other sector," said Robert Erikson, president of Cerberonics Inc., a Fairfax-based professional services contracting company. "Even if you opened one of the best little hamburger stalls in Northern Virginia or one of the most efficient delivery services in the Washington area, ... it's very difficult to wake up one morning with a staggering amount of revenue from which you can instantly hire sometimes upwards of hundreds of people and be in full-scale business."

But it happens in the federal contracting business.

However, Erikson added, "as great as those big things are when you get it, you really have to be in place to lose them."

Companies that are not fully prepared to lose contract work get badly burned when those contracts end. Erikson, a thoughtful and outspoken executive, knows the story well. He has lived it, and he has taken radical steps at Cerberonics to try to avoid going through it again.

A few years ago, Cerberonics was a small company -- $8 million in revenue and 243 employees in 1980 -- that provided engineering and analytical and support services to the federal government.

But in 1983 the company hit gold, winning a major contract to manage the spare parts and maintenance of more than 40 C9 aircraft, the military's workhorse for air transport and medical evacuation.

Although it was a one-year agreement, the government had the option to renew the contract at the end of each year for four additional years. That meant a potential $90 million in revenue over five years for Cerberonics.

The company's work force doubled. Revenue hit $32 million in 1983, and $40 million the next. It was a dizzy, exhilarating climb. By 1985, the C9 contract accounted for 60 percent of Cerberonics' annual revenue.

But that year, the C9 project was expanded, and there was another competition for the contract.

Cerberonics lost. The blow was completely unexpected.

"You go in nervous but somewhat optimistic and confident because you have these glowing letters in praise of your performance," Erikson said. "And then you get the phone call at the end of the competition that says, 'Gee, we're sorry, but Johnny Jones Inc. bid lower.' "

To make matters worse, Cerberonics lost its other principal government contract in a recompetition the same year. That contract -- to provide material support services for TC 4C aircraft used to train crews in electronic warfare -- accounted for $2.6 million, or 7 percent, of Cerberonics' fiscal 1985 revenue.

In one year, the company saw two-thirds of its revenue wiped out. By last year, Cerberonics' revenue had fallen to $12 million, and its work force had dropped to 175 employees.

"First you're in shock and disbelief," Erikson said. "When you hit the anger phase, you think of protests and litigation. Then you hit the reconciliation phase and you try and get a little wiser."

The consensus seems to be that small companies like Cerberonics are most vulnerable to these sort of jolts. May G. O'Leary, an analyst for Baker, Watts & Co., compared the situation facing Cerberonics to that of McLean-based professional services powerhouse BDM International Inc., which had revenue of more than $320 million last year.

"BDM is extremely diversified, and with about 800 contracts, the loss of one of those contracts to BDM would not be as severe as it would be to a company like Cerberonics," she said.

"A lot of these companies falter in the $20 {million} to $40 million range," said Jack Aalseth, chairman of ERC International Inc., a Fairfax-based professional services firm with 1986 revenue of $95 million.

Halifax Engineering falls in that range, with 1987 revenue of $25.8 million. Mills said, however, that the company is not dependent on any particular contract. "We're really in five different lines of business. When some of those are going well, others are not going so well." The firm provides telecommunications, computer services, engineering, security services and facilities operation and maintenance services to the government.

Nevertheless, the completion of two of Halifax's major contracts in fiscal 1987 was partly responsible for an 8 percent drop in the company's revenue last year, according to Halifax's annual report.

In the wake of its sudden tumble, Cerberonics also decided to spread its risks, but it has taken the idea one step further than Halifax. It is looking outside the federal government contracting market for business from municipal governments and the commercial sector.

The firm spent $5.2 million to buy the controlling interest in Insituform East Inc., a small but rapidly growing Landover company that makes linings for deteriorated sewers and pipelines, allowing municipal governments to avoid the costly and time-consuming process of ripping up streets and laying new pipes.

"Some of these sewer systems were built with wood," Erikson said. "They're collapsing all over the place."

More recently, Cerberonics made a plunge into the commercial sector. Last fall, it paid $3.5 million for Capitol Copy Products Inc. of Beltsville, which sells copiers and facsimile-transmission machines in the Washington area. "That, we think, should be a real stable base," Erikson said. "Even in a general recession, there are two things business needs ... a telephone and copier machine."

Company officials expect Insituform East to account for about $20 million in revenue in 1988, Capitol Copy Products about $10 million and Cerberonic's professional and technical services business about $6 million. Thus the company's federal contracting business will make up a small portion of the revenue pie in the newly restructured company. The reason: Erikson remains pessimistic about the federal contracting business because of changes in the procurement process.

In recent years, the government has adopted a "get tough" attitude in the wake of contracting scandals. For example, the government now is shying away from sole-source contracts in favor of more frequent competitive bidding, and when it does award sole-source contracts, it has reduced the profits on those projects. The government also has cracked down on the types of expenses for which it will reimburse contractors and is having contractors assume more risks.

"Competition is a great motherhood thing," Erikson said. "Who could be against competition?" But he argued that in recent government contract competitions, emphasis has been placed on lowest price instead of on highest quality.

"Imagine if you applied that kind of criteria to the world of services," he said. "Suppose it was legislated that you had to go to the lowest-priced doctor or lawyer or accountant." Top-flight engineers and computer scientists, he said, don't come cheap.

Some government contractors disagree with assertions that new procurement laws have jeopardized quality and hurt profits. They suggest that such handwringing is merely sour grapes by losers.

But a growing chorus of contactors seems to be siding with Erikson. "In the service area, the competition is incredibly fierce," said Robert B. Pirie Jr., president of Essex Corp., a professional services firm in Alexandria. He said some companies now make bids that include profits of 4 percent, compared to the 7 percent to 8 percent profit that professional service firms have generally obtained on federal contracts in recent years.

Like Cerberonics, Essex is seeking to increase the commercial side of its business, which now accounts for about 10 percent of its revenue.

Some professional services executives warn that the transition into the commercial sector can be fraught with hazards. ERC found that out the hard way with a foray into the commercial software market.

"It's a characteristic of this industry that we develop a lot of tools -- proprietary software, processes and procedures -- and we tend to get caught up in them ... thinking that the whole world needs them," Aalseth said, explaining why ERC decided to venture into the new field.

But he said ERC soon discovered that commercial software "is a completely different marketplace" than writing computer programs for the government.

"It's selling lower-cost items to a broad spectrum of clients across the country, which requires setting up a national sales and support organization," he said.

ERC eventually dropped out of the business, selling the software operation to private investors. But that doesn't mean ERC has given up on the commercial sector.

The company has gone from 100 percent Defense Department work a decade ago to an even split between commercial and government work.

The key, said ERC President John Gray, is that the company diversified through acquisition.

"We don't arrive as strangers in those markets," he said. ERC has acquired a number of energy and environmental firms, dealing with projects ranging from hazardous waste disposal to the design and construction of the nuclear power plant at Comanche Peak, Tex.

Gray said the acquisitions have put ERC on a strong footing in commercial markets, and he predicts healthy growth for the company this year.

Likewise, Erikson said he believes Cerberonics' acquisitions in commercial fields will help turn the company around. But he is less upbeat about the company's remaining contracting businesses.

"This may be a passing thing. But I frankly no longer hold an optimistic view for medium- and smaller-sized firms," he said.

Nevertheless, he said, Cerberonics has no plans to pull out of the government contracting business.

He wants the company to be there when the pendulum swings the other way, which he predicts will happen.

"We're going to batten down the hatches here and ride this one out," he said.