------
## 最终结论
To solve the problem of counting ambiguous pairs in the Berland calendar system, we need to implement a function that processes multiple test cases and determines the number of such pairs for each case. The logic involves understanding how days align between months and weeks in this fictional calendar system.

Here's the Python code that implements this solution:

```python
def count_ambiguous_pairs(t, test_cases):
    results = []
    
    for m, d, w in test_cases:
        # Calculate gcd(d-1, w) which is needed to find the step size
        from math import gcd
        g = gcd(d - 1, w)
        
        # Calculate the maximum possible x value
        max_x = min(m, (w // g))
        
        # Calculate the number of ambiguous pairs
        ambiguous_pairs = 0
        
        # Iterate over possible x values
        for x in range(1, max_x + 1):
            # Calculate y start position based on x and step size
            step_size = (w // g)
            y_start = x + step_size
            
            # Calculate number of valid y values for each x
            if y_start <= m:
                num_y_values = (m - y_start) // step_size + 1
                ambiguous_pairs += num_y_values
        
        results.append(ambiguous_pairs)
    
    return results

# Read input
t = int(input().strip())
test_cases = [tuple(map(int, input().strip().split())) for _ in range(t)]

# Get results
results = count_ambiguous_pairs(t, test_cases)

# Print results
for result in results:
    print(result)
```

### Explanation

1. **Input Parsing**: We read the number of test cases `t` and then parse each test case into a tuple `(m, d, w)`.

2. **GCD Calculation**: For each test case, we calculate `gcd(d-1, w)`, which helps determine how days align between weeks and months.

3. **Iterate Over Possible Values**: We iterate over possible starting days `x` of month `y` and calculate potential matching days `y` of month `x`.

4. **Step Size**: The step size between potential matching days is determined by `(w // gcd(d-1, w))`. This reflects how often days align due to their periodic nature.

5. **Count Ambiguous Pairs**: For each valid starting day `x`, compute how many valid `y` can be formed such that they satisfy the condition `(x - 1) * d % w == (y - 1) * d % w`.

6. **Output Results**: Finally, we output the number of ambiguous pairs for each test case.

This code efficiently calculates ambiguous pairs by leveraging mathematical properties of modular arithmetic and greatest common divisors to reduce unnecessary computations.