#include <bits/stdc++.h>
using namespace std;
#define en '\n'
#define Online 1
#define yes cout << "YES" << en
#define no  cout << "NO" << en
#define mems(arr,VAL)     memset(arr,VAL,sizeof(arr))
#define ll long long
#define vll vector<ll>
#define fo(i, a, b) for(ll i = a; i < b; i++)
#define fb(i, a, b) for(ll i = a; i > b; i--)
#define fv(it,v) for(auto it=v.begin();it<v.end();it++)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define in insert
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sz(a) int(a.size())
#define pll pair<ll, ll>
#define plll pair<ll, pll>
#define vpll vector<pll>
#define vplll vector<plll>
#define lbnd lower_bound
#define ubnd upper_bound
#define bs   binary_search
#define mp map<ll,ll>
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define tle 1000000007
template <typename Arg1>
void pn(Arg1&& arg1)
{ cout<<arg1<<"\n";}
template <typename Arg1, typename... Args>
void pn(Arg1&& arg1, Args&&... args)
{ cout<<arg1<<"\n"; pn(args...); }

template <typename Arg1>
void ps(Arg1&& arg1)
{ cout<<arg1<<" ";}
template <typename Arg1, typename... Args>
void ps(Arg1&& arg1, Args&&... args)
{ cout<<arg1<<" "; ps(args...); }

template <typename Arg1>
void pvs(Arg1&& arg1, ll n)
{ for(ll i=0;i<n;i++)ps(arg1[i]); }
template <typename Arg1>
void pvn(Arg1&& arg1, ll n)
{ for(ll i=0;i<n;i++)pn(arg1[i]); }

template <typename Arg1>
void bug(Arg1&& arg1)
{ cout<<"->"<<arg1<<endl; }
void solve(){
      ll y,z,n,p,x,k,l,sum=0,flag=0,t,ans=0;
      string s,s1="",s2;
      cin>>n;
      vll a(n);
      fo(i,0,n)
      cin>>a[i];
      unordered_map<ll,ll>m;
      fo(i,0,n-3){
            m.clear();
            m[a[i+1]]++,m[a[i+2]]++;
            fo(j,i+3,n){
                  if(a[i]==a[j] && m[a[j]]>1){
                        sum=m[a[j]];
                        ans+=(sum*(sum-1))/2;
                  }
                  if(m[a[i]]>0 && a[i]!=a[j])
                  ans+=m[a[j]];
                  m[a[j]]++;
                  // pn(ans,j);
            }
      }
      pn(ans);

}
int main()
{
      // #ifdef Online
      // freopen("input.txt", "r", stdin);
      // freopen("output.txt", "w", stdout);
      // #endif
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);
      int test=1,i=1;
      cin>>test;
      while(test--){
            // cout<<"Case #"<<i++<<": ";
            solve();
      }

      return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>

#define ll 			long long int
#define pb          push_back
#define	endl		'\n'
#define pii         pair<int,int>
#define vi          vector<int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (int)x.size()
#define hell        1000000007
#define rep(i,a,b)	for(int i=a;i<b;i++)
#define dep(i,a,b)	for(int i=a;i>=b;i--)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
using namespace std;

#define N  105

void solve()
{
	ll n;
	cin>>n;
	ll a[n];
	rep(i,0,n)
	cin>>a[i];
	set<ll> sp;
	vector<ll> v[n+1];
	rep(i,0,n)
	{
		sp.insert(a[i]);
		v[a[i]].pb(i);
	}
	vector<int> vp;
	for(auto x: sp)
	{
		vp.pb(x);

	}
	//cout<<vp[0]<<endl;
	ll ans=0;
	ll sz1=vp.size();
	for(int i=0;i<sz1;i++)
	{
		vector<ll> s =v[vp[i]];
		ll temp=0;
		ll tsz=s.size();
		if(tsz>=4)
		{
			temp=((tsz)*(tsz-1)*(tsz-2)*(tsz-3))/24;
			ans+=temp;
			//cout<<temp<<endl;
		}
		//cout<<vp[i]<<endl;
		for(int j=i+1;j<sz1;j++)
		{
			vector<ll> p=v[vp[j]];

			//cout<<vp[j]<<" esf"<<endl;
			ll top=s.size();
			for(int k=1;k<top;k++)
			{
				for(int l=0;l<k;l++)
				{
					int ind1=lower_bound(p.begin(),p.end(),s[l])-p.begin();
					int ind2=lower_bound(p.begin(),p.end(),s[k])-p.begin();
					ind2--;
				//	if(vp[j]==3&&vp[i]==1)
				//	cout<<ind1<<" "<<ind2<<endl;
					ll sz=p.size();
					if(ind2!=-1)
					{
						ans+=(ind2-ind1+1)*(sz-ind2-1);
					}

				}
			}

		}
	}
	cout<<ans<<endl;

}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int TESTS=1;
	#ifndef ONLINE_JUDGE
    	// for getting input from input.txt
    	freopen("input.txt", "r", stdin);
    	// for writing output to output.txt
    	freopen("output.txt", "w", stdout);
	#endif

   cin>>TESTS;
	int p=1;
	while(TESTS--)
	{
		//cout<<"CASE #"<<p<<":"<<" ";
		solve();
		p++;

	}
	return 0;
}


#include<bits/stdc++.h>

#define ll 			long long int
#define pb          push_back
#define	endl		'\n'
#define pii         pair<int,int>
#define vi          vector<int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (int)x.size()
#define hell        1000000007
#define rep(i,a,b)	for(int i=a;i<b;i++)
#define dep(i,a,b)	for(int i=a;i>=b;i--)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
using namespace std;

#define N  105

void solve()
{
	ll n;
	cin>>n;
	ll a[n];
	rep(i,0,n)
	cin>>a[i];
	unordered_set<ll> sp;
	vector<ll> v[n+1];
	rep(i,0,n)
	{
		sp.insert(a[i]);
		v[a[i]].pb(i);
	}
	vector<int> vp;
	for(auto x: sp)
	{
		vp.pb(x);

	}
	//cout<<vp[0]<<endl;
	ll ans=0;
	ll sz1=vp.size();
	for(int i=0;i<sz1;i++)
	{
		vector<ll> s =v[vp[i]];
		ll temp=0;
		ll tsz=s.size();
		if(tsz>=4)
		{
			temp=((tsz)*(tsz-1)*(tsz-2)*(tsz-3))/24;
			ans+=temp;
			//cout<<temp<<endl;
		}
		//cout<<vp[i]<<endl;
		for(int j=i+1;j<sz1;j++)
		{
			vector<ll> p=v[vp[j]];

			//cout<<vp[j]<<" esf"<<endl;
			ll top=s.size();
			for(int k=1;k<top;k++)
			{
				for(int l=0;l<k;l++)
				{
					int ind1=lower_bound(p.begin(),p.end(),s[l])-p.begin();
					int ind2=lower_bound(p.begin(),p.end(),s[k])-p.begin();
					ind2--;
				//	if(vp[j]==3&&vp[i]==1)
				//	cout<<ind1<<" "<<ind2<<endl;
					ll sz=p.size();
					if(ind2!=-1)
					{
						ans=ans+(ind2-ind1+1)*(sz-ind2-1)+((ind2-ind1+1)*(ind1));
					}

				}
			}

		}
	}
	cout<<ans<<endl;

}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int TESTS=1;
	#ifndef ONLINE_JUDGE
    	// for getting input from input.txt
    	freopen("input.txt", "r", stdin);
    	// for writing output to output.txt
    	freopen("output.txt", "w", stdout);
	#endif

   cin>>TESTS;
	int p=1;
	while(TESTS--)
	{
		//cout<<"CASE #"<<p<<":"<<" ";
		solve();
		p++;

	}
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>

#define ll 			long long int
#define pb          push_back
#define	endl		'\n'
#define pii         pair<int,int>
#define vi          vector<int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (int)x.size()
#define hell        1000000007
#define rep(i,a,b)	for(int i=a;i<b;i++)
#define dep(i,a,b)	for(int i=a;i>=b;i--)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
using namespace std;

#define N  105

void solve()
{
	ll n;
	cin>>n;
	ll a[n];
	rep(i,0,n)
	cin>>a[i];
	set<ll> sp;
	vector<ll> v[n+1];
	rep(i,0,n)
	{
		sp.insert(a[i]);
		v[a[i]].pb(i);
	}
	vector<int> vp;
	for(auto x: sp)
	{
		vp.pb(x);

	}
	//cout<<vp[0]<<endl;
	ll ans=0;
	ll sz1=vp.size();
	for(int i=0;i<sz1;i++)
	{
		vector<ll> s =v[vp[i]];
		ll temp=0;
		ll tsz=s.size();
		if(tsz>=4)
		{
			temp=((tsz)*(tsz-1)*(tsz-2)*(tsz-3))/24;
			ans+=temp;
		}
		//cout<<vp[i]<<endl;
		for(int j=i+1;j<sz1;j++)
		{
			vector<ll> p=v[vp[j]];

			//cout<<vp[j]<<" esf"<<endl;
			ll top=s.size();
			for(int k=1;k<top;k++)
			{
				//for(int l=k+1;l<s.size();l++)
				{
					int ind1=lower_bound(p.begin(),p.end(),s[k-1])-p.begin();
					int ind2=lower_bound(p.begin(),p.end(),s[k])-p.begin();
					ind2--;
				//	if(vp[j]==3&&vp[i]==1)
				//	cout<<ind1<<" "<<ind2<<endl;
					ll sz=p.size();
					if(ind2!=-1)
					{
						ans+=(ind2-ind1+1)*(sz-ind2-1);
					}

				}
			}

		}
	}
	cout<<ans<<endl;

}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int TESTS=1;
	#ifndef ONLINE_JUDGE
    	// for getting input from input.txt
    	freopen("input.txt", "r", stdin);
    	// for writing output to output.txt
    	freopen("output.txt", "w", stdout);
	#endif

   cin>>TESTS;
	int p=1;
	while(TESTS--)
	{
		//cout<<"CASE #"<<p<<":"<<" ";
		solve();
		p++;

	}
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>

#define ll 			long long int
#define pb          push_back
#define	endl		'\n'
#define pii         pair<int,int>
#define vi          vector<int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (int)x.size()
#define hell        1000000007
#define rep(i,a,b)	for(int i=a;i<b;i++)
#define dep(i,a,b)	for(int i=a;i>=b;i--)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
using namespace std;

#define N  105

void solve()
{
	ll n;
	cin>>n;
	ll a[n];
	rep(i,0,n)
	cin>>a[i];
	set<ll> sp;
	vector<ll> v[n+1];
	rep(i,0,n)
	{
		sp.insert(a[i]);
		v[a[i]].pb(i);
	}
	vector<int> vp;
	for(auto x: sp)
	{
		vp.pb(x);

	}
	//cout<<vp[0]<<endl;
	ll ans=0;
	ll sz1=vp.size();
	for(int i=0;i<sz1;i++)
	{
		vector<ll> s =v[vp[i]];
		ll temp=0;
		ll tsz=s.size();
		if(tsz>=4)
		{
			temp=((tsz)*(tsz-1)*(tsz-2)*(tsz-3))/24;
			ans+=temp;
		}
		//cout<<vp[i]<<endl;
		for(int j=i+1;j<sz1;j++)
		{
			vector<ll> p=v[vp[j]];

			//cout<<vp[j]<<" esf"<<endl;
			for(int k=1;k<s.size();k++)
			{
				//for(int l=k+1;l<s.size();l++)
				{
					int ind1=lower_bound(p.begin(),p.end(),s[k-1])-p.begin();
					int ind2=lower_bound(p.begin(),p.end(),s[k])-p.begin();
					ind2--;
				//	if(vp[j]==3&&vp[i]==1)
				//	cout<<ind1<<" "<<ind2<<endl;
					ll sz=p.size();
					if(ind2!=-1)
					{
						ans+=(ind2-ind1+1)*(sz-ind2-1);
					}

				}
			}

		}
	}
	cout<<ans<<endl;

}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int TESTS=1;
	#ifndef ONLINE_JUDGE
    	// for getting input from input.txt
    	freopen("input.txt", "r", stdin);
    	// for writing output to output.txt
    	freopen("output.txt", "w", stdout);
	#endif

   cin>>TESTS;
	int p=1;
	while(TESTS--)
	{
		//cout<<"CASE #"<<p<<":"<<" ";
		solve();
		p++;

	}
	return 0;
}


#include<bits/stdc++.h>

#define ll 			long long int
#define pb          push_back
#define	endl		'\n'
#define pii         pair<int,int>
#define vi          vector<int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (int)x.size()
#define hell        1000000007
#define rep(i,a,b)	for(int i=a;i<b;i++)
#define dep(i,a,b)	for(int i=a;i>=b;i--)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
using namespace std;

#define N  105

