//1062
#include"stdio.h"
#include"stdlib.h"typedef struct bb{
char *top;
char *base;
int stasksize;
}stack,*sb;
int initstack(sb a)
{
a->base=(char*)malloc(10000*sizeof(char));
if(!a->base) exit(1);
a->top=a->base;
a->stasksize=10000;
return 1;
}
int gettop(stack a,char *e)
{
if(a.top==a.base) return 0;
*e=*(a.top-1);
return 1;
}
int push(sb a,char e)
{
*a->top++=e;
return 1;
}
int pop(sb a,char *e)
{
if(a->top==a->base) return 0;
*e=*--a->top;
return 1;
}
int isempty(stack a)
{
if(a.top==a.base) return 0;
else return 1;
}
int main()
{
int t;
scanf("%d",&t);
getchar();
while(t--)
{
stack a;
sb asb=&a;
initstack(asb);
char b[1000],e;
char *esb=&e;
gets(b);
int i;
for(i=0;b[i];i++)
{
if(b[i]==32)
{
while(isempty(a))
{
pop(asb,esb);
if(e==32);
else printf("%c",e);
}
printf(" ");
}
else push(asb,b[i]);
}
while(isempty(a))
{
pop(asb,esb);
if(e==32);
else printf("%c",e);
}
printf("\n");
}
return 0;
}