import java.io.*; import java.util.*; class LInt extends ArrayList { LInt(int v) { super(); add(v); } int last() { return get(size()-1); } void pop() { remove(size()-1); } } public class enzyme_inaba { //////////////////////////////// 再帰で探索 ////////////////////////////// List ans; // 見つかった答えはここに入れる void solve() { // 辞書順最小 → 1番目は "0 A" LInt cutA = new LInt(0); // 辞書順最小 → 2番目は "AB.first() B" とわかる場合がある if( AB.first() < Math.min(A.first(), B.first()) ) rec( cutA, new LInt(AB.first()) ); //あとは探索 else { // 辞書順最小 → 2番目は "AB.first() A" とわかる場合もある if( AB.first() == A.first() ) cutA.add(AB.first()); rec( cutA ); //あとは探索 } // 答えを出力 System.out.println(ans.size()); for(int i=0; i=0 ) System.out.println( ans.get(i)+" A"); else System.out.println(-ans.get(i)+" B"); } // まだAでしか切ってないよ状態 void rec( LInt cutA ) { for( int ab : AB ) { int next = cutA.last() + ab; // 次の切り口 if( next >= B.last() ) break; // 長すぎるB片ができちゃうのでアウト if( A.contains(next-cutA.last()) ) {cutA.add(next); rec(cutA); cutA.pop();} // Aで切る場合を探索 rec( cutA, new LInt(next) ); // Bで切る場合を探索 } } // A,B両方で切ったよ状態 void rec( LInt cutA, LInt cutB ) { // 枝カット // 辞書順の小さい方から順番に探索しているので、 // すでに求まってる解と同じ長さのカット列ができた時点でアウト if( ans!=null && cutA.size()+cutB.size()>=ans.size() ) return; for( int ab : AB ) { int next = Math.max(cutA.last(), cutB.last()) + ab; // 次の切り口 if( next > Length ) break; if( A.contains(next-cutA.last()) ) // Aで切る場合を探索 if( next == Length ) check(cutA, cutB); // 1周したので解として妥当かチェック else {cutA.add(next); rec(cutA,cutB ); cutA.pop();} if( next == Length ) break; if( B.contains(next-cutB.last()) ) // Bで切る場合を探索 {cutB.add(next); rec(cutA,cutB); cutB.pop();} } } // 最終チェック void check( LInt cutA, LInt cutB ) { if( !B.contains(cutB.get(0)+Length - cutB.last()) ) return; // Bで切った場合の0をまたぐ部分の長さチェック if( !setOfCuts(cutA).containsAll(A) ) return; // Aを全部使ったか if( !setOfCuts(cutB).containsAll(B) ) return; // Bを全部使ったか List cutAB = new ArrayList(cutA); cutAB.addAll(cutB); Collections.sort(cutAB); if( !setOfCuts(cutAB).containsAll(AB) ) return; // ABを全部使ったか // 解発見。辞書順最小性は、探索ルーチンによりすでに保証されている ans = cutAB; for(int i=0; i setOfCuts( List a ) { Set s = new TreeSet(); for(int i=0; i A = new TreeSet(); SortedSet B = new TreeSet(); SortedSet AB = new TreeSet(); enzyme_inaba( Scanner in, int N ) throws IOException { Length = N; for(int k=in.nextInt(); k--!=0;) A.add( in.nextInt() ); for(int k=in.nextInt(); k--!=0;) B.add( in.nextInt() ); for(int k=in.nextInt(); k--!=0;) AB.add( in.nextInt() ); } public static void main( String[] _ ) throws IOException { Scanner in = new Scanner(System.in); boolean firstCase = true; for(int N; (N=in.nextInt())!=0;) { if( firstCase ) firstCase = false; else System.out.println(""); new enzyme_inaba(in, N).solve(); } } }