```
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int main() {
int test_cases;
cin >> test_cases;
while (test_cases--) {
int r;
cin >> r;
vector<int> houses(r);
for (int i = 0; i < r; i++) {
cin >> houses[i];
}
// 排序門牌號碼
sort(houses.begin(), houses.end());
// 找到中位數
int median;
if (r % 2 == 0) {
median = houses[r / 2 - 1]; // 偶數的情況取下中位數
} else {
median = houses[r / 2]; // 奇數的情況取中位數
}
// 計算距離和
long long total_distance = 0;
for (int i = 0; i < r; i++) {
total_distance += abs(houses[i] - median);
}
cout << total_distance << endl; // 輸出結果
}
return 0; // 確保返回語句
}
```