// #include<bits/stdc++.h>

// using namespace std;

// int main(){
    
//     ios::sync_with_stdio(false);
//     cin.tie(NULL);
    
//     string j,s;
//     int t;
//     cin>>t;
    
//     while(t--){
        
//         cin>>j>>s;
        
//         unordered_set<char> jewel;
        
//         for(char c : j){
//             jewel.insert(c);
//         }
        
//         int cnt=0;
//         for(char c : s){
//             // if(jewel.count(c)){
//             //     cnt++;
//             // }

//             //OR

//             if(jewel.find(c)!=jewel.end()){
//             	cnt++;
//             }
//         }
        
//         cout<<cnt<<'\n';
//     }
// }


// OR USING ARRAY 

#include<bits/stdc++.h>

using namespace std;

int main(){
    
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    
    int t;
    cin>>t;
    
    string j,s;
    
    while(t--){
        cin>>j>>s;
        
        bool jewel[256]={false};
        
        for(char c : j){
            jewel[c]=true;
        }
        
        int cnt=0;
        for(char c : s){
            if(jewel[c]){
                cnt++;
            }
        }
        cout<<cnt<<'\n';
    }
    
    return 0;
}