using System;
using System.Collections.Generic;
using System.Text;

class TEST{
	static void Main(){
		while(true){
			String s;
			if((s=Console.ReadLine())==null)break;
			Dictionary<String,String> strSets=new Dictionary<String,String>();
			while(true){
				var t=Console.ReadLine();
//Console.WriteLine(s.Substring(0,1)+"\t"+t);
				strSets.Add(s.Substring(0,1),t);
				s=Console.ReadLine();
				if(s[0]=='R')break;
				//s=Console.ReadLine();
			}
			String expr=Console.ReadLine();
			Sol mySol=new Sol(strSets,expr);
			Console.WriteLine(mySol.Ans);
		}
	}
}


class Sol{
	String ans;
	public String Ans{
		get{return ans;}
	}
	
	
	Set U;
	
	public Sol(Dictionary<String,String> strSets,String expr_in){
		String expr=expr_in;

		Set[] sets=new Set[strSets.Count];
		
		int i=0;
		foreach(KeyValuePair<string, string> kvp in strSets){
			var s=kvp.Value.Replace(' ',',');
			s="{"+s+"}";

			sets[i]=new Set(s);
			i++;
			//式中はすべてcsv({a,b,...})で統一
			expr=expr.Replace(kvp.Key,s);
		}
		
		U=Set.CreateAllUnion(sets);

		String ss=ParseAndCalc(expr);
		
		if(ss=="{}")ans="NULL";
		if(ss!="{}"){
			ss=ss.Substring(1,ss.Length-2);
			ss=ss.Replace(',',' ');
			ans=ss;
		}
	}

	String ParseAndCalc(String expr){
		String ret=String.Copy(expr);
		bool chk=true;
		while(chk){
			chk=false;
			int i;
			
			//まず'('を処理する
			for(i=0;i<ret.Length;i++){
				if(ret[i]=='('){
					chk=true;
					break;
				}
			}
			if(chk){
				int depth=1;
				int j=i;
				while(depth!=0){
					j++;
					if(ret[j]=='(')depth++;
					if(ret[j]==')')depth--;
				}
				var s=ret.Substring(i+1,j-i+1-2);
				ret=ret.Substring(0,i)+ParseAndCalc(s)+ret.Substring(j+1);
				continue;
			}
			//次にComplementを処理
			for(i=0;i<ret.Length;i++){
				if(ret[i]=='c'){
					chk=true;
					break;
				}
			}
			if(chk){
				int rt=0;
				int rb=0;
				//右オペランド
				rt=i+1;
				for(rb=rt;ret[rb]!='}';rb++);
				Set R=new Set(ret.Substring(rt,rb-rt+1));
				
				ret=ret.Substring(0,i)+Set.CreateSub(U,R).toCsv()+ret.Substring(rb+1);
				continue;
			}
			
			
			//集合演算子は先頭から処理。
			for(i=0;i<ret.Length;i++){
				if(ret[i]=='u'||ret[i]=='i'||ret[i]=='d'||ret[i]=='s'){
					chk=true;
					break;
				}
			}

			if(chk){
				int lt=0;
				int lb=0;
				int rt=0;
				int rb=0;
				//右オペランド
				rt=i+1;
				for(rb=rt;ret[rb]!='}';rb++);
				Set R=new Set(ret.Substring(rt,rb-rt+1));
				//左オペランド
				lb=i-1;
				for(lt=lb;ret[lt]!='{';lt--);
				Set L=new Set(ret.Substring(lt,lb-lt+1));
				
				switch(ret[i]){
					case 'u':
						ret=ret.Substring(0,lt)+Set.CreateUnion(L,R).toCsv()+ret.Substring(rb+1);
					break;
					case 'i':
						ret=ret.Substring(0,lt)+Set.CreateIntersection(L,R).toCsv()+ret.Substring(rb+1);
					break;
					case 'd':
						ret=ret.Substring(0,lt)+Set.CreateSub(L,R).toCsv()+ret.Substring(rb+1);
					break;
					case 's':
						ret=ret.Substring(0,lt)+Set.CreateSymDiff(L,R).toCsv()+ret.Substring(rb+1);
					break;
				}
				continue;
			}

		}
		
		return ret;
	}


}





class Set{
	List<int> content;
	public int this[int i]{
		//set{content[i]=value;}
		get{return content[i];}
	}
	
	public int Size{
		get{return content.Count;}
	}

	public String toCsv(){
		String s="{";
		if(this.Size>0){
			String[] t=Array.ConvertAll(content.ToArray(),elem=>elem.ToString());
			s=s+String.Join(",",t);
		}
		s+="}";
		return s;
	}
	


	
	public Set(List<int> Lin){
		content=new List<int>();
		for(int k=0;k<Lin.Count;k++){
			content.Add(Lin[k]);
		}
		//sort when constructed
		content.Sort();
	}
	public Set(int[] arr){
		content=new List<int>();
		for(int k=0;k<arr.Length;k++){
			content.Add(arr[k]);
		}
		//sort when constructed
		content.Sort();
	}
	public Set(String csv){ //csv: {a,b,...} or {}(empty)
		content=new List<int>();
		if(csv.Length>2){
			var ss=csv.Substring(1,csv.Length-2).Split(',');
			for(int k=0;k<ss.Length;k++){
				content.Add(int.Parse(ss[k]));
			}
			//sort when constructed
			content.Sort();
		}
	}
	
	
	
	
	public static Set CreateIntersection(Set A,Set B){
		List<int> L=new List<int>();
		for(int i=0;i<A.Size;i++){
			for(int j=0;j<B.Size;j++){
				if(A[i]==B[j]){
					L.Add(A[i]);
					break;
				}
			}
		}
		return new Set(L);
	}
	public static Set CreateUnion(Set A,Set B){
		List<int> L=new List<int>();
		for(int i=0;i<B.Size;i++){
			L.Add(B[i]);
		}
		
		for(int i=0;i<A.Size;i++){
			if(L.Contains(A[i])==false)L.Add(A[i]);
		}
		return new Set(L);
	}
	public static Set CreateSub(Set A,Set B){
		List<int> L=new List<int>();
		for(int i=0;i<A.Size;i++){
			L.Add(A[i]);
		}
		
		for(int j=0;j<B.Size;j++){
			if(L.Contains(B[j]))L.Remove(B[j]);
		}
		return new Set(L);
	}
	
	public static Set CreateSymDiff(Set A,Set B){
		return Set.CreateUnion( Set.CreateSub(A,B),Set.CreateSub(B,A));
	}
	
	public static Set CreateAllUnion(Set[] sets){

		int[] arr=new int[sets[0].Size];
		for(int i=0;i<sets[0].Size;i++)arr[i]=sets[0][i];
		Set U= new Set(arr);

		if(sets.Length==1){
			return U;
		}
		
		Set V=U;
		for(int i=1;i<sets.Length;i++){
			V=Set.CreateUnion(V,sets[i]);
		}
		return V;
	}
}
	

Measured memory_bytes: 6384