#include <iostream>
#include <string>
#include <vector>
#include <algorithm> // 包含std::reverse  

using namespace std;

int main() {
    int t;
    cin >> t; // 读取询问组数
    vector<string> results;

    for (int i = 0; i < t; ++i) {
        int n;
        cin >> n; // 读取字符串长度(在这个问题中实际上不需要使用这个信息)
        string s;
        cin.ignore(); // 忽略前一个输入后的换行符
        getline(cin, s); // 读取整行字符串,包括空格

        // 去除空格并倒置字符串
        string no_spaces;
        for (char c : s) {
            if (c != ' ') {
                no_spaces += c; // 使用+=来添加字符到字符串末尾
            }
        }
        reverse(no_spaces.begin(), no_spaces.end()); // 倒置字符串

        results.push_back(no_spaces); // 将结果存储在vector中
    }

    // 输出结果
    for (const auto& result : results) {
        cout << result << endl;
    }

    return 0;
}

C++去掉空格:

// 去除空格并倒置字符串
string no_spaces;
for (char c : s) {
  if (c != ' ') {
	no_spaces += c; // 使用+=来添加字符到字符串末尾
  }
}
reverse(no_spaces.begin(), no_spaces.end()); // 倒置字符串