// Arup Guha
// 3/11/2022
// Solution to 2021 SER D1 Problem: Shortest Missing Subsequences

import java.util.*;
import java.io.*;

public class shortestmissingsub {

	public static int charSize;
	public static char[] alpha;
	public static char[] str;
	public static int lenShortest;
	public static int[] first;
	public static int[][] next;
	
	public static void main(String[] args) throws Exception {
	
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		alpha = stdin.readLine().toCharArray();
		str = stdin.readLine().toCharArray();
		charSize = alpha.length;
		
		// Get shortest missing length.
		lenShortest = getShortest();
		
		// next[letter][i] stores the next time letter appears after index i.
		first = new int[26];
		Arrays.fill(first, -1);
		makeNext();
		
		// Solve each query and add to our buffer.
		StringBuffer sb = new StringBuffer();
		int numQ = Integer.parseInt(stdin.readLine());
		for (int i=0; i<numQ; i++) 
			sb.append(solve(stdin.readLine().toCharArray())+"\n");
		
		// Ta da!
		System.out.print(sb);
	}
	
	// Returns 1 if input is the right length AND doesn't exist as a subsequence.
	public static int solve(char[] inp) {
		
		// Easy case.
		if (inp.length != lenShortest) return 0;
		
		int cur = first[inp[0]-'a'];
		
		// Just in case.
		if (cur == -1 && lenShortest == 1) return 1;
		
		// Use jump table to find letters.
		for (int i=1; i<inp.length; i++) {
			
			// Jump to next index storing inp[i] after index cur.
			cur = next[inp[i]-'a'][cur];
			
			// Never found it.
			if (cur == -1) return 1;
		}
		
		// If we made it through it's a subsequence.
		return 0;
	}
	
	// Makes the jump table.
	public static void makeNext() {
	
		next = new int[26][str.length];
		for (int i=0; i<26; i++) Arrays.fill(next[i], -1);
		
		// Stores last occurrence of each character.
		int[] last = new int[26];
		Arrays.fill(last, -1);
		
		// Go through each letter.
		for (int i=0; i<str.length; i++) {
			
			// Get letter mark first.
			int c = str[i]-'a';
			if (first[c] == -1) first[c] = i;	

			// For every character between occurences of char c, index i is the next time c occurs.
			for (int z=Math.max(0, last[c]); z<i; z++)
				next[c][z] = i;
			
			// Update last.
			last[str[i]-'a'] = i;
		}
	}
	
	// Returns the length of the shortest missing subsequence.
	public static int getShortest() {
	
		int i=0, res = 1;
		
		// Go till end of string.
		while (i < str.length) {
		
			int unique = 0;
			boolean[] used = new boolean[26];
			
			// Will break out when we see each unique letter once.
			while (i < str.length) {
			
				// New letter, mark it.
				if (!used[str[i]-'a']) {
					used[str[i]-'a'] = true;
					unique++;
				}
				
				// Go to next.
				i++;
				
				// See if we're done.
				if (unique == charSize) {
					res++;
					break;
				}
			}
		}
		
		// Length of shortest missing.
		return res;
	}
}