#include <bits/stdc++.h>
using namespace std;
int d,i,d1,n;
string a[100],st;
int main()
{
cin>>n;
for (i=1; i<=n; i++){
getline(cin,a[i]);
}
for (i=1; i<=n; i++)
{
st=a[i];
d=st.length();
while (st[0]==32)
{
st.erase(0,1);
}
while (st[d-1]==32)
{
st.erase(d-1,1);
}
d1=st.length();
for (i=0; i<d1; i++)
if ((st[i]==32) && st[i+1]==32)
{
st.erase(i,1);
i--;
}
cout<<st<<endl;
}
return 0;
}
PASCAL:
Program HOC24;
var a: array[1..100] of string;
t,i: byte;
function chuyendoi(s: string): string;
var b: array[1..100] of string;
j,k,x: byte;
st: string;
begin
chuyendoi:='' st:=''
while s[length(s)]=#32 do delete(s,length(s),1);
while s[1]=#32 do delete(s,1,1);
while pos(#32#32,s)<>0 do delete(s,pos(#32#32,s),1);
k:=0; s:=s+#32; s:=lowercase(s);
while length(s)<>0 do
begin
k:=k+1;
b[k]:=copy(s,1,pos(#32,s)-1);
delete(s,1,pos(#32,s));
end;
for j:= 1 to (k-1) do b[j][1]:=upcase(b[j][1]);
b[k]:=upcase(b[k]);
for j:=1 to k do
if k-1=j then st:=st+b[j]+','+#32 else st:=st+b[j]+' '
chuyendoi:=st;
end;
begin
readln(t);
for i:=1 to t do readln(a[i]);
for i:=1 to t do writeln(chuyendoi(a[i]));
readln
end.