#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++;
            }
        }
        
        cout<<cnt<<'\n';
    }
}