#include<iostream>
#include<vector>
#include<string>

using namespace std;

void test(){
	int n,k;
	cin>>n>>k;
	char a;
	
	//An array to keep the available letters count.
	int letters [26];
	for(int i = 0; i < 26; i++){
		letters[i]=0;
	}
	
	//Import letters to the array
	for(int i = 0; i < n; i++){
		cin>>a;
		letters[a-'A']++;
	}
	
	//Letters array for a single word;
	int wordletters[26];
	for(int i =0; i<k;i++){
		
		for(int j = 0; j < 26; j++){
			wordletters[j]=0;
		}
		
		string word;
		cin>>word;
		for(int j = 0; j < word.length(); j++){
			wordletters[word[j]-'a']++;
		}
		
		bool ispossible = true;
		
		for(int j = 0; j < 26; j++){
			if(wordletters[j]>letters[j]){
				ispossible=false;
				break;
			}
		}
		if(ispossible){
			cout<<"TAK\n";
		}
		else{
			cout<<"NIE\n";
		}
	}
}


int main(){
//	ios_base::sync_with_stdio(false);
//	cin.tie(NULL);
	int T;
	cin>>T;
	while(T-->0){
		test();
		cout<<'\n';
	}

	return 0;
}