#include <iostream>
#include <string>
#include <boost/algorithm/searching/knuth_morris_pratt.hpp>

int main(){
    size_t pos;
    int n;
	std::string T;
	std::cin >> T;
	std::cin >> n;
    std::string T = "BANNANABANANAN";
    for(int i=0;i<n;i++){
    	std::string P;
    	cin >> P;
    	std::string::const_iterator search_result;
    	search_result = boost::algorithm::boyer_moore_search(T.begin(), T.end(),P.begin(), P.end());
    	if(search_result - T.begin() >= 0){
    		printf("1\n");
		}else{
   		 	printf("0\n");
   		}
   	}
}