void solve()
{
	ll n;
	cin>>n;
	ll a[n];
	rep(i,0,n)
	cin>>a[i];
	unordered_set<ll> sp;
	vector<ll> v[n+1];
	rep(i,0,n)
	{
		sp.insert(a[i]);
		v[a[i]].pb(i);
	}
	vector<int> vp;
	for(auto x: sp)
	{
		vp.pb(x);

	}
	//cout<<vp[0]<<endl;
	ll ans=0;
	ll sz1=vp.size();
	for(int i=0;i<sz1;i++)
	{
		vector<ll> s =v[vp[i]];
		ll temp=0;
		ll tsz=s.size();
		if(tsz>=4)
		{
			temp=((tsz)*(tsz-1)*(tsz-2)*(tsz-3))/24;
			ans+=temp;
			//cout<<temp<<endl;
		}
		//cout<<vp[i]<<endl;
		for(int j=i+1;j<sz1;j++)
		{
			vector<ll> p=v[vp[j]];

			//cout<<vp[j]<<" esf"<<endl;
			vector<ll> kuch;

			ll top=s.size();
			for(int k=0;k<top;k++)
			{
				int ind2=lower_bound(p.begin(),p.end(),s[k])-p.begin();
				kuch.pb(ind2);

			}
			for(int k=1;k<top;k++)
			{
				for(int l=0;l<k;l++)
				{
					//int ind1=lower_bound(p.begin(),p.end(),s[l])-p.begin();
					int ind1=kuch[l];
					int ind2=kuch[k];
					ind2--;
				//	if(vp[j]==3&&vp[i]==1)
				//	cout<<ind1<<" "<<ind2<<endl;
					ll sz=p.size();
					if(ind2!=-1)
					{
						ans=ans+(ind2-ind1+1)*(sz-ind2-1)+((ind2-ind1+1)*(ind1));
					}

				}
			}

		}
	}
	cout<<ans<<endl;

}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int TESTS=1;
	#ifndef ONLINE_JUDGE
    	// for getting input from input.txt
    	freopen("input.txt", "r", stdin);
    	// for writing output to output.txt
    	freopen("output.txt", "w", stdout);
	#endif

   cin>>TESTS;
	int p=1;
	while(TESTS--)
	{
		//cout<<"CASE #"<<p<<":"<<" ";
		solve();
		p++;

	}
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>

#define ll 			long long int
#define pb          push_back
#define	endl		'\n'
#define pii         pair<int,int>
#define vi          vector<int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (int)x.size()
#define hell        1000000007
#define rep(i,a,b)	for(int i=a;i<b;i++)
#define dep(i,a,b)	for(int i=a;i>=b;i--)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
using namespace std;

#define N  105

void solve()
{
	ll n;
	cin>>n;
	ll a[n];
	rep(i,0,n)
	cin>>a[i];
	set<ll> sp;
	vector<ll> v[n+1];
	rep(i,0,n)
	{
		sp.insert(a[i]);
		v[a[i]].pb(i);
	}
	vector<int> vp;
	for(auto x: sp)
	{
		vp.pb(x);

	}
	//cout<<vp[0]<<endl;
	ll ans=0;
	ll sz1=vp.size();
	for(int i=0;i<sz1;i++)
	{
		vector<ll> s =v[vp[i]];
		ll temp=0;
		ll tsz=s.size();
		if(tsz>=4)
		{
			temp=((tsz)*(tsz-1)*(tsz-2)*(tsz-3))/24;
			ans=temp;
		}
		//cout<<vp[i]<<endl;
		for(int j=i+1;j<sz1;j++)
		{
			vector<ll> p=v[vp[j]];

			//cout<<vp[j]<<" esf"<<endl;
			for(int k=1;k<s.size();k++)
			{
				//for(int l=k+1;l<s.size();l++)
				{
					int ind1=lower_bound(p.begin(),p.end(),s[k-1])-p.begin();
					int ind2=lower_bound(p.begin(),p.end(),s[k])-p.begin();
					ind2--;
				//	if(vp[j]==3&&vp[i]==1)
				//	cout<<ind1<<" "<<ind2<<endl;
					ll sz=p.size();
					if(ind2!=-1)
					{
						ans+=(ind2-ind1+1)*(sz-ind2-1);
					}

				}
			}

		}
	}
	cout<<ans<<endl;

}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int TESTS=1;
	#ifndef ONLINE_JUDGE
    	// for getting input from input.txt
    	freopen("input.txt", "r", stdin);
    	// for writing output to output.txt
    	freopen("output.txt", "w", stdout);
	#endif

   cin>>TESTS;
	int p=1;
	while(TESTS--)
	{
		//cout<<"CASE #"<<p<<":"<<" ";
		solve();
		p++;

	}
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>

#define ll 			long long int
#define pb          push_back
#define	endl		'\n'
#define pii         pair<int,int>
#define vi          vector<int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (int)x.size()
#define hell        1000000007
#define rep(i,a,b)	for(int i=a;i<b;i++)
#define dep(i,a,b)	for(int i=a;i>=b;i--)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
using namespace std;

#define N  105

