// Arup Guha
// 10/2/2026
// Solution to COP 3503 Exam 1 Question 6

import java.util.*;

public class e1q6 {

	// Runs a simple test.
	public static void main(String[] args) {
		String sStr = "BACD";
		int n = sStr.length();
		printPossible(new char[n], 0, sStr.toCharArray(), new boolean[26]);
	}
	
	// Prints out all solutions where the first k items of the curString are fixed.
	// used stores which letters have already been placed in curString  and s is the original
	// string where our letters have to be "close" to.
	public static void printPossible(char[] curString, int k,  char[] s, boolean[] used) {
	
		// Filled it!
		if (k == s.length) {
			System.out.println(new String(curString));
			return;
		}
	
		// Go through all neighoring letters.
		for (int i=-3; i<=3; i++) {
			
			// But can't be the same letter.
			if (i == 0) continue;
			
			// This is the letter we are considering.
			char c = (char)(s[k] + i);
			
			// Making sure it's a letter and acting accordingly.
			if (c < 'A') continue;
			if (c > 'Z') break;
			
			// We've used this letter before.
			if (used[c-'A']) continue;
		
			// Mark it and put it in the string.
			used[c-'A'] = true;
			curString[k] = c;
			
			// Recurse.
			printPossible(curString, k+1, s, used);
			
			// Unmark for next iteration.
			used[c-'A'] = false;
		}
	
	}

}