Under construction

Still being built. Things may change

Shortest Missing Subsequences

Time limit10sMemory limit1024 MB

Summary
Given a target string and queries over an alphabet, decide for each query whether it is a shortest string that is not a subsequence of the target.
Level

Hard8 of 10

Topics
String, Greedy, Dynamic programming, Hash map
Solved
No attempts yet

Problem

Given a string ss, a string tt is a subsequence of ss if tt can be obtained from ss by deleting zero or more characters of ss. Note that tt is not necessarily a substring of ss: the characters of tt need not be contiguous in ss, but they must appear in the same order as they do in ss.

For a given subset vv of the lowercase English letters from 'a' to 'z', a string uu is a missing subsequence of a string ss if uu is not a subsequence of ss, and every character of uu and every character of ss belongs to vv. A shortest missing subsequence of ss is a missing subsequence of ss whose length is the smallest among all missing subsequences of ss.

You are given a set of English letters, a target string made up of characters from that set, and a list of query strings made up of characters from that set. For each query string, determine whether it is a shortest missing subsequence of the target string.

Input

The first line contains a string vv (1≤∣v∣≤261 \le |v| \le 26) of lowercase letters in lexicographical order. Each letter appears at most once. This is the set of letters.

The second line contains a string ss (1≤∣s∣≤1061 \le |s| \le 10^6; ss contains only letters from vv). This is the target string.

The third line contains an integer nn (1≤n≤1061 \le n \le 10^6), the number of queries.

Each of the next nn lines contains a string qq (1≤∣q∣≤1061 \le |q| \le 10^6; qq contains only letters from vv). These are the query strings. The sum of the lengths of all query strings does not exceed 10610^6.

Output

Output nn lines. On each line, output 11 if the query string is a shortest missing subsequence of the target string, or 00 if it is not. The outputs must be in the order of the input queries.

Examples1

  1. Example 1

    Input
    abc
    abcccabac
    3
    cbb
    cbba
    cba
    
    Expected output
    1
    0
    0