void solve()
{
	ll n;
	cin>>n;
	ll a[n];
	rep(i,0,n)
	cin>>a[i];
	set<ll> sp;
	vector<ll> v[n+1];
	rep(i,0,n)
	{
		sp.insert(a[i]);
		v[a[i]].pb(i);
	}
	vector<int> vp;
	for(auto x: sp)
	{
		vp.pb(x);

	}
	//cout<<vp[0]<<endl;
	ll ans=0;
	ll sz1=vp.size();
	for(int i=0;i<sz1;i++)
	{
		vector<ll> s =v[vp[i]];
		ll temp=0;
		ll tsz=s.size();
		if(tsz>=4)
		{
			temp=((tsz)*(tsz-1)*(tsz-2)*(tsz-3))/24;
			ans+=temp;
			//cout<<temp<<endl;
		}
		//cout<<vp[i]<<endl;
		for(int j=i+1;j<sz1;j++)
		{
			vector<ll> p=v[vp[j]];

			//cout<<vp[j]<<" esf"<<endl;
			ll top=s.size();
			for(int k=1;k<top;k++)
			{
				for(int l=0;l<k;l++)
				{
					int ind1=lower_bound(p.begin(),p.end(),s[l])-p.begin();
					int ind2=lower_bound(p.begin(),p.end(),s[k])-p.begin();
					ind2--;
				//	if(vp[j]==3&&vp[i]==1)
				//	cout<<ind1<<" "<<ind2<<endl;
					ll sz=p.size();
					if(ind2!=-1)
					{
						ans=ans+(ind2-ind1+1)*(sz-ind2-1)+((ind2-ind1+1)*(ind1));
					}

				}
			}

		}
	}
	cout<<ans<<endl;

}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int TESTS=1;
	#ifndef ONLINE_JUDGE
    	// for getting input from input.txt
    	freopen("input.txt", "r", stdin);
    	// for writing output to output.txt
    	freopen("output.txt", "w", stdout);
	#endif

   cin>>TESTS;
	int p=1;
	while(TESTS--)
	{
		//cout<<"CASE #"<<p<<":"<<" ";
		solve();
		p++;

	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back

ll nc4(int n)
{
	if(n<3) return 0;
	else
	{
		return (1LL*n*(n-1)*(n-2)*(n-3))/24 ;
	}
}
int find(int i,int j, vector<int> &v)
{
	int befi = lower_bound(v.begin(),v.end(),i)-v.begin();
	int befj = lower_bound(v.begin(),v.end(),j)-v.begin();
	int betw = befj-befi;
	int aftj = v.size() - befj;
	// cout<<i<<" "<<j<<" "<<befi <<" "<<befj <<" "<<betw <<" "<<aftj<<endl;
	return befi*betw + betw*aftj;
}
int main()
{
	int t,i,n;
	cin>>t;
	vector<int> a(3005);
	while(t--)
	{
		cin>>n;

		vector< vector<int> > vec(n+1);
		vector<int> fill;
		for(int i=0;i<n;i++) {
			cin>>a[i];
			vec[a[i]].pb(i);
		}
		for(int i=1;i<=n;i++)
		{
			if(vec[i].size()>1)
				fill.pb(i);
		}

		ll  ans = 0;
		for(int i=0;i<fill.size();i++)
		{
			int idx = fill[i];
			ans= 1LL*ans+nc4(vec[idx].size());
			for(int j=i+1;j<fill.size();j++)
			{
				int idx2 = fill[j];
				for(int ii=0; ii<vec[idx].size() ;ii++)
				{
					for(int jj =ii+1;jj<vec[idx].size();jj++)
					{
						ans += 1LL*find(vec[idx][ii],vec[idx][jj],vec[idx2]);
					}
				}
			}
		}
		cout<<ans<<endl;

	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back

ll nc4(int n)
{
	if(n<3) return 0;
	else
	{
		return (1LL*n*(n-1)*(n-2)*(n-3))/24 ;
	}
}
ll find(vector<int>&v1, vector<int> &v2)
{
	int n= v1.size();
	int m = v2.size(),i,j,ii,jj;
	ii=jj=0;
	ll ans = 0;
	for(i=0;i<n;i++)
	{
		for(j=i+1;j<n;j++)
		{
			while(ii<m && v2[ii]<v1[i])
				ii++;
			while(jj<m && v2[jj]<v1[j])
				jj++;
			ans+= 1LL*(ii + m -jj )*(jj-ii);
		}
	}
	return ans;
}
int main()
{
	int t,i,n;
	cin>>t;
	vector<int> a(3005);
	while(t--)
	{
		cin>>n;

		vector< vector<int> > vec(n+1);
		vector<int> fill;
		for(int i=0;i<n;i++) {
			cin>>a[i];
			vec[a[i]].pb(i);
		}
		for(int i=1;i<=n;i++)
		{
			if(vec[i].size()>1)
				fill.pb(i);
		}

		ll  ans = 0;
		for(int i=0;i<fill.size();i++)
		{
			int idx = fill[i];
			ans= 1LL*ans+nc4(vec[idx].size());
			for(int j=i+1;j<fill.size();j++)
			{
				int idx2 = fill[j];
				ans += 1LL*find(vec[idx],vec[idx2]);
			}
		}
		cout<<ans<<endl;

	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back

ll nc4(int n)
{
	if(n<3) return 0;
	else
	{
		return (1LL*n*(n-1)*(n-2)*(n-3))/24 ;
	}
}
ll find(vector<int>&v1, vector<int> &v2)
{
	int n= v1.size();
	int m = v2.size(),i,j,ii,jj;
	ii=jj=0;
	ll ans = 0;
	for(i=0;i<n;i++)
	{
		while(ii<m && v2[ii]<v1[i])
			ii++;
		jj=ii;
		for(j=i+1;j<n;j++)
		{
			while(jj<m && v2[jj]<v1[j])
				jj++;
			ans+= 1LL*(ii + m -jj )*(jj-ii);
		}
	}
	return ans;
}
int main()
{
	int t,i,n;
	cin>>t;
	vector<int> a(3005);
	while(t--)
	{
		cin>>n;

		vector< vector<int> > vec(n+1);
		vector<int> fill;
		for(int i=0;i<n;i++) {
			cin>>a[i];
			vec[a[i]].pb(i);
		}
		for(int i=1;i<=n;i++)
		{
			if(vec[i].size()>1)
				fill.pb(i);
		}

		ll  ans = 0;
		for(int i=0;i<fill.size();i++)
		{
			int idx = fill[i];
			ans= 1LL*ans+nc4(vec[idx].size());
			for(int j=i+1;j<fill.size();j++)
			{
				int idx2 = fill[j];
				ans += 1LL*find(vec[idx],vec[idx2]);
			}
		}
		cout<<ans<<endl;

	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back

ll nc4(int n)
{
	if(n<3) return 0;
	else
	{
		return (1LL*n*(n-1)*(n-2)*(n-3))/24 ;
	}
}
int find(vector<int>&v1, vector<int> &v2)
{
	int n= v1.size();
	int m = v2.size(),i,j,ii,jj;
	ii=jj=0;
	int ans = 0;
	for(i=0;i<n;i++)
	{
		for(j=i+1;j<n;j++)
		{
			while(ii<m && v2[ii]<v1[i])
				ii++;
			while(jj<m && v2[jj]<v1[j])
				jj++;
			ans+= (ii + m -jj )*(jj-ii);
		}
	}
	return ans;
}
int main()
{
	int t,i,n;
	cin>>t;
	vector<int> a(3005);
	while(t--)
	{
		cin>>n;

		vector< vector<int> > vec(n+1);
		vector<int> fill;
		for(int i=0;i<n;i++) {
			cin>>a[i];
			vec[a[i]].pb(i);
		}
		for(int i=1;i<=n;i++)
		{
			if(vec[i].size()>1)
				fill.pb(i);
		}

		ll  ans = 0;
		for(int i=0;i<fill.size();i++)
		{
			int idx = fill[i];
			ans= 1LL*ans+nc4(vec[idx].size());
			for(int j=i+1;j<fill.size();j++)
			{
				int idx2 = fill[j];
				ans += 1LL*find(vec[idx],vec[idx2]);
			}
		}
		cout<<ans<<endl;

	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back

ll nc4(int n)
{
	if(n<3) return 0;
	else
	{
		return (n*(n-1)*(n-2)*(n-3))/24 ;
	}
}
int find(int i,int j, vector<int> &v)
{
	int befi = lower_bound(v.begin(),v.end(),i)-v.begin();
	int befj = lower_bound(v.begin(),v.end(),j)-v.begin();
	int betw = befj-befi;
	int aftj = v.size() - befj;
	// cout<<i<<" "<<j<<" "<<befi <<" "<<befj <<" "<<betw <<" "<<aftj<<endl;
	return befi*betw + betw*aftj;
}
int main()
{
	int t,i,n;
	cin>>t;
	vector<int> a(3005);
	while(t--)
	{
		cin>>n;

		vector< vector<int> > vec(n+1);
		vector<int> fill;
		for(int i=0;i<n;i++) {
			cin>>a[i];
			vec[a[i]].pb(i);
		}
		for(int i=1;i<=n;i++)
		{
			if(vec[i].size()>1)
				fill.pb(i);
		}

		ll  ans = 0;
		for(int i=0;i<fill.size();i++)
		{
			int idx = fill[i];
			ans= 1LL*ans+nc4(vec[idx].size());
			for(int j=i+1;j<fill.size();j++)
			{
				int idx2 = fill[j];
				for(int ii=0; ii<vec[idx].size() ;ii++)
				{
					for(int jj =ii+1;jj<vec[idx].size();jj++)
					{
						ans += 1LL*find(vec[idx][ii],vec[idx][jj],vec[idx2]);
					}
				}
			}
		}
		cout<<ans<<endl;

	}
}


#include <bits/stdc++.h>
using namespace std;
long long sum[9000010];
long long a[3010];
int n;
int main(){
	int t;
	cin>>t;
	while(t--){
//		memset(sum,0,sizeof(sum));
		cin>>n;
		for(int i=0;i<=n*n;i++)sum[i]=0;
		for(int i=0;i<n;i++){
			cin>>a[i];
		}
		long long ans=0;
		for(int k=n-1;k>=0;k--){
			for(int l=k+1;l<n;l++){
				sum[a[k]*n+a[l]]++;
			}
			for(int i=0;i<k-1;i++){
				ans+=sum[a[i]*n+a[k-1]];
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
void reverse(string& str)
{
    int n = str.length();

    // Swap character starting from two
    // corners
    for (int i = 0; i < n / 2; i++)
        swap(str[i], str[n - i - 1]);
}
int main(){
    string s;
    cin>>s;
    int n=s.length();
    int k;
    cin>>k;
    map<char,char>m;
    vector<string>v(2*k);
    for(int i=0;i<k;i++){
        cin>>v[i];
        m[v[i][0]]=v[i][1];
        m[v[i][1]]=v[i][0];
    }
    for(int i=k;i<2*k;i++){
        string a=v[i-k];
        reverse(a);
        v[i]=a;

    }
    int i=0;
    int cnt=0;
    int ans=0;
    while(i<n){
        int f=0;
        int j=i;
        char h=s[j];
        //vector<int>f(26,0);
       int x=0;
       int y=0;
        while(j<n && (s[j]==h|| s[j]==m[h])){
            if(s[j]==h){
                x++;
            }
            else{y++;}
           // cout<<s[j]<<" "<<x<<" "<<y<<"\n";
            j++;

        }
       // cout<<x<<" "<<y<<"\n";
        ans+=min(x,y);
        i=j;
    }
    cout<<ans<<"\n";
}


#include<bits/stdc++.h>
using namespace std;
void reverse(string& str)
{
    int n = str.length();

    // Swap character starting from two
    // corners
    for (int i = 0; i < n / 2; i++)
        swap(str[i], str[n - i - 1]);
}
int main(){
    string s;
    cin>>s;
    int n=s.length();
    int k;
    cin>>k;
    vector<string>v(2*k);
    for(int i=0;i<k;i++){
        cin>>v[i];
    }
    for(int i=k;i<2*k;i++){
        string a=v[i-k];
        reverse(a);
        v[i]=a;

    }
    int i=1;
    int cnt=0;
    while(i<n){
        int f=0;
        for(int j=0;j<2*k;j++){
            if(s[i]==v[j][1] && s[i-1]==v[j][0]){
                f=1;
                break;
            }
        }
        if(f){
            cnt++;
            i=i+2;
        }
        else{
            i=i+1;
        }
    }
    cout<<cnt<<"\n";
}


#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
int main(){
  ios_base::sync_with_stdio(false);
      cin.tie(0);
      cout.tie(0);
      #ifndef ONLINE_JUDGE
      freopen("input.txt","r",stdin);
      freopen("output.txt","w",stdout);
      #endif
    string s;
    cin>>s;
    int k;cin>>k;
    int n=s.length();
    int a[26]={0};
    int b[26]={0};
    for(int i=0;i<k;i++)
    {
      char c1,c2;
      cin>>c1>>c2;

      a[c1-'a']=c2-'a';
      a[c2-'a']=c1-'a';
      b[c1-'a']=1;
      b[c2-'a']=1;
    }
    int ans=0;
    for(int i=0;i<n;i++)
    {
      if(b[s[i]-'a'])
      {

        int j=i;

        int Len1=0;
        int Len2=0;
        while(j<n && (s[j]==s[i] || (s[j]==a[s[i]-'a']+'a')))
        {
          if(s[j]==s[i])Len1++;
          if(s[j]==a[s[i]-'a']+'a')Len2++;
          j++;
        }
        i=j-1;

        ans+=min(Len1,Len2);

      }
    }
    cout<<ans<<endl;

}


#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
int main(){
  ios_base::sync_with_stdio(false);
      cin.tie(0);
      cout.tie(0);
      #ifndef ONLINE_JUDGE
      freopen("input.txt","r",stdin);
      freopen("output.txt","w",stdout);
      #endif
    string s;
    cin>>s;
    int k;cin>>k;
    int n=s.length();
    int a[26]={0};
    for(int i=0;i<k;i++)
    {
      char c1,c2;
      cin>>c1>>c2;
      a[c1-'a']=c2-'a';
      a[c2-'a']=c1-'a';
    }
    int ans=0;
    for(int i=0;i<n;i++)
    {
      if(a[s[i]-'a'])
      {

        int j=i;
        int Len1=0;
        int Len2=0;
        while(j<n && (s[j]==s[i] || (s[j]==a[s[i]-'a']+'a')))
        {
          if(s[j]==s[i])Len1++;
          if(s[j]==a[s[i]-'a']+'a')Len2++;
          j++;
        }
        i=j-1;

        ans+=min(Len1,Len2);

      }
    }
    cout<<ans<<endl;

}


#include<stdio.h>
#include<bits/stdc++.h>
#include<cmath>
using namespace std;
#define carpe_diem ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long
#define endl '\n'
#define S second
#define F first
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define M 1000000007
#define pcc pair<char,char>

int main()
{
    carpe_diem
    string s; cin>>s;
    int n=s.size();
    int k; cin>>k;
    set<pcc> ust;
    for(int i=0; i<k; i++)
    {
        string temp; cin>>temp;
        ust.insert(mp(temp[0], temp[1]));
        ust.insert(mp(temp[1], temp[0]));
    }
    vector<pair<char, int>> vc;
    int i=0;
    while(i<n)
    {
        int c=1;
        while(i+1<n && s[i+1]==s[i])
        {
            i++;
            c++;
        }
        vc.pb( mp(s[i], c) );
        i++;
    }
    int ans=0;
    i=0;
    while(i<vc.size()-1)
    {
        if ( ust.find( mp(vc[i].F, vc[i+1].F) ) != ust.end() )
        {
            int a1=vc[i].S;
            int a2=vc[i+1].S;
            int j=i+2;
            while(j<n && vc[j].F==vc[j-2].F)
            {
                if(i%2==j%2)
                {
                    a1+=vc[j].S;
                }
                else
                {
                    a2+=vc[j].S;
                }
                j++;
            }
            i=j;
            ans+=min(a1,a2);
        }
        else {i++;}
    }
    cout<<ans<<endl;
}


#include<stdio.h>
#include<bits/stdc++.h>
#include<cmath>
using namespace std;
#define carpe_diem ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long
#define endl '\n'
#define S second
#define F first
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define M 1000000007
#define pcc pair<char,char>

string dp[7]={"1869","6198","1896","9186","9168","6189","8691"};

int main()
{
    carpe_diem
    string s; cin>>s;
    int n=s.size();
    int k; cin>>k;
    set<pcc> ust;
    for(int i=0; i<k; i++)
    {
        string temp; cin>>temp;
        ust.insert(mp(temp[0], temp[1]));
        ust.insert(mp(temp[1], temp[0]));
    }
    int i=0;
    int ans=0;
    while(i+1<n)
    {
        if( ust.find(mp(s[i],s[i+1])) !=ust.end() )
        {
            ans++; i+=2;
        }
        else {i+=1;}
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define lli long long int

int a[50];

int main(){
	string s;
	cin>>s;
	int k,sum= 0 ;
	cin>>k;
	for(int i=0;i<k;++i){
		char x,y;
		cin>>x>>y;
		for(int j=0;j<s.length()-1;++j){
			if((s[j]==x&&s[j+1]==y)||(s[j]==y&&s[j+1]==x)){
				a[i]++;
				++j;
			}
		}
		sum+=a[i];
	}

	cout<<sum<<endl;

}


#include<bits/stdc++.h>
using namespace std;
#define lli long long int

int a[50];
char x[50],y[50];
int main(){
	string s;
	cin>>s;
	int k,sum= 0 ;
	cin>>k;

	for(int i=0;i<k;++i){
		char a,b;
		cin>>a>>b;
		x[a-'a'] = b;
		x[b - 'a'] = a;
	}

	for(int i=0;i<s.length();++i){
		if(x[s[i]-'a']!=0){
			char a,b;
			a = s[i];
			b = x[s[i]-'a'];
			//cout<<a<<' '<<b<<' '<<endl;
			int flag = 1;
			int c1,c2;
			c1 = c2 = 0;
			while(i<s.length()&&(s[i]==a||s[i]==b)){
				if(s[i]==a)
					++c1;
				else
					++c2;
				++i;
				flag = 0;
			}

			if(!flag) --i;
			sum += min(c1,c2);

		}
	}

	cout<<sum<<endl;

}


# include <bits/stdc++.h>

# define pb push_back
# define sz(x) (int)((x).size())
# define whole(x) (x).begin(), (x).end()

using namespace std;

typedef long long ll;

string s;
int k;
int main(){
 cin >> s >> k;
 vector < vector < bool > > bad(28, vector < bool > (28, false));
 for (int i = 1; i <= k; ++i)
 {
  string t;
  cin >> t;
  bad[t[0] - 'a'][t[1] - 'a'] = bad[t[1] - 'a'][t[0] - 'a'] = true;
 }
 vector < int > dp(26, 0);
 int ans = 0;
 for (int i = 0; i < sz(s); ++i)
 {
  vector < int > next(26, 0);
  for (int j = 0; j < 26; ++j) next[j] = dp[j] + 1;
  for (int j = 0; j < 26; ++j)
  	if (!bad[s[i] - 'a'][j] && !bad[j][s[i] - 'a']) next[s[i] - 'a'] = min(next[s[i] - 'a'], dp[j]);
  for (int j = 0; j < 26; ++j) dp[j] = next[j];
 }
 ans = *min_element(whole(dp));
 printf ("%d\n", ans);
 return 0;
}


# include <bits/stdc++.h>

# define pb push_back
# define sz(x) (int)((x).size())
# define whole(x) (x).begin(), (x).end()

using namespace std;

typedef long long ll;

string s;
int k;
int main(){
 cin >> s >> k;
 vector < vector < bool > > bad(28, vector < bool > (28, false));
 for (int i = 1; i <= k; ++i)
 {
  string t;
  cin >> t;
  bad[t[0] - 'a'][t[1] - 'a'] = bad[t[1] - 'a'][t[0] - 'a'] = true;
 }
 vector < int > dp(26, 0);
 int ans = 0;
 for (int i = 0; i < sz(s) - 1; ++i)
 {
  vector < int > next(26, 0);
  for (int j = 0; j < 26; ++j) next[j] = dp[j] + 1;
  for (int j = 0; j < 26; ++j)
  	if (!bad[s[i] - 'a'][j] && !bad[j][s[i] - 'a']) next[s[i] - 'a'] = min(next[s[i] - 'a'], dp[j]);
  for (int j = 0; j < 26; ++j) dp[j] = next[j];
 }
 ans = *min_element(whole(dp));
 printf ("%d\n", ans);
 return 0;
}


# include <bits/stdc++.h>

# define pb push_back
# define sz(x) (int)((x).size())
# define whole(x) (x).begin(), (x).end()

using namespace std;

typedef long long ll;

string s;
int k;
int main(){
 cin >> s >> k;
 vector < vector < bool > > bad(28, vector < bool > (28, false));
 for (int i = 1; i <= k; ++i)
 {
  string t;
  cin >> t;
  bad[t[0] - 'a'][t[1] - 'a'] = bad[t[1] - 'a'][t[0] - 'a'] = true;
 }
 int ans = 0;
 for (int i = 0; i < sz(s) - 1; ++i)
 {
  if (bad[s[i] - 'a'][s[i + 1] - 'a'] || bad[s[i + 1] - 'a'][s[i] - 'a']) ++i, ++ans;
 }
 printf ("%d\n", ans);
 return 0;
}


#pragma GCC optimize("unroll-loops", "omit-frame-pointer","inline")
#pragma GCC option("arch=native","tune=native","no-zero-upper")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("Ofast")

#include<bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define f(i,a,b) for(ll i=a; i<b; i++)
#define TC ll test_case; cin>>test_case; for(ll number=1 ; number <= test_case ; number ++ )
#define vll vector<ll>
#define pb push_back
#define pll pair<ll,ll>
#define mp make_pair
#define ff first
#define ss second
#define sz(a) (ll)(a).size()
#define sp(x) cout << fixed << setprecision(x);
#define ReadFile freopen("input.txt","r",stdin)
#define WriteFile freopen("output.txt","w",stdout)
#define endl "\n"

const ld pi = acos(-1);
const ull M=LLONG_MAX,N=1e7+5,INF=1e18;

bool bs(vector<string> arr, ll l,ll r,string x)
{
    sort(x.begin(),x.end());
    ll mid;

    if (r>=l)
    {
        mid = (l+r)/2;
        if(arr[mid]==x )
            return true;
        if (x.compare(arr[mid])<0)
            return bs(arr, l, mid-1, x);
        else
            return bs(arr, mid + 1, r, x);
    }
    return false;
}

void solve()
{
    string s,tt,res="";
    ll n,l,cnt=0;
    cin >> s >> n;
    vector<string> a(n);
    vll ap(26,0);

    f(i,0,n)
    {
        cin >> a[i];
        sort(a[i].begin(),a[i].end());
    }
    sort(a.begin(),a.end());

    f(i,0,sz(a))
    {
        ap[a[i][0]-'a']++;
        ap[a[i][1]-'a']++;
    }

    f(i,1,sz(s)-1)
    {
        if(bs(a,0,sz(a)-1,s.substr(i-1,2)))
        {
            bool fi,se;
            fi=se=false;

            tt=s[i-1]+s[i+1];
            if( bs(a,0,sz(a)-1,tt) )
            {
                // cout << tt << " ";
                se=true;
            }

            tt=s[i]+s[i+1];
            if(  bs(a,0,sz(a)-1,tt) )
            {
                // cout << tt << " ";
                fi=true;
            }

            if(se && fi)
            {
                if(ap[s[i]-'a']>ap[s[i-1]-'a'])
                {
                    s.erase(i,1);
                    cnt++;
                }
                else
                {
                    s.erase(i-1,1);
                    cnt++;
                }
            }
            else if(se)
            {
                s.erase(i,1);
                cnt++;
            }
            else if(fi)
            {
                s.erase(i-1,1);
                cnt++;
            }
            else
            {
                s.erase(i,1);
                cnt++;
            }
            i--;
        }
    }
    // cout << s << endl;
    cout << cnt << endl;
}

int32_t main()
{
    FIO
    solve();
    return 0;
}


#pragma GCC optimize("unroll-loops", "omit-frame-pointer","inline")
#pragma GCC option("arch=native","tune=native","no-zero-upper")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("Ofast")

#include<bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define f(i,a,b) for(ll i=a; i<b; i++)
#define TC ll test_case; cin>>test_case; for(ll number=1 ; number <= test_case ; number ++ )
#define vll vector<ll>
#define pb push_back
#define pll pair<ll,ll>
#define mp make_pair
#define ff first
#define ss second
#define sz(a) (ll)(a).size()
#define sp(x) cout << fixed << setprecision(x);
#define ReadFile freopen("input.txt","r",stdin)
#define WriteFile freopen("output.txt","w",stdout)
#define endl "\n"

const ld pi = acos(-1);
const ull M=LLONG_MAX,N=1e7+5,INF=1e18;

bool bs(vector<string> arr, ll l,ll r,string x)
{
    sort(x.begin(),x.end());
    ll mid;

    if (r>=l)
    {
        mid = (l+r)/2;
        if(arr[mid]==x )
            return true;
        if (x.compare(arr[mid])<0)
            return bs(arr, l, mid-1, x);
        else
            return bs(arr, mid + 1, r, x);
    }
    return false;
}

void solve()
{
    string s,tt,res="";
    ll n,l,cnt=0;
    cin >> s >> n;
    vector<string> a(n);
    vll ap(26,0);

    f(i,0,n)
    {
        cin >> a[i];
        sort(a[i].begin(),a[i].end());
    }
    sort(a.begin(),a.end());

    f(i,0,sz(a))
    {
        ap[a[i][0]-'a']++;
        ap[a[i][1]-'a']++;
    }

    f(i,1,sz(s)-1)
    {
        if(bs(a,0,sz(a)-1,s.substr(i-1,2)))
        {
            bool fi,se;
            fi=se=false;

            tt=s[i-1]+s[i+1];
            if( bs(a,0,sz(a)-1,tt) )
            {
                // cout << tt << " ";
                se=true;
            }

            tt=s[i]+s[i+1];
            if(  bs(a,0,sz(a)-1,tt) )
            {
                // cout << tt << " ";
                fi=true;
            }

            if(se && fi)
            {
                if(ap[s[i]-'a']>ap[s[i-1]-'a'])
                {
                    s.erase(i,1);
                    cnt++;
                }
                else
                {
                    s.erase(i-1,1);
                    cnt++;
                }
            }
            else if(se)
            {
                s.erase(i,1);
                cnt++;
            }
            else if(fi)
            {
                s.erase(i-1,1);
                cnt++;
            }
            else
            {
                s.erase(i,1);
                cnt++;
            }
            if(se || fi)
            {
                i--;
            }
        }
    }
    // cout << s << endl;
    cout << cnt << endl;
}

int32_t main()
{
    FIO
    solve();
    return 0;
}


#pragma GCC optimize("unroll-loops", "omit-frame-pointer","inline")
#pragma GCC option("arch=native","tune=native","no-zero-upper")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("Ofast")

#include<bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define f(i,a,b) for(ll i=a; i<b; i++)
#define TC ll test_case; cin>>test_case; for(ll number=1 ; number <= test_case ; number ++ )
#define vll vector<ll>
#define pb push_back
#define pll pair<ll,ll>
#define mp make_pair
#define ff first
#define ss second
#define sz(a) (ll)(a).size()
#define sp(x) cout << fixed << setprecision(x);
#define ReadFile freopen("input.txt","r",stdin)
#define WriteFile freopen("output.txt","w",stdout)
#define endl "\n"

const ld pi = acos(-1);
const ull M=LLONG_MAX,N=1e7+5,INF=1e18;

void solve()
{
    string s;
    ll k,ans=0,a,b;
    char c1,c2;
    cin >> s >> k;

    f(i,0,k)
    {
        cin >> c1 >> c2;
        a=b=0;
        f(j,0,sz(s))
        {
            if(s[j]==c1)
            {
                a++;
            }
            else if(s[j]==c2)
            {
                b++;
            }
            else
            {
                ans+=min(a,b);
                a=b=0;
            }
        }
        ans+=min(a,b);
    }
    cout << ans << endl;
}

int32_t main()
{
    FIO
    solve();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define M 1000000007
vector <pair <ll,ll>> compress_array(string a, ll n)
{
    vector <pair <ll,ll>> ans;
    ll i=0;
    ll c=1;
    while(i<n-1)
    {
        if(a[i]!=a[i+1])
        {
            pair <ll,ll> temp;
            temp.first=a[i];
            temp.second=c;
            ans.push_back(temp);
            c=1;
        }
        else
        {
            c++;
        }
        i++;
    }
    pair <ll,ll> temp;
    temp.first=a[i];
    temp.second=c;
    ans.push_back(temp);
    c=1;
    return ans;
}
int main()
{
 cin.tie(0);
ios::sync_with_stdio(0);
    string s;
    cin>>s;
    int k;
    cin>>k;
    map <char,char> m;
    for(ll i=0;i<k;i++)
    {
        string x;
        cin>>x;
        m[x[0]]=x[1];
        m[x[1]]=x[0];
    }
    vector <pair <ll,ll>> v=compress_array(s,s.size());
    ll ans=0;
    for(int i=0;i<v.size()-1;i++)
    {
        if(m.find(v[i].first)!=m.end())
        {
            char c=v[i].first;
            ll ans1=0,ans2=0;
            while((m[v[i].first]==v[i+1].first)&&(v[i].first==c)&&(i<v.size()))
            {
                ans1+=v[i].second;
                ans2+=v[i+1].second;
                i+=2;
            }
            ans+=min(ans1,ans2);
        }
    }
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define M 1000000007
vector <pair <ll,ll>> compress_array(string a, ll n)
{
    vector <pair <ll,ll>> ans;
    ll i=0;
    ll c=1;
    while(i<n-1)
    {
        if(a[i]!=a[i+1])
        {
            pair <ll,ll> temp;
            temp.first=a[i];
            temp.second=c;
            ans.push_back(temp);
            c=1;
        }
        else
        {
            c++;
        }
        i++;
    }
    pair <ll,ll> temp;
    temp.first=a[i];
    temp.second=c;
    ans.push_back(temp);
    c=1;
    return ans;
}
int main()
{
 cin.tie(0);
ios::sync_with_stdio(0);
    string s;
    cin>>s;
    int k;
    cin>>k;
    map <char,char> m;
    for(ll i=0;i<k;i++)
    {
        string x;
        cin>>x;
        m[x[0]]=x[1];
        m[x[1]]=x[0];
    }
    vector <pair <ll,ll>> v=compress_array(s,s.size());
    ll ans=0;
    for(int i=0;i<v.size()-1;i++)
    {
        if(m.find(v[i].first)!=m.end())
        {
            char c=v[i].first;
            ll ans1=0,ans2=0;
            ll flag=0;
            while((m[v[i].first]==v[i+1].first)&&(v[i].first==c)&&(i<v.size()-1))
            {
                ans1+=v[i].second;
                ans2+=v[i+1].second;
                i+=2;
                flag=1;
            }
            if((flag)&&(i<v.size()))
            {
                if(v[i].first==c)
                {
                    ans1+=v[i].second;
                    i++;
                }
            }
            if(flag)
                i--;
            //cout<<ans1<<" "<<ans2<<" "<<s.size()<<endl;
            ans+=min(ans1,ans2);
        }
    }
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define M 1000000007
vector <pair <ll,ll>> compress_array(string a, ll n)
{
    vector <pair <ll,ll>> ans;
    ll i=0;
    ll c=1;
    while(i<n-1)
    {
        if(a[i]!=a[i+1])
        {
            pair <ll,ll> temp;
            temp.first=a[i];
            temp.second=c;
            ans.push_back(temp);
            c=1;
        }
        else
        {
            c++;
        }
        i++;
    }
    pair <ll,ll> temp;
    temp.first=a[i];
    temp.second=c;
    ans.push_back(temp);
    c=1;
    return ans;
}
int main()
{
 cin.tie(0);
ios::sync_with_stdio(0);
    string s;
    cin>>s;
    int k;
    cin>>k;
    map <char,char> m;
    for(ll i=0;i<k;i++)
    {
        string x;
        cin>>x;
        m[x[0]]=x[1];
        m[x[1]]=x[0];
    }
    vector <pair <ll,ll>> v=compress_array(s,s.size());
    ll ans=0;
    for(int i=0;i<v.size()-1;i++)
    {
        if(m.find(v[i].first)!=m.end())
        {
            char c=v[i].first;
            ll ans1=0,ans2=0;
            ll flag=0;
            while((m[v[i].first]==v[i+1].first)&&(v[i].first==c)&&(i<v.size()-1))
            {
                ans1+=v[i].second;
                ans2+=v[i+1].second;
                i+=2;
                flag=1;
            }
            if((flag)&&(i<v.size()))
            {
                if(v[i].first==c)
                {
                    ans1+=v[i].second;
                    i++;
                }
            }
            //cout<<ans1<<" "<<ans2<<" "<<s.size()<<endl;
            ans+=min(ans1,ans2);
        }
    }
    cout<<ans<<endl;
}


/*
ID: aryan_luffy
LANG: C++
TASK: ride
*/
#include <bits/stdc++.h>
#define lli long long int
#define pb push_back
#define mod 1000000007
#pragma GCC optimize ("-O2")
#define mod2 998244353
#define MAXN 1000000000
#define v32 vector<int>
#define v64 vector<lli>
#define v1024 vector <vector <int>>
#define v4096 vector <vector <lli>>
#define vt vector
#define f(x, y, z) for (lli x = y; x < z; x++)
#define fd(x, y, z) for (lli x = y; x > z; x--)
#define lb lower_bound
#define ld long double
#define m64 map<lli,lli>
#define m32 map<int,int>
#define m64it map<lli,lli>::iterator
#define m32it map<int,int>::iterator
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define ist insert
#define endl "\n"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set tree<pair<int,int>, null_type,less<pair<int,int>>, rb_tree_tag,tree_order_statistics_node_update>
#define p_q priority_queue
#define min_p_q priority_queue <pair<lli,int>,vt <pair<lli,int>>,greater <pair<lli,int>>>
using namespace std;
using namespace __gnu_pbds;

//USE AT YOUR OWN RISK
lli tmod(lli x,lli m=mod){return (x%m+m)%m;}

template <typename T>
void DEBUG_ARR(vt<T> a,char c)
{
	f(i,0,a.size())cout<<a[i]<<c;
	cout<<"\n";
}

//make sure x is below mod
lli power(lli x, lli y,lli m=mod)
{
    lli res = 1;

    while (y > 0)
    {
        if (y & 1)
            res = (res*x)%m;
        y = y>>1;
        x = (x*x)%m;
    }
    if(res<0)
       res+=m;
    return res;
}

// lli fact[2000009];
// lli ncr(lli n,lli r){
//     lli ans=fact[n];
//     if(r>n)return 1;
//     if(r<0)return 0;
//     if(n<0)return 0;
//     ans=(ans*power(fact[n-r],mod-2))%mod;
//     ans=(ans*power(fact[r],mod-2))%mod;
//     return ans;
// }
void normalize(lli &a,lli &b){
    if(a==0 && b==0)return;
    if(a<0){
        a=-a;
        b=-b;
    }
    else if(a==0){
        if(b<0)b=-b;
    }
    lli g=__gcd(abs(a),abs(b));
    if(g!=0){
        a/=g;
        b/=g;
    }
}
const int N=2e5+5;

//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
// int smallestprime[(int)1e7+10];
// int nodivisors[(int)1e7+5];

int main()
{
    // fact[0]=1;
    // f(i,1,2000001)fact[i]=(i*fact[i-1])%mod;
    // f(i,2,10000001){
    //     if(smallestprime[i]==0)
    //     for(lli j=1;j*i<=10000000;j++){
    //         if(smallestprime[j*i]==0)
    //         smallestprime[j*i]=i;
    //     }
    // }
    // nodivisors[1]=1;
    fastio;
    int t=1;
    // cin>>t;
    // int temp=t;
    // cout<<smvalallestprime[5]<<"\n";
	while(t--){
        // cout<<"Case #"<<temp-t<<": ";
        // clock_t t=clock();
        string s;cin>>s;
        int k;cin>>k;
        m32 mp;
        for(int i=0;i<k;i++){
            char a,b;cin>>a>>b;
            mp[a]=b;
            mp[b]=a;
        }
        // int dp[s.size()+1];
        // memset(dp,0,sizeof dp);
        int dp2[26];
        memset(dp2,0,sizeof dp2);
        f(i,0,s.size()){
            // int temp=0;
            int val=dp2[s[i]-'a']+1;
            f(j,0,26){
                if(j+'a'!=mp[s[i]] && s[i]-'a'!=j){
                    dp2[s[i]-'a']=max(dp2[s[i]-'a'],1+dp2[j]);
                }
            }
            dp2[s[i]-'a']=max(1,dp2[s[i]-'a']);
            dp2[s[i]-'a']=max(dp2[s[i]-'a'],val);
            // cout<<dp2[s[i]-'a']<<"\n";
        }
        int ans=0;
        f(i,0,26)ans=max(ans,dp2[i]);
        cout<<s.size()-ans<<"\n";
        // cout<<clock()-t<<"\n";
    }
    return 0;
}


/*
ID: aryan_luffy
LANG: C++
TASK: ride
*/
#include <bits/stdc++.h>
#define lli long long int
#define pb push_back
#define mod 1000000007
#pragma GCC optimize ("-O2")
#define mod2 998244353
#define MAXN 1000000000
#define v32 vector<int>
#define v64 vector<lli>
#define v1024 vector <vector <int>>
#define v4096 vector <vector <lli>>
#define vt vector
#define f(x, y, z) for (lli x = y; x < z; x++)
#define fd(x, y, z) for (lli x = y; x > z; x--)
#define lb lower_bound
#define ld long double
#define m64 map<lli,lli>
#define m32 map<int,int>
#define m64it map<lli,lli>::iterator
#define m32it map<int,int>::iterator
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define ist insert
#define endl "\n"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set tree<pair<int,int>, null_type,less<pair<int,int>>, rb_tree_tag,tree_order_statistics_node_update>
#define p_q priority_queue
#define min_p_q priority_queue <pair<lli,int>,vt <pair<lli,int>>,greater <pair<lli,int>>>
using namespace std;
using namespace __gnu_pbds;

//USE AT YOUR OWN RISK
lli tmod(lli x,lli m=mod){return (x%m+m)%m;}

template <typename T>
void DEBUG_ARR(vt<T> a,char c)
{
	f(i,0,a.size())cout<<a[i]<<c;
	cout<<"\n";
}

//make sure x is below mod
lli power(lli x, lli y,lli m=mod)
{
    lli res = 1;

    while (y > 0)
    {
        if (y & 1)
            res = (res*x)%m;
        y = y>>1;
        x = (x*x)%m;
    }
    if(res<0)
       res+=m;
    return res;
}

// lli fact[2000009];
// lli ncr(lli n,lli r){
//     lli ans=fact[n];
//     if(r>n)return 1;
//     if(r<0)return 0;
//     if(n<0)return 0;
//     ans=(ans*power(fact[n-r],mod-2))%mod;
//     ans=(ans*power(fact[r],mod-2))%mod;
//     return ans;
// }
void normalize(lli &a,lli &b){
    if(a==0 && b==0)return;
    if(a<0){
        a=-a;
        b=-b;
    }
    else if(a==0){
        if(b<0)b=-b;
    }
    lli g=__gcd(abs(a),abs(b));
    if(g!=0){
        a/=g;
        b/=g;
    }
}
const int N=2e5+5;

//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
// int smallestprime[(int)1e7+10];
// int nodivisors[(int)1e7+5];

int main()
{
    // fact[0]=1;
    // f(i,1,2000001)fact[i]=(i*fact[i-1])%mod;
    // f(i,2,10000001){
    //     if(smallestprime[i]==0)
    //     for(lli j=1;j*i<=10000000;j++){
    //         if(smallestprime[j*i]==0)
    //         smallestprime[j*i]=i;
    //     }
    // }
    // nodivisors[1]=1;
    fastio;
    int t=1;
    // cin>>t;
    // int temp=t;
    // cout<<smvalallestprime[5]<<"\n";
	while(t--){
        // cout<<"Case #"<<temp-t<<": ";
        // clock_t t=clock();
        string s;cin>>s;
        int k;cin>>k;
        m32 mp;
        for(int i=0;i<k;i++){
            char a,b;cin>>a>>b;
            mp[a]=b;
            mp[b]=a;
        }
        // int dp[s.size()+1];
        // memset(dp,0,sizeof dp);
        int dp2[26];
        memset(dp2,0,sizeof dp2);
        f(i,0,s.size()){
            // int temp=0;
            int val=dp2[s[i]-'a']+1;
            f(j,0,26){
                if(j+'a'!=mp[s[i]] && s[i]-'a'!=j){
                    dp2[s[i]-'a']=max(dp2[s[i]-'a'],1+dp2[j]);
                }
            }
            dp2[s[i]-'a']=max(1,dp2[s[i]-'a']);
            // cout<<dp2[s[i]-'a']<<"\n";
        }
        int ans=0;
        f(i,0,26)ans=max(ans,dp2[i]);
        cout<<s.size()-ans<<"\n";
        // cout<<clock()-t<<"\n";
    }
    return 0;
}


//  Save to Drive
#include<iostream>
#include <bits/stdc++.h>
#define db double
#define ll long long int

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    t=1;
    while(t--)
    {
        string s;
        cin>>s;
        ll k;

        cin>>k;
        ll ans=0;
        while(k--)
        {
            ll i=0;
            string str;
            cin>>str;
            ll c0=0;
            ll c1=0;
            ll n=s.length();
            while(i<n)
            {
                if(s[i]==str[0])
                {
                    c0++;
                }
                else if(s[i]==str[1])
                {
                    c1++;
                }
                else
                {
                    ans+=min(c1,c0);
                    c1=0;
                    c0=0;
                }
                i++;
            }
            ans+=min(c1,c0);

        }
        cout<<ans<<"\n";
    }

    return 0;
}


//  Save to Drive
#include<iostream>
#include <bits/stdc++.h>
#define db double
#define ll long long int

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    t=1;
    while(t--)
    {
        string s;
        cin>>s;
        ll k;
        cin>>k;
        ll ans=0;
        while(k--)
        {
            string str;
            cin>>str;
            ll i=0;
            ll j=1;
            ll n=s.length();
            while(i<n-1 && j<n)
            {
                if(((s[i]==str[0]) && (s[j]==str[1])) || ((s[i]==str[1]) && (s[j]==str[0])))
                {
                    ans++;
                    j++;
                }
                else
                {
                    i=j;;
                    j++;
                }
            }
        }
        cout<<ans<<"\n";
    }

    return 0;
}


#include<bits/stdc++.h>
#define pcc pair<char,char>
using namespace std;
map<pcc,int>mp ;
int main()
{
    string s,as="" ; cin>>s ;
    int k ; cin>>k ;
    for(int i = 0 ; i < k ; i++)
    {
        string ss ; cin>>ss ;
        char c1=ss[0],c2=ss[1] ;
        mp[{c1,c2}]=1 ; mp[{c2,c1}]=1 ;
    }
    as+=s[0] ;
    for(int i = 1 ; i < s.size() ; i++)
    {
        int sz=as.size()-1 ;
        if(!mp[{as[sz],s[i]}]) as+=s[i] ;
    }
    cout<<s.size()-as.size()<<endl ;
}


#include<bits/stdc++.h>
#define pcc pair<char,char>
using namespace std;
map<char,char>mp ;
int main()
{
    string s,as="" ; cin>>s ;
    int k,ans=0 ; cin>>k ;
    for(int i = 0 ; i < k ; i++)
    {
        string ss ; cin>>ss ;
        char c1=ss[0],c2=ss[1] ;
        mp[c1]=c2 ; mp[c2]=c1 ;
    }
    for(int i = 0 ; i < s.size() ; i++)
    {
        char c1=s[i],c2=mp[s[i]]; int j=0 ;
        string ss="" ;
        for(j = i ; j < s.size() ; j++)
        {
            if(s[j]==c1 || s[j]==c2)
            {
                ss+=s[j] ;
            }
            else break ;
        }
        int x=0,y=0 ;
        //cout<<ss<<endl ;
        for(char ch : ss)
        {
            if(ch==c1) x++ ; else y++ ;
        }
        ans+=min(x,y) ;
        i=j-1 ;
    }
    cout<<ans<<endl ;
}
/*
pgpgppgggpbbnnn
2
pg
nb
*/


#include<bits/stdc++.h>
#define pcc pair<char,char>
using namespace std;
map<pcc,int>mp ;
int main()
{
    string s,as="" ; cin>>s ;
    int k,ans=0 ; cin>>k ;
    for(int i = 0 ; i < k ; i++)
    {
        string ss ; cin>>ss ;
        char c1=ss[0],c2=ss[1] ;
        mp[{c1,c2}]=1 ; mp[{c2,c1}]=1 ;
    }
    for(int i = 1 ; i < s.size() ; i++)
    {
        if(mp[{s[i-1],s[i]}])
        {
            //cout<<"i "<<i<<endl ;
            int ct=1,j=i ;
            for( ; j < s.size() ; )
            {
                //cout<<"j "<<j<<endl ;
                if(mp[{s[j],s[j-1]}]) ct++ ;
                else {i=j-1 ; break ;}
                j++ ;
            }
            if(j==s.size()) i=s.size() ;
           // cout<<ct<<endl ;
            ans+=(ct/2) ;
        }
    }
    cout<<ans<<endl ;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (ll r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
#define mp(a, b) make_pair(a, b)
const int MOD = 1000 * 1000 * 1000 + 7;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    string s, l;
    cin>>s;
    set<string>no;
    int k, n;
    cin>>k;
    n=s.size();
    rep(i,0,k){
        cin>>l;
        no.insert(l);
        reverse be(l);
        no.insert(l);
    }
    int ans=0;
    rep(i,0,n-1){
        string a=s.substr(i, 2);
        if(no.count(a)){
            s[i+1]='.';
            ans++;
        }
    }
    cout<<ans<<'\n';
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (ll r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
#define mp(a, b) make_pair(a, b)
const ll MOD = 1000 * 1000 * 1000 + 7;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    string s, e;
    vector<pair<char, ll>>A;
    cin>>s;
    ll count=1, n=s.size();
    char  x=s[0];
    rep(i,1,n){
        if(s[i]!=x){
            A.pb(mp(x, count));
            count=1;
            x=s[i];
        }else{
            count++;
        }
    }
    A.pb(mp(x, count));
    set<string>no;
    ll k;
    cin>>k;//
    rep(i,0,k){
        cin>>e;
        no.insert(e);
        reverse be(e);
        no.insert(e);
    }
    ll ans=0, l=0, r=1;
    while(r<n){
        string a="";
        a+=A[l].first;a+=A[r].first;
        if(no.count(a)){
            if(A[l].second<A[r].second){
                ans+=A[l].second;
                if(A[r].first==A[l].first){
                    A[r].second+=A[l].second;
                }
                l++, r++;
            }else{
                ans+=A[r].second;
                r++;
            }
        }else{
            if(A[r].first==A[l].first){
                A[r].second+=A[l].second;
            }
            l=r; r++;
        }
    }
    cout<<ans<<'\n';
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (ll r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
#define mp(a, b) make_pair(a, b)
const ll MOD = 1000 * 1000 * 1000 + 7;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    string s, e;
    vector<pair<char, ll>>A;
    cin>>s;
    ll count=1, n=s.size();
    char  x=s[0];
    rep(i,1,n){
        if(s[i]!=x){
            A.pb(mp(x, count));
            count=1;
            x=s[i];
        }else{
            count++;
        }
    }
    A.pb(mp(x, count));
    set<string>no;
    ll k;
    cin>>k;
    rep(i,0,k){
        cin>>e;
        no.insert(e);
        reverse be(e);
        no.insert(e);
    }
    ll ans=0, l=0, r=1;
    while(r<n){
        string a="";
        a+=A[l].first;a+=A[r].first;
        if(no.count(a)){
            if(A[l].second<A[r].second){
                ans+=A[l].second;
                if(A[r].first==A[l].first){
                    A[r].second+=A[l].second;
                }
                l++, r++;
            }else{
                ans+=A[r].second;
                r++;
            }
        }else{
            if(A[r].first==A[l].first){
                A[r].second+=A[l].second;
            }
            l=r; r++;
        }
    }
    cout<<ans<<'\n';
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (ll r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
#define mp(a, b) make_pair(a, b)
const ll MOD = 1000 * 1000 * 1000 + 7;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    string s, e;
    vector<pair<char, ll>>A;
    cin>>s;
    ll count=1, n=s.size();
    char  x=s[0];
    rep(i,1,n){
        if(s[i]!=x){
            A.pb(mp(x, count));
            count=1;
            x=s[i];
        }else{
            count++;
        }
    }
    A.pb(mp(x, count));
    set<string>no;
    ll k;
    cin>>k;
    rep(i,0,k){
        cin>>e;
        no.insert(e);
        reverse be(e);
        no.insert(e);
    }
    ll ans=0, l=0, r=1;
    while(r<n){
        if(A[r].first==A[l].first){
            A[r].second+=A[l].second;
            l=r; r++;
            continue;
        }
        string a="";
        a+=A[l].first;a+=A[r].first;
        if(no.count(a)){
            if(r!=n-1&&A[r+1].first==A[l].first){
                A[r+1].second+=A[l].second;
                l=r, r++;
                continue;//
            }
            if(A[l].second<A[r].second){
                ans+=A[l].second;
                if(A[r].first==A[l].first){
                    A[r].second+=A[l].second;
                }
                l=r, r++;
            }else{
                ans+=A[r].second;
                r++;
            }
        }else{
            l=r; r++;
        }
    }
    cout<<ans<<'\n';
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (ll r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
#define mp(a, b) make_pair(a, b)
const ll MOD = 1000 * 1000 * 1000 + 7;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    string s, e;
    vector<pair<char, ll>>A;
    cin>>s;
    ll count=1, n=s.size();
    char  x=s[0];
    rep(i,1,n){
        if(s[i]!=x){
            A.pb(mp(x, count));
            count=1;
            x=s[i];
        }else{
            count++;
        }
    }
    n=A.size();
    A.pb(mp(x, count));
    set<string>no;
    ll k;
    cin>>k;
    rep(i,0,k){
        cin>>e;
        no.insert(e);
        reverse be(e);
        no.insert(e);
    }
    ll ans=0, l=0, r=1;
    while(r<n){
        if(A[r].first==A[l].first){
            A[r].second+=A[l].second;
            l=r; r++;
            continue;
        }
        string a="";
        a+=A[l].first;a+=A[r].first;
        if(no.count(a)){
            if(r!=n-1&&A[r+1].first==A[l].first){
                A[r+1].second+=A[l].second;
                l=r, r++;
                continue;
            }
            if(A[l].second<A[r].second){
                ans+=A[l].second;
                if(A[r].first==A[l].first){
                    A[r].second+=A[l].second;
                }
                l=r, r++;
            }else{
                ans+=A[r].second;
                r++;
            }
        }else{
            l=r; r++;
        }
    }
    cout<<ans<<'\n';
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (ll r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
#define mp(a, b) make_pair(a, b)
const int MOD = 1000 * 1000 * 1000 + 7;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    string s, e;
    vector<pair<char, int>>A;
    cin>>s;
    int count=1, n=s.size();
    char  x=s[0];
    rep(i,1,n){
        if(s[i]!=x){
            A.pb(mp(x, count));
            count=1;
            x=s[i];
        }else{
            count++;
        }
    }
    A.pb(mp(x, count));
    set<string>no;
    int k;
    cin>>k;
    rep(i,0,k){
        cin>>e;
        no.insert(e);
        reverse be(e);
        no.insert(e);
    }
    int ans=0, l=0, r=1;
    while(r<n){
        string a="";
        a+=A[l].first;a+=A[r].first;
        if(no.count(a)){
            if(A[l].second<A[r].second){
                ans+=A[l].second;
                if(A[r].first==A[l].first){
                    A[r].second+=A[l].second;
                }
                l++, r++;
            }else{
                ans+=A[r].second;
                r++;
            }
        }else{
            if(A[r].first==A[l].first){
                A[r].second+=A[l].second;
            }
            l=r; r++;
        }
    }
    cout<<ans<<'\n';
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    string s, e;
    cin>>s;
    int k, a=0, b=0, ans=0;
    cin>>k;
    rep(i,0,k){
        cin>>e;
        rep(j,0,s.size()){
            if(s[j]==e[0])a++;
            else if(s[j]==e[1])b++;
            else{
                ans+=min(a,b);
                a=0, b=0;
            }
        }
        ans+=min(a,b);
        a=0, b=0;
    }
    cout<<ans<<'\n';
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (ll r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
#define mp(a, b) make_pair(a, b)
const ll MOD = 1000 * 1000 * 1000 + 7;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    string s, e;
    vector<pair<char, ll>>A;
    cin>>s;
    ll count=1, n=s.size();
    char  x=s[0];
    rep(i,1,n){
        if(s[i]!=x){
            A.pb(mp(x, count));
            count=1;
            x=s[i];
        }else{
            count++;
        }
    }
    A.pb(mp(x, count));
    set<string>no;
    ll k;
    cin>>k;
    rep(i,0,k){
        cin>>e;
        no.insert(e);
        reverse be(e);
        no.insert(e);
    }
    ll ans=0, l=0, r=1;
    while(r<n){
        if(A[r].first==A[l].first){
            A[r].second+=A[l].second;
            l=r; r++;
        }
        string a="";
        a+=A[l].first;a+=A[r].first;
        if(no.count(a)){
            if(r!=n-1&&A[r+1].first==A[l].first){
                A[r+1].second+=A[l].second;
                l=r, r++;
                continue;
            }
            if(A[l].second<A[r].second){
                ans+=A[l].second;
                if(A[r].first==A[l].first){
                    A[r].second+=A[l].second;
                }
                l=r, r++;
            }else{
                ans+=A[r].second;
                r++;
            }
        }else{
            l=r; r++;
        }
    }
    cout<<ans<<'\n';
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (ll r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
#define mp(a, b) make_pair(a, b)
const ll MOD = 1000 * 1000 * 1000 + 7;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    string s, e;
    vector<pair<char, ll>>A;
    cin>>s;
    ll count=1, n=s.size();
    char  x=s[0];
    rep(i,1,n){
        if(s[i]!=x){
            A.pb(mp(x, count));
            count=1;
            x=s[i];
        }else{
            count++;
        }
    }
    A.pb(mp(x, count));
    set<string>no;
    ll k;
    cin>>k;
    rep(i,0,k){
        cin>>e;
        no.insert(e);
        reverse be(e);
        no.insert(e);
    }
    ll ans=0, l=0, r=1;
    while(r<n){
        string a="";
        a+=A[l].first;a+=A[r].first;
        if(no.count(a)){
            if(r!=n-1&&A[r+1].first==A[l].first){
                A[r+1].second+=A[l].second;
                l=r, r++;
                continue;
            }
            if(A[l].second<A[r].second){
                ans+=A[l].second;
                if(A[r].first==A[l].first){
                    A[r].second+=A[l].second;
                }
                l=r, r++;
            }else{
                ans+=A[r].second;
                r++;
            }
        }else{
            if(A[r].first==A[l].first){
                A[r].second+=A[l].second;
            }
            l=r; r++;
        }
    }
    cout<<ans<<'\n';
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (ll r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
#define mp(a, b) make_pair(a, b)
const ll MOD = 1000 * 1000 * 1000 + 7;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    string s, e;
    vector<pair<char, ll>>A;
    cin>>s;
    ll count=1, n=s.size();
    char  x=s[0];
    rep(i,1,n){
        if(s[i]!=x){
            A.pb(mp(x, count));
            count=1;
            x=s[i];
        }else{
            count++;
        }
    }
    A.pb(mp(x, count));
    set<string>no;
    ll k;
    cin>>k;
    rep(i,0,k){
        cin>>e;
        no.insert(e);
        reverse be(e);
        no.insert(e);
    }
    ll ans=0, l=0, r=1;
    while(r<n){
        if(A[r].first==A[l].first){
            A[r].second+=A[l].second;
            l=r; r++;
            continue;
        }
        string a="";
        a+=A[l].first;a+=A[r].first;
        if(no.count(a)){
            if(r!=n-1&&A[r+1].first==A[l].first){
                A[r+1].second+=A[l].second;
                l=r, r++;
                continue;
            }
            if(A[l].second<A[r].second){
                ans+=A[l].second;
                if(A[r].first==A[l].first){
                    A[r].second+=A[l].second;
                }
                l=r, r++;
            }else{
                ans+=A[r].second;
                r++;
            }
        }else{
            l=r; r++;
        }
    }
    cout<<ans<<'\n';
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (ll r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
#define mp(a, b) make_pair(a, b)
const ll MOD = 1000 * 1000 * 1000 + 7;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    string s, e;
    vector<pair<char, ll>>A;
    cin>>s;
    ll count=1, n=s.size();
    char  x=s[0];
    rep(i,1,n){
        if(s[i]!=x){
            A.pb(mp(x, count));
            count=1;
            x=s[i];
        }else{
            count++;
        }
    }
    A.pb(mp(x, count));
    n=A.size();
    set<string>no;
    ll k;
    cin>>k;
    rep(i,0,k){
        cin>>e;
        no.insert(e);
        reverse be(e);
        no.insert(e);
    }
    ll ans=0, l=0, r=1;
    while(r<n){
        if(A[r].first==A[l].first){
            A[r].second+=A[l].second;
            l=r; r++;
            continue;
        }
        string a="";
        a+=A[l].first;a+=A[r].first;
        if(no.count(a)){
            if(r!=n-1&&A[r+1].first==A[l].first){
                A[r+1].second+=A[l].second;
                l=r, r++;
                continue;
            }
            if(A[l].second<A[r].second){
                ans+=A[l].second;
                if(A[r].first==A[l].first){
                    A[r].second+=A[l].second;
                }
                l=r, r++;
            }else{
                ans+=A[r].second;
                r++;
            }
        }else{
            l=r; r++;
        }
    }
    cout<<ans<<'\n';
}


#include<bits/stdc++.h>
using namespace std;
typedef  long long  ll;
const long long int mod=1e9 +7;
ll m,k,n,a[200005],red[200005]={0},edge[505][505];
set<char>st,st1,st2;
string s1="",s;
vector<ll>v[2000005],vnode[200005],v1,v2,v3;
vector< pair<ll,ll> >vv;
vector<pair< pair <ll,ll>,ll > > vvv;
priority_queue <ll> pq;
map<ll,ll>mp;

int main(){
	ios::sync_with_stdio(false);
    cin.tie(NULL);
	ll i=0,j=0,z=0,res=0,q,ans=0,mx=0,nb=0,nbmax=0,nbs=0,sm=0,nb2=0,mm=5e8,idx=0,ok=0,l,r,negatif=0,positif=0;
	ll x,y;
	set<char>::iterator it,it1,it2;
	char c;
   // freopen("a.txt","r",stdin);
    cin>>s;
    cin>>m;
    for(i=0;i<m;i++){
    	cin>>s1;
    	edge[s1[0]-'a'][s1[1]-'a']=edge[s1[1]-'a'][s1[0]-'a']=1;
	}
	n=s.size();
	if(n==1||!m)return cout<<"0",0;
	red[s[0]-'a']++;
	st.insert(s[0]);
	for(i=1;i<n;i++){
		it=st.begin();
		if(!st.size()||st.find(s[i])!=st.end()||st.size()==1&&edge[s[i]-'a'][(*it)-'a']==1){
			st.insert(s[i]);
			red[s[i]-'a']++;
		}
		else if(st.size()==2&&st.find(s[i])==st.end()){
			mm=5e9;
			for(it=st.begin();it!=st.end();it++)mm=min(mm,red[(*it)-'a']);
			res+=mm;
			for(it=st.begin();it!=st.end();it++)red[(*it)-'a']=0;
			st.clear();
			st.insert(s[i]);red[s[i]-'a']++;
	   }
	   else if(st.size()==1){
	   	  red[(*it)-'a']=0;
	   	  st.erase(*it);
	   	  st.insert(s[i]);
	   	  red[s[i]-'a']++;
	   }
	}
	if(st.size()==2){
			mm=5e9;
			for(it=st.begin();it!=st.end();it++)mm=min(mm,red[(*it)-'a']);
			res+=mm;
			st.clear();
	}
   cout<<res;
 }


#include<bits/stdc++.h>
using namespace std;
typedef  long long  ll;
const long long int mod=1e9 +7;
ll m,k,n,a[200005],dp[200005],edge[505][505];
set<ll>st,st1,st2;
string s1="",s;
vector<ll>v[2000005],vnode[200005],v1,v2,v3;
vector< pair<ll,ll> >vv;
vector<pair< pair <ll,ll>,ll > > vvv;
priority_queue <ll> pq;
map<ll,ll>mp;

int main(){
	ios::sync_with_stdio(false);
    cin.tie(NULL);
	ll i=0,j=0,z=0,res=0,q,ans=0,mx=0,nb=0,nbmax=0,nbs=0,sm=0,nb2=0,mm=5e8,idx=0,ok=0,l,r,negatif=0,positif=0;
	ll x,y;
	set<ll>::iterator it,it1,it2;
	char c;
    //freopen("a.txt","r",stdin);
    cin>>s;
    cin>>m;
    for(i=0;i<m;i++){
    	cin>>s1;
    	edge[s1[0]-'a'][s1[1]-'a']=edge[s1[1]-'a'][s1[0]-'a']=1;
	}
	n=s.size();
	if(n==1||!m)return cout<<"0",0;
	i=0;j=1;
	while(i<n&&j<n){
		while(edge[s[i]-'a'][s[j]-'a']==1){
			j++;
			res++;
		}
		i=j;
		j=i+1;
	}
	cout<<res;
 }


#include<bits/stdc++.h>
using namespace std;
typedef  long long  ll;
const long long int mod=1e9 +7;
ll m,k,n,a[200005],red[200005]={0},edge[505][505];
set<char>st,st1,st2;
string s1="",s;
vector<ll>v[2000005],vnode[200005],v1,v2,v3;
vector< pair<ll,ll> >vv;
vector<pair< pair <ll,ll>,ll > > vvv;
priority_queue <ll> pq;
map<ll,ll>mp;

int main(){
	ios::sync_with_stdio(false);
    cin.tie(NULL);
	ll i=0,j=0,z=0,res=0,q,ans=0,mx=0,nb=0,nbmax=0,nbs=0,sm=0,nb2=0,mm=5e8,idx=0,ok=0,l,r,negatif=0,positif=0;
	ll x,y;
	set<char>::iterator it,it1,it2;
	char c;
   // freopen("a.txt","r",stdin);
    cin>>s;
    cin>>m;
    for(i=0;i<m;i++){
    	cin>>s1;
    	edge[s1[0]-'a'][s1[1]-'a']=edge[s1[1]-'a'][s1[0]-'a']=1;
	}
	n=s.size();
	if(n==1||!m)return cout<<"0",0;
	red[s[0]-'a']++;
	st.insert(s[0]);
	for(i=1;i<n;i++){
		it=st.begin();
		if(st.find(s[i])!=st.end()||st.size()==1&&edge[s[i]-'a'][(*it)-'a']==1){
			st.insert(s[i]);
			red[s[i]-'a']++;
		}
		else if(st.size()==2&&st.find(s[i])==st.end()){
			mm=5e9;
			for(it=st.begin();it!=st.end();it++)mm=min(mm,red[(*it)-'a']);
			res+=mm;
			for(it=st.begin();it!=st.end();it++)red[(*it)-'a']=0;
			st.clear();
			st.insert(s[i]);red[s[i]-'a']++;
	   }
	}
	if(st.size()==2){
			mm=5e9;
			for(it=st.begin();it!=st.end();it++)mm=min(mm,red[(*it)-'a']);
			res+=mm;
			st.clear();
	}
   cout<<res;
 }


#include<iostream>
using namespace std;
#include<algorithm>
#include<map>
#include<vector>
#include<set>
#include<stack>
#include<queue> //priority_queue inside queue
#include<deque>
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define nl '\n'
typedef long long ll;
typedef unsigned long long ull;
#define f(i,start,end) for(i=start;i<=end;i++)
#define min_heap priority_queue<int, vector<int>, greater<int> >
#define max_heap priority_queue<int>
#define eb emplace_back
#define maxHeapPoint priority_queue<Pair>
#define mapGreater map<int, int, greater<int> >

class Pair //perfect! standard..
{
public:
    int first, second;
    Pair(): first(0), second(0) {}
    Pair(int x, int y): first(x), second(y) {}

    bool operator< (const Pair& obj) const {
        if(obj.first!=this->first)
            return obj.first > this->first;
        return obj.second > this->second; //sorts smaller to greater!
    }
};

/*int sum(int n)
{
    int ret = 0;
    if(n&1) ret = ((n+1)/2)*n;
    else ret = (n/2)*(n+1);
    return ret;
}
bool isPalindrome(string s, int n)
{
    int l = 0, r = n-1;
    while(l<r) {
        if(s[l]!=s[r]) return 0;
        l++;
        r--;
    }
    return 1;
}*/

int main()
{
    fast;
    //int t=0,tc=0; cin >> tc; f(t,1,tc) //testcases
    {
        int i=0, j=0;
        string s; cin >> s;
        int k; cin >> k;
        string a[k];
        f(i,0,k-1) cin >> a[i];
        int n = s.size();
        int ans=0;
        f(i, 0, n-1)
        {
          f(j, 0, k-1)
          {
            if(s[i]==a[j][0] || s[i]==a[j][1])
            {
                int first=0, second=0;
                while(s[i]==a[j][0]||s[i]==a[j][1])
                {
                    s[i]==a[j][0]? first++: second++;
                    i++;
                }
                ans += min(first, second);
                i--;
                break;
            }
          }
        }
        //pgpgppgggpbbnnn
        cout << ans << nl;
    }
    return 0;
}


#include<iostream>
using namespace std;
#include<algorithm>
#include<map>
#include<vector>
#include<set>
#include<stack>
#include<queue> //priority_queue inside queue
#include<deque>
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define nl '\n'
typedef long long ll;
typedef unsigned long long ull;
#define f(i,start,end) for(i=start;i<=end;i++)
#define min_heap priority_queue<int, vector<int>, greater<int> >
#define max_heap priority_queue<int>
#define eb emplace_back
#define maxHeapPoint priority_queue<Pair>
#define mapGreater map<int, int, greater<int> >

class Pair //perfect! standard..
{
public:
    int first, second;
    Pair(): first(0), second(0) {}
    Pair(int x, int y): first(x), second(y) {}

    bool operator< (const Pair& obj) const {
        if(obj.first!=this->first)
            return obj.first > this->first;
        return obj.second > this->second; //sorts smaller to greater!
    }
};

/*int sum(int n)
{
    int ret = 0;
    if(n&1) ret = ((n+1)/2)*n;
    else ret = (n/2)*(n+1);
    return ret;
}
bool isPalindrome(string s, int n)
{
    int l = 0, r = n-1;
    while(l<r) {
        if(s[l]!=s[r]) return 0;
        l++;
        r--;
    }
    return 1;
}*/

int main()
{
    fast;
    //int t=0,tc=0; cin >> tc; f(t,1,tc) //testcases
    {
        int i=0, j=0;
        string s; cin >> s;
        int k; cin >> k;
        string a[k];
        f(i,0,k-1) cin >> a[i];
        int n = s.size();
        int ans=0;
        f(i,0,n-1)
        {
            f(j,0,k-1)
            {
                if((s[i]==a[j][0]&&s[i+1]==a[j][1])||(s[i+1]==a[j][0]&&s[i]==a[j][1]))
                { ans++; i++; break; }
            }
        }
        cout << ans << nl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ins insert
#define int long long
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
string s;int k;
vector<pair<char,char>>forbiddenPair;
int answer=0;
void calculate(int l,int r,char e1,char e2)
{
    if(e1==e2)
    {
        answer=answer+(r-l);
    }
    else
    {
        int countE1=0;int countE2=0;
        for(int i=l;i<=r;i++)
        {
            if(s[i]==e1)countE1++;
            else countE2++;
        }
        if(countE1!=0 && countE2!=0)
            answer=answer+min(countE1,countE2);
    }
}
int32_t main()
{
    FAST;
    cin>>s;
    cin>>k;
    for(int i=1;i<=k;i++)
    {
        string x;cin>>x;
        forbiddenPair.pb(mp(x[0],x[1]));
    }
    for(int i=0;i<forbiddenPair.size();i++)
    {
        char e1=forbiddenPair[i].fi;
        char e2=forbiddenPair[i].se;
        int enterOnce=0;int l=-1;int r=-1;
        for(int j=0;j<s.size();j++)
        {
            if(s[j]==e1 || s[j]==e2)
            {
                if(enterOnce==0)l=j;
                else if(enterOnce==1)r=j;
                enterOnce=1;
            }
            else if(enterOnce==1 && s[j]!=e1 && s[j]!=e2)
            {
                enterOnce=0;
                if(r!=-1)
                {
                    calculate(l,r,e1,e2);l=-1;r=-1;
                }
            }
        }
        if(l!=-1 && r!=-1)calculate(l,r,e1,e2);
    }cout<<answer<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ins insert
#define int long long
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
string s;int k;
vector<string>forbiddenPair;
int n;
int32_t main()
{
    FAST;
    cin>>s;
    cin>>k;
    n=s.size();
    for(int i=1;i<=k;i++)
    {
        string x;cin>>x;
        forbiddenPair.pb(x);
    }
    int answer=0;
    int mainPointer=0;int secondaryPointer=1;
    if(n==1 || n==0)cout<<0<<endl;
    else
    {
        while(mainPointer<=n-1)
        {
            char a1=s[mainPointer];char a2=s[secondaryPointer];
            int flag=-1;
            for(int i=0;i<forbiddenPair.size();i++)
            {
                char b1=forbiddenPair[i][0];char b2=forbiddenPair[i][1];
                if( (a1==b1 && a2==b2) || (a1==b2 && a2==b1) )
                {
                    flag=i;break;
                }
            }
            if(flag!=-1)
            {
                if(secondaryPointer==n-1){/*cout<<"The deleted Character is "<<n-1<<endl;*/answer++;break;}
                else //Check whether to delete first character or second character
                {
                    char main=s[mainPointer];        char secondary=s[secondaryPointer];
                    char for1=forbiddenPair[flag][0];char for2=forbiddenPair[flag][1];
                    if(main==for1 && secondary==for2)
                    {
                        if(s[secondaryPointer+1]==for1)
                        {
                            //cout<<"The deleted Character is "<<secondaryPointer<<endl;
                            answer++;secondaryPointer++;
                        }
                        else
                        {
                            //cout<<"The deleted Character is "<<mainPointer<<endl;
                            answer++;mainPointer=secondaryPointer;secondaryPointer=mainPointer+1;
                        }
                    }
                    else if(main==for2 && secondary==for1)
                    {
                        if(s[secondaryPointer+1]==for2)
                        {
                            //cout<<"The deleted Character is "<<secondaryPointer<<endl;
                            answer++;secondaryPointer++;
                        }
                        else
                        {
                            //cout<<"The deleted Character is "<<mainPointer<<endl;
                            answer++;mainPointer=secondaryPointer;secondaryPointer=mainPointer+1;
                        }
                    }
                }
            }else {mainPointer=secondaryPointer;secondaryPointer=mainPointer+1;}
        }cout<<answer<<endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair< int,int > ii;
typedef map<int,int> mii;
typedef map< long long int, long long int> mll;

#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for( auto i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())

#define edl '\n'
#define ll long long int
#define mp make_pair

#define in(n) int n; cin >> n;
#define inl(n) ll n; cin >> n;
#define forn(i,n) for ( int i=0 ; i<n ; i++ )
#define forni(i,j,n) for ( int i=j ; i<n ; i++ )
#define forr (i,n) for ( int i=n; i>=0; i-- )
#define numb(arr,i) (int)(arr[i]-'0')
#define toi(ch) (int)(ch-'a')

#define filecode ifstream in;ofstream out;in.open("input.txt");out.open("output.txt");
#define Arr(n) int arr[n]={}; forn(i,n) cin>>arr[i];
#define setPrecision(n) cout << std::setprecision(n) << std::fixed;
#define outk(z,n) cout << "Case #" << z << ": " << n << endl;
#define MOD 1000000009
#define PI 3.14159265

int main()
{
    ios::sync_with_stdio(false) ;
	cin.tie(0); cout.tie(0);
    string in;
    cin >> in;
    char rel[26];
    in(n);
    char a,b;
    forn(i,n){
        cin >> a >> b;
        rel[toi(a)] = b;
        rel[toi(b)] = a;
    }
    n = in.length();
    a = in[0];
    int minn[n]={},cont=1;
    forni(i,1,n){
        cont = 1;
        while ( in[i]==a ){cont++;minn[i]=minn[i-1];i++;}
        if ( a==rel[toi(in[i])] ){
            minn[i] = min(minn[i-1]+1,cont+minn[max(0,i-cont-1)]);
        } else {
            minn[i] = minn[i-1];
        }
        //cout << i << " " << a << " " << minn[i] << edl;
        a = in[i];
    }
    cout << minn[n-1] << edl;

}


#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair< int,int > ii;
typedef map<int,int> mii;
typedef map< long long int, long long int> mll;

#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for( auto i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())

#define edl '\n'
#define ll long long int
#define mp make_pair

#define in(n) int n; cin >> n;
#define inl(n) ll n; cin >> n;
#define forn(i,n) for ( int i=0 ; i<n ; i++ )
#define forni(i,j,n) for ( int i=j ; i<n ; i++ )
#define forr (i,n) for ( int i=n; i>=0; i-- )
#define numb(arr,i) (int)(arr[i]-'0')
#define toi(ch) (int)(ch-'a')

#define filecode ifstream in;ofstream out;in.open("input.txt");out.open("output.txt");
#define Arr(n) int arr[n]={}; forn(i,n) cin>>arr[i];
#define setPrecision(n) cout << std::setprecision(n) << std::fixed;
#define outk(z,n) cout << "Case #" << z << ": " << n << endl;
#define MOD 1000000009
#define PI 3.14159265

int main()
{
    ios::sync_with_stdio(false) ;
	cin.tie(0); cout.tie(0);
    string in;
    cin >> in;
    char rel[26];
    forn(i,26){
        rel[i] = 'A';
    }
    in(n);
    char a,b;
    forn(i,n){
        cin >> a >> b;
        rel[toi(a)] = b;
        rel[toi(b)] = a;
    }
    n = in.length();
    int fs,se,mn=0;
    forn(i,n){
        a = in[i];
        b = rel[toi(a)];
        fs = 1;se = 0;
        while ( i<n ){
            if ( in[i+1]==a ) fs++;
            else if ( in[i+1]==b ) se++;
            else break;
            i++;
        }
        if ( fs==1 && se==0 ) continue;
        mn += min(fs,se);
    }
    cout << mn << edl;

}


#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair< int,int > ii;
typedef map<int,int> mii;
typedef map< long long int, long long int> mll;

#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for( auto i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())

#define edl '\n'
#define ll long long int
#define mp make_pair

#define in(n) int n; cin >> n;
#define inl(n) ll n; cin >> n;
#define forn(i,n) for ( int i=0 ; i<n ; i++ )
#define forni(i,j,n) for ( int i=j ; i<n ; i++ )
#define forr (i,n) for ( int i=n; i>=0; i-- )
#define numb(arr,i) (int)(arr[i]-'0')
#define toi(ch) (int)(ch-'a')

#define filecode ifstream in;ofstream out;in.open("input.txt");out.open("output.txt");
#define Arr(n) int arr[n]={}; forn(i,n) cin>>arr[i];
#define setPrecision(n) cout << std::setprecision(n) << std::fixed;
#define outk(z,n) cout << "Case #" << z << ": " << n << endl;
#define MOD 1000000009
#define PI 3.14159265

int main()
{
    ios::sync_with_stdio(false) ;
	cin.tie(0); cout.tie(0);
    string in;
    cin >> in;
    char rel[26];
    in(n);
    char a,b;
    forn(i,n){
        cin >> a >> b;
        rel[toi(a)] = b;
        rel[toi(b)] = a;
    }
    n = in.length();
    int fs,se,mn=0;
    forn(i,n){
        a = in[i];
        b = rel[toi(a)];
        fs = 1;se = 0;
        while ( i<n ){
            if ( in[i+1]==a ) fs++;
            else if ( in[i+1]==b ) se++;
            else break;
            i++;
        }
        mn += min(fs,se);
    }
    cout << mn << edl;

}


#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair< int,int > ii;
typedef map<int,int> mii;
typedef map< long long int, long long int> mll;

#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for( auto i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())

#define edl '\n'
#define ll long long int
#define mp make_pair

#define in(n) int n; cin >> n;
#define inl(n) ll n; cin >> n;
#define forn(i,n) for ( int i=0 ; i<n ; i++ )
#define forni(i,j,n) for ( int i=j ; i<n ; i++ )
#define forr (i,n) for ( int i=n; i>=0; i-- )
#define numb(arr,i) (int)(arr[i]-'0')
#define toi(ch) (int)(ch-'a')

#define filecode ifstream in;ofstream out;in.open("input.txt");out.open("output.txt");
#define Arr(n) int arr[n]={}; forn(i,n) cin>>arr[i];
#define setPrecision(n) cout << std::setprecision(n) << std::fixed;
#define outk(z,n) cout << "Case #" << z << ": " << n << endl;
#define MOD 1000000009
#define PI 3.14159265

int main()
{
    ios::sync_with_stdio(false) ;
	cin.tie(0); cout.tie(0);
    string in;
    cin >> in;
    char rel[26];
    in(n);
    char a,b;
    forn(i,n){
        cin >> a >> b;
        rel[toi(a)] = b;
        rel[toi(b)] = a;
    }
    n = in.length();
    a = in[0];
    int minn[n]={},cont=1;
    forni(i,1,n){
        while ( in[i]==a ){cont++;minn[i]=minn[i-1];i++;}
        if ( a==rel[toi(in[i])] ){
            minn[i] = min(minn[i-1]+1,cont+minn[max(0,i-cont-1)]);
        } else {
            minn[i] = minn[i-1];
        }
        //cout << i << " " << a << " " << minn[i] << edl;
        a = in[i];
    }
    cout << minn[n-1] << edl;

}


#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair< int,int > ii;
typedef map<int,int> mii;
typedef map< long long int, long long int> mll;

#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for( auto i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())

#define edl '\n'
#define ll long long int
#define mp make_pair

#define in(n) int n; cin >> n;
#define inl(n) ll n; cin >> n;
#define forn(i,n) for ( int i=0 ; i<n ; i++ )
#define forni(i,j,n) for ( int i=j ; i<n ; i++ )
#define forr (i,n) for ( int i=n; i>=0; i-- )
#define numb(arr,i) (int)(arr[i]-'0')
#define toi(ch) (int)(ch-'a')

#define filecode ifstream in;ofstream out;in.open("input.txt");out.open("output.txt");
#define Arr(n) int arr[n]={}; forn(i,n) cin>>arr[i];
#define setPrecision(n) cout << std::setprecision(n) << std::fixed;
#define outk(z,n) cout << "Case #" << z << ": " << n << endl;
#define MOD 1000000009
#define PI 3.14159265

int main()
{
    ios::sync_with_stdio(false) ;
	cin.tie(0); cout.tie(0);
    string in;
    cin >> in;
    char rel[26];
    in(n);
    char a,b;
    forn(i,n){
        cin >> a >> b;
        rel[toi(a)] = b;
        rel[toi(b)] = a;
    }
    n = in.length();
    int fs,se,mn=0;
    forn(i,n){
        a = in[i];
        b = rel[toi(a)];
        fs = 1;se = 0;
        while ( i<n ){
            if ( in[i+1]==a ) fs++;
            else if ( in[i+1]==b ) se++;
            else break;
            i++;
        }
        if ( fs=1 && se==0 ) continue;
        mn += min(fs,se);
    }
    cout << mn << edl;

}


#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair< int,int > ii;
typedef map<int,int> mii;
typedef map< long long int, long long int> mll;

#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for( auto i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())

#define edl '\n'
#define ll long long int
#define mp make_pair

#define in(n) int n; cin >> n;
#define inl(n) ll n; cin >> n;
#define forn(i,n) for ( int i=0 ; i<n ; i++ )
#define forni(i,j,n) for ( int i=j ; i<n ; i++ )
#define forr (i,n) for ( int i=n; i>=0; i-- )
#define numb(arr,i) (int)(arr[i]-'0')
#define toi(ch) (int)(ch-'a')

#define filecode ifstream in;ofstream out;in.open("input.txt");out.open("output.txt");
#define Arr(n) int arr[n]={}; forn(i,n) cin>>arr[i];
#define setPrecision(n) cout << std::setprecision(n) << std::fixed;
#define outk(z,n) cout << "Case #" << z << ": " << n << endl;
#define MOD 1000000009
#define PI 3.14159265

int main()
{
    ios::sync_with_stdio(false) ;
	cin.tie(0); cout.tie(0);
    string in;
    cin >> in;
    char rel[26];
    in(n);
    char a,b;
    forn(i,n){
        cin >> a >> b;
        rel[toi(a)] = b;
        rel[toi(b)] = a;
    }
    n = in.length();
    int fs,se,mn=0;
    forn(i,n){
        a = in[i];
        b = rel[toi(a)];
        fs = 1;se = 0;
        while ( i<n ){
            if ( in[i+1]==a ) fs++;
            else if ( in[i+1]==b ) se++;
            else break;
            i++;
        }
        if ( fs==1 && se==0 ) continue;
        mn += min(fs,se);
    }
    cout << mn << edl;

}


#include<bits/stdc++.h>
#define int long long
#define ll long long
#define maxN 1000005
#define rep(i,n) for(int i=0;i<n;i++)
#define refv(i,l,r) for(int i=l;i<r;i++)
#define rerv(i,l,r) for(int i=l;i>r;i--)
#define pi pair<ll,ll>
#define pii pair<ll,pair<ll,ll> >
int power(int x, unsigned int y){int res = 1;while (y > 0){ if (y & 1){res = res*x;} y = y>>1;x = x*x;}return res;}
int powermod(int x, unsigned int y, int p){int res = 1;x = x % p;while (y > 0){if (y & 1){res = (res*x) % p;}y = y>>1; x = (x*x) % p;}return res;}
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define inf 10000000000000
#define F first
#define S second
#define mod 1000000007
#define printvector(v) for(int i= 0;i<v.size();i++) {cout<<v[i]<<" ";} cout<<endl;
#define printset(s) for(auto it = s.begin();it!=S.end();it++){cout<<*it<<" ";} cout<<endl;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define endl '\n'
using namespace std;

int32_t main()
{
    fastio;
    string s;
    cin>>s;
    int n;
    cin>>n;
    int count=0;
    while(n--)
    {
    	string a;
    	cin>>a;
    	char one = a[0];
    	char two = a[1];
    	int sz = s.size();

    	for(int i=0;i<sz-1;i++)
    	{
    		int oc=0;
    	int tc=0;
    		if(s[i]==one||s[i]==two)
    		{
    			if(s[i]==one )
    			{
    				oc++;
				}
				else
				{
					tc++;
				}
    			for(int j=i+1;j<sz;j++)
    			{
    				if(s[j]!=one&&s[j]!=two)
    				{
    					break;
    					i=j;
					}
					else
					{
						if(s[j]==one)
						{
							oc++;
						}
						else
						{
							tc++;
						}
						i=j;
					}
				}
				count+=min(oc,tc);
			}

		}
    	//cout<<count<<endl;
	}

    cout<<count<<endl;
  return 0;

}


#include<bits/stdc++.h>
#define int long long
#define ll long long
#define maxN 1000005
#define rep(i,n) for(int i=0;i<n;i++)
#define refv(i,l,r) for(int i=l;i<r;i++)
#define rerv(i,l,r) for(int i=l;i>r;i--)
#define pi pair<ll,ll>
#define pii pair<ll,pair<ll,ll> >
int power(int x, unsigned int y){int res = 1;while (y > 0){ if (y & 1){res = res*x;} y = y>>1;x = x*x;}return res;}
int powermod(int x, unsigned int y, int p){int res = 1;x = x % p;while (y > 0){if (y & 1){res = (res*x) % p;}y = y>>1; x = (x*x) % p;}return res;}
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define inf 10000000000000
#define F first
#define S second
#define mod 1000000007
#define printvector(v) for(int i= 0;i<v.size();i++) {cout<<v[i]<<" ";} cout<<endl;
#define printset(s) for(auto it = s.begin();it!=S.end();it++){cout<<*it<<" ";} cout<<endl;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define endl '\n'
using namespace std;

int32_t main()
{
    fastio;
    string s;
    cin>>s;
    int n;
    cin>>n;
    int count=0;
    while(n--)
    {
    	string a;
    	cin>>a;
    	char one = a[0];
    	char two = a[1];
    	int sz = s.size();
    	for(int i=0;i<s.size()-1;i++)
    	{
    		if(s[i]==one && s[i+1]==two)
    		{   int sum = 1;
    		    if(i+1==sz-1)
    		    {
    		    	count++;
    		    	break;
				}
    			for(int j=i+2;j<sz;j++)
    			{

    				if(s[j]==one)
    				{
    					count++;
    					i=j;
    					break;
					}
					else if(s[j]!=two)
					{

						count++;
						break;
					}
					else if(s[j]==two)
					{

						count++;
						i=j-2;

					}

				}

			}

			if(s[i]==two && s[i+1]==one)
    		{   int sum = 1;
    		    if(i+1==sz-1)
    		    {
    		    	count++;
    		    	break;
				}
    			for(int j=i+2;j<sz;j++)
    			{

    				if(s[j]==two)
    				{
    					count++;
    					i=j;
    					break;
					}
					else if(s[j]!=one)
					{

						count++;
						break;
					}
					else if(s[j]==one)
					{

						count++;
						i=j-2;

					}

				}

			}
		}

	}

    cout<<count<<endl;
  return 0;

}


#include <bits/stdc++.h>

using namespace std;

#define int long long
#define endl '\n'

void solve() {
    string s;
    cin >> s;

    int k;
    cin >> k;

    vector<string> v;
    for (int i = 0; i < k; i++) {
        string tp;
        cin >> tp;
        v.push_back(tp);
        reverse(tp.begin(), tp.end());
        v.push_back(tp);
    }

    vector<int> vis(s.size() + 1, -1);
    for (int i = 0; i <= s.size(); i++)
        vis[i] = i;

    int ans = 0;
    for (int i = 1; i < s.size(); i++) {
        string tp;
        tp += s[vis[i - 1]];
        tp += s[i];
        for (auto x: v) {
            if (x == tp) {
                ans++;
                vis[i] = vis[i - 1];
                break;
            }
        }
    }
    cout << ans << endl;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
//    cin >> t;

    while (t--)
        solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long
#define endl '\n'

void solve() {
    string s;
    cin >> s;

    int k;
    cin >> k;

    map<char, char> mp;
    for (int i = 0; i < k; i++) {
        string tp;
        cin >> tp;
        mp[tp[0]] = tp[1];
        mp[tp[1]] = tp[0];
    }

    int ans = 0;
    for (int i = 0; i < s.size(); i++) {
        map<char, int> cnt;
        while (i < s.size() - 1 and (s[i] == s[i + 1] or s[i] == mp[s[i + 1]])) {
            cnt[s[i]]++;
            i++;
        }
        cnt[s[i]]++;
        int mx = 0;
        for (auto x: cnt)
            mx = max(mx, x.second);
        ans += mx;
    }
    cout << s.size() - ans << endl;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
//    cin >> t;

    while (t--)
        solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long
#define endl '\n'

void solve() {
    string s;
    cin >> s;

    int k;
    cin >> k;

    map<char, char> mp;
    for (int i = 0; i < k; i++) {
        string tp;
        cin >> tp;
        mp[tp[0]] = tp[1];
        mp[tp[1]] = tp[0];
    }

    int ans = 0;
    for (int i = 0; i < s.size(); i++) {
        map<char, int> cnt;
        bool fl = false;
        while (i < s.size() - 1 and (s[i] == s[i + 1] or s[i] == mp[s[i + 1]])) {
            cnt[s[i]]++;
            i++;
            fl = true;
        }
        cnt[s[i]]++;
        int mx = LLONG_MAX;
        for (auto x: cnt)
            mx = min(mx, x.second);
        if (cnt.size() > 1)
            ans += mx;
    }
    cout << ans << endl;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
//    cin >> t;

    while (t--)
        solve();
    return 0;
}