#include <iostream>
using namespace std;

const int MAX = 4;

long keys[MAX];

long getSize(long n)
{
	long ans=1;
	while (ans <= n)
		ans *= 10;
	return ans;
}

long process(long val)
{
	long key = keys[MAX-1];
	long size = getSize(key);
	long ans = val % size;
	long lastMod = key;
	for(int i=MAX-2; i>=0; i--) {
		val /= size;
		long prevKey = key;
		key = keys[i];
		size = getSize(key);
		long rem = val % size;
		for(int k=0; k<key; k++) {
			ans += lastMod;
			if (ans % key == rem)
				break;
			if (ans < 0) {
				cout << "oops!";
				exit(-1);
			}
		}
		lastMod *= key;
		ans %= lastMod;
	}
	return ans;
}
		
void print(long val, bool flag)
{
	char ch;
	ch = 'A' + (val/10000)-1;
	if (ch == '[')
		ch = ' ';
	cout << ch;
	val %= 10000;
	ch = 'A' + (val/100)-1;
	if (ch == '[')
		ch = ' ';
	if (!flag || ch != ' ')
		cout << ch;
	val %= 100;
	ch = 'A' + val-1;
	if (ch == '[')
		ch = ' ';
	if (!flag || ch != ' ')
		cout << ch;
}

int main()
{
	int ncase;
	int m;
	int i;
	cin >> ncase;

	for(int icase=1; icase<= ncase; icase++) {
		cin >> m;
		for(i=0; i<MAX; i++) {
			cin >> keys[i];
		}
		for(i=0; i<m; i++) {
			long val;
			cin >> val;
			val = process(val);
//			cout << val << endl;
			print(val, i==m-1);
		}
		cout << endl;
	}

	return 0;
}
