#include <iostream> //根据先序遍历序列和中序遍历序列建立二叉树,并按层遍历
#include <string>
using namespace std;
string str1,str2;
int n;
struct Node
{
char ch;
Node *left,*right;
}node[5000];
Node* built(int s1,int t1,int s2,int t2) //根据先序遍历序列和中序遍历序列建二叉树
{
int m=n++; //声明为局部变量
node[m].ch=str1[s1];
int mid=str2.find(str1[s1]); //str1[s1]为根结点字符
if(s2<mid)
node[m].left=built(s1+1,s1+mid-s2,s2,mid-1); //递归构造左子树,左子树结点总数是mid-s2
if(t2>mid)
node[m].right=built(s1+mid-s2+1,t1,mid+1,t2); //递归构造右子树,右子树结点总数是t2-mid
return &node[m]; //返回根结点指针
}
void bfs(Node *root) //按层遍历
{
Node *col[1000],*temp;
int front=0,rear=0;
col[rear++]=root;
while(front<rear)
{
temp=col[front++];
cout<<temp->ch;
if(temp->left!=NULL)
{
col[rear++]=temp->left;
temp->left=NULL; //遍历时顺便对整棵树进行初始化
}
if(temp->right!=NULL)
{
col[rear++]=temp->right;
temp->right=NULL;
}
}
cout<<endl;
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>str1>>str2;
n=0;
Node *root=built(0,str1.size()-1,0,str2.size()-1);
bfs(root);
}
return 0;
}