#define _CRT_SECURE_NO_WARNINGS
//#pragma comment(linker, "/STACK:100000000")
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <stdio.h>
#include <vector>
#include <map>

#define ll long long
#define ld long double
#define f(i, a, b) for(int i=(int)(a); i<(int)(b); ++i)
#define fn(i, b) for(int i=0; i<(int) (b); i++)
#define fz(i, a, b) for (int i=(int)(a); i>=(int)(b); --i)
#define mp make_pair
#define pii pair<ll, ll>
#define pb push_back
#define bg begin()
#define ed end()
#define pi 3.141592653589793238462643383279
ll gcd(ll a, ll b){ return (!b) ? a : gcd(b, a % b); }

ll lcm(ll a, ll b) { return (((a*b) / gcd(a, b))); }

#define MAX 1000100 //53
#define INF 10
#define y1 yy1
#define eps 0
void out(){ puts("-1"); exit(0); }

#define ls (v << 1)
#define rs (v << 1)+1
using namespace std;

string s;
int k;
char a, b;
int ans = 0;
char nums[1001];

int main(){
	//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

	cin >> s;
	fn(i, s.length()) nums[s[i]]++;
	cin >> k;
	fn(i, 700) nums[i] = (char)(700);

	getchar();
	while (k--) {
		cin >> a >> b;
		nums[a] = b;
		nums[b] = a;
	}
	ans = 0;
	for (int i=0; i<s.length()-1; ){
		char fa = s[i];
		char fb = nums[a];
		int counta = 0, countb = 0;
		while (s[i] == fa || s[i] == fb) {
			if (s[i] == fa) counta++; else countb++;
			i++;
		}
		ans += min(counta, countb);
	}
	cout << ans << endl;

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
//#pragma comment(linker, "/STACK:100000000")
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <stdio.h>
#include <vector>
#include <map>

#define ll long long
#define ld long double
#define f(i, a, b) for(int i=(int)(a); i<(int)(b); ++i)
#define fn(i, b) for(int i=0; i<(int) (b); i++)
#define fz(i, a, b) for (int i=(int)(a); i>=(int)(b); --i)
#define mp make_pair
#define pii pair<ll, ll>
#define pb push_back
#define bg begin()
#define ed end()
#define pi 3.141592653589793238462643383279
ll gcd(ll a, ll b){ return (!b) ? a : gcd(b, a % b); }

ll lcm(ll a, ll b) { return (((a*b) / gcd(a, b))); }

#define MAX 1000100 //53
#define INF 10
#define y1 yy1
#define eps 0
void out(){ puts("-1"); exit(0); }

#define ls (v << 1)
#define rs (v << 1)+1
using namespace std;

string s;
int k;
char a, b;
int ans = 0;
char nums[1001];

int main(){
	//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

	cin >> s;
	fn(i, s.length()) nums[s[i]]++;
	cin >> k;
	fn(i, 700) nums[i] = (char)(700);

	getchar();
	while (k--) {
		cin >> a >> b;
		getchar();
		nums[a] = b;
		nums[b] = a;
	}
	ans = 0;
	for (int i=0; i<s.length()-1; ){
		char fa = s[i];
		char fb = nums[s[i]];
		int counta = 0, countb = 0;
		while (s[i] == fa || s[i] == fb) {
			if (s[i] == fa) counta++; else countb++;
			i++;
		}
		ans += min(counta, countb);
	}
	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <ostream>
#include <istream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <limits>
#include <functional>
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <queue>
#include <iterator>
#include <stack>
#include <deque>
#include <numeric>
#include <tuple>
#include <ctime>
#include <array>
#include <random>
#include <iomanip>
#include <typeinfo>
#include <locale>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double dl;
typedef size_t sz;

typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef pair<ull, ull> pul;

typedef vector<int> vi;
typedef vector<vi> vvi;

typedef vector<sz> vsz;
typedef vector<vsz> vvsz;

typedef vector<unsigned int> vui;
typedef vector<vui> vvui;

typedef vector<long long> vl;
typedef vector<vl> vvl;

typedef vector<unsigned long long> vul;
typedef vector<vul> vvul;

typedef vector<double> vd;
typedef vector<vd> vvd;

typedef vector<string> vs;
typedef vector<vs> vvs;

int ri(){ int r; cin >> r; return r; }
unsigned int rui(){ unsigned int r; cin >> r; return r; }
long long rl(){ long long r; cin >> r; return r; }
unsigned long long rul(){ unsigned long long r; cin >> r; return r; }
double rd(){ double r; cin >> r; return r; }

string rss(){ string r; cin >> r; return r; }
string rs(){ string r; getline(cin, r); return r; }

vs rvs(int n)
{
	assert(n);

	vs r;

	for (int i = 0; i < n; ++i)
	{
		string s = rs();
		while (s.empty())
			s = rs();
		r.push_back(s);
	}
	return r;
}

vi rvi(int n){ assert(n); vi r; for (int i = 0; i < n; ++i) r.push_back(ri()); return r; }
vui rvui(int n){ assert(n); vui r; for (int i = 0; i < n; ++i) r.push_back(rui()); return r; }
vl rvl(int n){ assert(n); vl r; for (int i = 0; i < n; ++i) r.push_back(rl()); return r; }
vul rvul(int n){ assert(n); vul r; for (int i = 0; i < n; ++i) r.push_back(rul()); return r; }
vd rvd(int n){ assert(n); vd r; for (int i = 0; i < n; ++i) r.push_back(rd()); return r; }

#define all(cont) (cont).begin(), (cont).end()

////////////////////////////////////////////////////////////////////////////////////////////////////

int main(int argc, const char* argv[])
{
	auto s = rss();
	int n = ri();
	vs v = rvs(n);

	int r = 0;
	for (int i = 0; i < v.size(); ++i)
	{
		auto t = v[i];
		reverse(all(t));

		auto p1 = s.find(v[i]);
		auto p2 = s.find(t);

		while (p1 != s.npos || p2 != s.npos)
		{
			auto p = min(p1, p2);
			if (p + 1 != max(p1, p2))
				s.erase(p, 1);
			else
				s.erase(p + 1, 1);

			p1 = s.find(v[i]);
			p2 = s.find(t);

			++r;
		}
	}

	cout << r << endl;

	return 0;
}


#include <iostream>
#include <ostream>
#include <istream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <limits>
#include <functional>
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <queue>
#include <iterator>
#include <stack>
#include <deque>
#include <numeric>
#include <tuple>
#include <ctime>
#include <array>
#include <random>
#include <iomanip>
#include <typeinfo>
#include <locale>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double dl;
typedef size_t sz;

typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef pair<ull, ull> pul;

typedef vector<int> vi;
typedef vector<vi> vvi;

typedef vector<sz> vsz;
typedef vector<vsz> vvsz;

typedef vector<unsigned int> vui;
typedef vector<vui> vvui;

typedef vector<long long> vl;
typedef vector<vl> vvl;

typedef vector<unsigned long long> vul;
typedef vector<vul> vvul;

typedef vector<double> vd;
typedef vector<vd> vvd;

typedef vector<string> vs;
typedef vector<vs> vvs;

int ri(){ int r; cin >> r; return r; }
unsigned int rui(){ unsigned int r; cin >> r; return r; }
long long rl(){ long long r; cin >> r; return r; }
unsigned long long rul(){ unsigned long long r; cin >> r; return r; }
double rd(){ double r; cin >> r; return r; }

string rss(){ string r; cin >> r; return r; }
string rs(){ string r; getline(cin, r); return r; }

vs rvs(int n)
{
	assert(n);

	vs r;

	for (int i = 0; i < n; ++i)
	{
		string s = rs();
		while (s.empty())
			s = rs();
		r.push_back(s);
	}
	return r;
}

vi rvi(int n){ assert(n); vi r; for (int i = 0; i < n; ++i) r.push_back(ri()); return r; }
vui rvui(int n){ assert(n); vui r; for (int i = 0; i < n; ++i) r.push_back(rui()); return r; }
vl rvl(int n){ assert(n); vl r; for (int i = 0; i < n; ++i) r.push_back(rl()); return r; }
vul rvul(int n){ assert(n); vul r; for (int i = 0; i < n; ++i) r.push_back(rul()); return r; }
vd rvd(int n){ assert(n); vd r; for (int i = 0; i < n; ++i) r.push_back(rd()); return r; }

#define all(cont) (cont).begin(), (cont).end()

////////////////////////////////////////////////////////////////////////////////////////////////////

int main(int argc, const char* argv[])
{
	auto s = rss();
	int n = ri();
	vs v = rvs(n);

	int r = 0;
	for (int i = 0; i < v.size(); ++i)
	{
		auto b = s.find_first_of(v[i]);
		while (b != s.npos)
		{
			auto e = s.find_first_not_of(v[i], b + 1);
			if (e == s.npos)
				e = s.size();

			auto cf = count(s.begin() + b, s.begin() + e, v[i][0]);
			auto cs = count(s.begin() + b, s.begin() + e, v[i][1]);

			r += min(cf, cs);
			b = s.find_first_of(v[i], e);
		}
	}

	cout << r << endl;

	return 0;
}


#include <iostream>
#include <ostream>
#include <istream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <limits>
#include <functional>
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <queue>
#include <iterator>
#include <stack>
#include <deque>
#include <numeric>
#include <tuple>
#include <ctime>
#include <array>
#include <random>
#include <iomanip>
#include <typeinfo>
#include <locale>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double dl;
typedef size_t sz;

typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef pair<ull, ull> pul;

typedef vector<int> vi;
typedef vector<vi> vvi;

typedef vector<sz> vsz;
typedef vector<vsz> vvsz;

typedef vector<unsigned int> vui;
typedef vector<vui> vvui;

typedef vector<long long> vl;
typedef vector<vl> vvl;

typedef vector<unsigned long long> vul;
typedef vector<vul> vvul;

typedef vector<double> vd;
typedef vector<vd> vvd;

typedef vector<string> vs;
typedef vector<vs> vvs;

int ri(){ int r; cin >> r; return r; }
unsigned int rui(){ unsigned int r; cin >> r; return r; }
long long rl(){ long long r; cin >> r; return r; }
unsigned long long rul(){ unsigned long long r; cin >> r; return r; }
double rd(){ double r; cin >> r; return r; }

string rss(){ string r; cin >> r; return r; }
string rs(){ string r; getline(cin, r); return r; }

vs rvs(int n)
{
	assert(n);

	vs r;

	for (int i = 0; i < n; ++i)
	{
		string s = rs();
		while (s.empty())
			s = rs();
		r.push_back(s);
	}
	return r;
}

vi rvi(int n){ assert(n); vi r; for (int i = 0; i < n; ++i) r.push_back(ri()); return r; }
vui rvui(int n){ assert(n); vui r; for (int i = 0; i < n; ++i) r.push_back(rui()); return r; }
vl rvl(int n){ assert(n); vl r; for (int i = 0; i < n; ++i) r.push_back(rl()); return r; }
vul rvul(int n){ assert(n); vul r; for (int i = 0; i < n; ++i) r.push_back(rul()); return r; }
vd rvd(int n){ assert(n); vd r; for (int i = 0; i < n; ++i) r.push_back(rd()); return r; }

#define all(cont) (cont).begin(), (cont).end()

////////////////////////////////////////////////////////////////////////////////////////////////////

int main(int argc, const char* argv[])
{
	auto s = rss();
	int n = ri();
	if (!n)
	{
		cout << s << endl;
		return 0;
	}

	vs v = rvs(n);

	int r = 0;
	for (int i = 0; i < v.size(); ++i)
	{
		auto b = s.find_first_of(v[i]);
		while (b != s.npos)
		{
			auto e = s.find_first_not_of(v[i], b + 1);
			if (e == s.npos)
				e = s.size();

			auto cf = count(s.begin() + b, s.begin() + e, v[i][0]);
			auto cs = count(s.begin() + b, s.begin() + e, v[i][1]);

			r += min(cf, cs);
			b = s.find_first_of(v[i], e);
		}
	}

	cout << r << endl;

	return 0;
}


#include <iostream>
#include <ostream>
#include <istream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <limits>
#include <functional>
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <queue>
#include <iterator>
#include <stack>
#include <deque>
#include <numeric>
#include <tuple>
#include <ctime>
#include <array>
#include <random>
#include <iomanip>
#include <typeinfo>
#include <locale>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double dl;
typedef size_t sz;

typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef pair<ull, ull> pul;

typedef vector<int> vi;
typedef vector<vi> vvi;

typedef vector<sz> vsz;
typedef vector<vsz> vvsz;

typedef vector<unsigned int> vui;
typedef vector<vui> vvui;

typedef vector<long long> vl;
typedef vector<vl> vvl;

typedef vector<unsigned long long> vul;
typedef vector<vul> vvul;

typedef vector<double> vd;
typedef vector<vd> vvd;

typedef vector<string> vs;
typedef vector<vs> vvs;

int ri(){ int r; cin >> r; return r; }
unsigned int rui(){ unsigned int r; cin >> r; return r; }
long long rl(){ long long r; cin >> r; return r; }
unsigned long long rul(){ unsigned long long r; cin >> r; return r; }
double rd(){ double r; cin >> r; return r; }

string rss(){ string r; cin >> r; return r; }
string rs(){ string r; getline(cin, r); return r; }

vs rvs(int n)
{
	assert(n);

	vs r;

	for (int i = 0; i < n; ++i)
	{
		string s = rs();
		while (s.empty())
			s = rs();
		r.push_back(s);
	}
	return r;
}

vi rvi(int n){ assert(n); vi r; for (int i = 0; i < n; ++i) r.push_back(ri()); return r; }
vui rvui(int n){ assert(n); vui r; for (int i = 0; i < n; ++i) r.push_back(rui()); return r; }
vl rvl(int n){ assert(n); vl r; for (int i = 0; i < n; ++i) r.push_back(rl()); return r; }
vul rvul(int n){ assert(n); vul r; for (int i = 0; i < n; ++i) r.push_back(rul()); return r; }
vd rvd(int n){ assert(n); vd r; for (int i = 0; i < n; ++i) r.push_back(rd()); return r; }

#define all(cont) (cont).begin(), (cont).end()

////////////////////////////////////////////////////////////////////////////////////////////////////

int main(int argc, const char* argv[])
{
	auto s = rss();
	int n = ri();
	if (!n)
	{
		cout << 0 << endl;
		return 0;
	}

	vs v = rvs(n);

	int r = 0;
	for (int i = 0; i < v.size(); ++i)
	{
		auto b = s.find_first_of(v[i]);
		while (b != s.npos)
		{
			auto e = s.find_first_not_of(v[i], b + 1);
			if (e == s.npos)
				e = s.size();

			auto cf = count(s.begin() + b, s.begin() + e, v[i][0]);
			auto cs = count(s.begin() + b, s.begin() + e, v[i][1]);

			r += min(cf, cs);
			b = s.find_first_of(v[i], e);
		}
	}

	cout << r << endl;

	return 0;
}


#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define CLEAR(a, val) memset(a, val, sizeof(a))
#define UP(i, s, t) for (int i = (s); i < (t); i += 1)
#define DOWN(i, s, t) for (int i = (s); i > (t); i -= 1)

typedef long long ll;
using namespace std;
const int MAXINT = 1 << 31 - 1;
const int MAXN = 100005;

char arr[MAXN];
int len, k;

int main () {
	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif

	while (scanf("%s", arr) != -1) {
		len = strlen(arr);
		cin >> k;

		int res = 0;
		UP(i, 0, k) {
			char x, y;
			cin >> x >> y;

			int j = 0;
			UP(j, 0, len) {
				int cx = 0, cy = 0;
				while (j < len && (arr[j] == x || arr[j] == y)) {
					cx += arr[j] == x;
					cy += arr[j] == y;
					j ++;
				}
				res += min(cx, cy);
			}
		}

		cout << res << endl;
	}
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <set>
using namespace std;

#define STRMAX 100010
#define ABET 26
int dp[ STRMAX ][ 26 ];

int main(int argc, char* argv[])
{
    //freopen( "in.txt", "rt", stdin );
    //memset( dp, -1, sizeof( dp ) );
    for( int i = 0; i < STRMAX; ++i )
    {
        for( int j = 0; j < ABET; ++j )
        {
            dp[ i ][ j ] = INT_MAX / 2;
        }
    }
    string t;
    cin >> t;
    int k = 0;
    cin >> k;
    set<pair<int, int > > forbs;
    for( int i = 0; i < k; ++i )
    {
        string a;
        cin >> a ;
        forbs.insert( make_pair( a[ 0 ] - 'a', a[ 1 ] - 'a') );
        forbs.insert( make_pair( a[ 1 ] - 'a', a[ 0 ] - 'a') );
    }

    dp[ 0 ][ t[ 0 ] - 'a' ] = 0;

    for( int i = 1; i < t.size( ); ++i )
    {
        int abet = t[ i ] - 'a';

        for( int j = 0; j < ABET; ++j )
        {
            if( forbs.end( ) == forbs.find( make_pair( abet, j ) ) )
            {
                dp[ i ][ abet ] = min( dp[ i - 1 ][ j ], dp[ i ][ abet ] );
            }

            dp[ i ][ j ] = min( dp[ i ][ j ],  1 + dp[ i - 1 ][ j ] );
        }
    }

    int mm = INT_MAX;
    for( int i = 0; i < ABET; ++i )
    {
        mm = min( dp[ t.size( ) - 1 ][ i ], mm );
    }

    cout << mm;
}


//// SRM534.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <set>
using namespace std;

//long long sett[ 1000000 ] = { 0 };
//long long cur = 0;
//vector<set<int> > friends;
//
//
//bool equals( set<int>& a, set<int>& b )
//{
//  if( a.size( ) != b.size( ) ) return false;
//  for( set<int>::iterator ita = a.begin( ), itb = b.begin( ); a.end() != ita; ++ita, ++itb )
//  {
//      if( (*ita) != (*itb ) ) return false;
//  }
//
//  return true;
//}
//
//int in_set( int a, set<int> v )
//{
//  for( set<int>::iterator it = v.begin( ); it != v.end( ); ++it )
//  {
//      if(  equals( friends[ a ], friends[ *it ] ) )
//      {
//          return sett[ *it ];
//      }
//  }
//
//  return -1;
//}
//
//int main( )
//{
//  freopen( "in.txt",  "rt", stdin );
//
//  int n, m;
//  cin >> n >> m;
//  friends.resize( n );
//  for( int i = 0; i < m; ++i )
//  {
//      int a = 0, b = 0;
//      scanf( "%d %d", &a, &b );
//      --a;
//      --b;
//
//      friends[ a ].insert( b );
//      friends[ b ].insert( a );
//
//      if( sett[ a ] == sett[ b ] )
//      {
//          sett[ a ] = cur;
//          sett[ b ] = cur;
//          ++cur;
//      }
//      else
//      {
//          sett[ a ] = cur;
//          ++cur;
//          sett[ b ] = cur;
//          ++cur;
//      }
//
//      int ka = in_set( a, friends[ b ] );
//      if( ka != -1 )
//      {
//          sett[ a ] = ka;
//      }
//
//      int kb = in_set( b, friends[ a ] );
//
//      if( kb != -1 )
//      {
//          sett[ b ] = kb;
//      }
//  }
//
//  map<long long, int > counts;
//
//  long long res = 0;
//
//  for( int i = 0; i < friends.size( ); ++i )
//  {
//      counts[ sett[ i ] ]++;
//  }
//
//  for( map< long long, int>::iterator it = counts.begin( ); it != counts.end( ); ++it )
//  {
//      res += it->second * ( it->second - 1 ) / 2;
//  }
//
//  cout << res;
//
//}

//int gcd(int a, int b)
//{
//    while( 1 )
//    {
//        a = a % b;
//      if( a == 0 )
//          return b;
//      b = b % a;
//
//        if( b == 0 )
//          return a;
//    }
//}
//
//
//int conflict( int k, set<int>& act )
//{
//  for( set<int>::iterator it = act.begin( ); it != act.end( ); ++it )
//  {
//      if( gcd( (*it), k ) != 1 )
//      {
//          return (*it);
//      }
//  }
//
//  return -1;
//}
//
//int main( )
//{
//  //freopen( "in.txt", "rt", stdin );
//  int n, m;
//  cin >> n >> m;
//  set<int> active;
//
//  for( int i = 0; i < m; ++i )
//  {
//      char s;
//      int k;
//      cin >> s >> k;
//      if( s == '-' )
//      {
//          if( active.end( ) == active.find( k ) )
//          {
//              cout << "Already off" << endl;
//          }
//          else
//          {
//              cout << "Success" << endl;
//              active.erase( k );
//          }
//      }
//      else
//      {
//          if( active.end( ) != active.find( k ) )
//          {
//              cout << "Already on" << endl;
//          }
//          else
//          {
//              int s = conflict( k, active );
//
//              if( -1 == s )
//              {
//                  cout << "Success" << endl;
//                  active.insert( k );
//              }
//              else
//              {
//                  cout << "Conflict with " << s << endl;
//              }
//          }
//
//      }
//  }
//}

#define STRMAX 100010
#define ABET 27
int dp[ STRMAX ][ ABET ];

int main(int argc, char* argv[])
{
    //freopen( "in.txt", "rt", stdin );
    //memset( dp, -1, sizeof( dp ) );
    for( int i = 0; i < STRMAX; ++i )
    {
        for( int j = 0; j < ABET; ++j )
        {
            dp[ i ][ j ] = INT_MAX / 2;
        }
    }
    string t;
    cin >> t;
    int k = 0;
    cin >> k;
    set<pair<int, int > > forbs;
    for( int i = 0; i < k; ++i )
    {
        string a;
        cin >> a ;
        forbs.insert( make_pair( a[ 0 ] - 'a', a[ 1 ] - 'a') );
        forbs.insert( make_pair( a[ 1 ] - 'a', a[ 0 ] - 'a') );
    }

    dp[ 0 ][ t[ 0 ] - 'a' ] = 0;
    dp[ 0 ][ 26 ] = 1;

    for( int i = 1; i < t.size( ); ++i )
    {
        int abet = t[ i ] - 'a';

        for( int j = 0; j < ABET; ++j )
        {
            if( forbs.end( ) == forbs.find( make_pair( abet, j ) ) )
            {
                dp[ i ][ abet ] = min( dp[ i - 1 ][ j ], dp[ i ][ abet ] );
            }

            dp[ i ][ j ] = min( dp[ i ][ j ],  1 + dp[ i - 1 ][ j ] );
        }
    }

    int mm = INT_MAX;
    for( int i = 0; i < ABET; ++i )
    {
        mm = min( dp[ t.size( ) - 1 ][ i ], mm );
    }

    cout << mm;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <set>
using namespace std;

#define STRMAX 100010
#define ABET 26
int dp[ 10001 ][ 26 ];

int main(int argc, char* argv[])
{
    //freopen( "in.txt", "rt", stdin );
    //memset( dp, -1, sizeof( dp ) );
    for( int i = 0; i < STRMAX; ++i )
    {
        for( int j = 0; j < ABET; ++j )
        {
            dp[ i ][ j ] = INT_MAX / 2;
        }
    }
    string t;
    cin >> t;
    int k = 0;
    cin >> k;
    set<pair<int, int > > forbs;
    for( int i = 0; i < k; ++i )
    {
        string a;
        cin >> a ;
        forbs.insert( make_pair( a[ 0 ] - 'a', a[ 1 ] - 'a') );
        forbs.insert( make_pair( a[ 1 ] - 'a', a[ 0 ] - 'a') );
    }

    dp[ 0 ][ t[ 0 ] - 'a' ] = 0;

    for( int i = 1; i < t.size( ); ++i )
    {
        int abet = t[ i ] - 'a';

        for( int j = 0; j < ABET; ++j )
        {
            if( forbs.end( ) == forbs.find( make_pair( abet, j ) ) )
            {
                dp[ i ][ abet ] = min( dp[ i - 1 ][ j ], dp[ i ][ abet ] );
            }

            dp[ i ][ j ] = min( dp[ i ][ j ],  1 + dp[ i - 1 ][ j ] );
        }
    }

    int mm = INT_MAX;
    for( int i = 0; i < ABET; ++i )
    {
        mm = min( dp[ t.size( ) - 1 ][ i ], mm );
    }

    cout << mm;
}


#include<stdio.h>
#include<string.h>

#define maxm 100010
#define inf (1<<29)

int dp[maxm][30],op[30],n;
char s[maxm],s1[10];
int cal(int ind,int last);
int mini(int a,int b){
    if(a<b) return a; return b;
}

int main(){

    int i,j,k,l;

    //freopen("in.txt","r",stdin);

    scanf("%s",s);
    scanf("%d",&k);  memset(op,-1,sizeof(op));
    for(i=1;i<=k;i++){
        scanf("%s",s1);
        op[s1[0]-'a']=s1[1]-'a';
        op[s1[1]-'a']=s1[0]-'a';
    }
    n=strlen(s); memset(dp,-1,sizeof(dp));
    printf("%d\n",cal(0,27));

    return 0;
}

int cal(int ind,int last){

	if(ind==n) return 0;
    if(dp[ind][last]!=-1) return dp[ind][last];

    int ret,now;
    now=s[ind]-'a';
    //if(ind==0) return dp[ind][last]=cal(ind+1,now);

    if(op[now]==last){
        return dp[ind][last]=cal(ind+1,last)+1;
    }
    ret=cal(ind+1,last)+1;
	ret=mini(ret,cal(ind+1,now));

    return dp[ind][last]=ret;
}


#include<stdio.h>
#include<string.h>

#define maxm 100010

int dp[maxm][30],op[30],n;
char s[maxm],s1[10];
int cal(int ind,int last);
int mini(int a,int b){
    if(a<b) return a; return b;
}

int main(){

    int i,j,k,l;

    scanf("%s",s);
    scanf("%d",&k);
    for(i=1;i<=k;i++){
        scanf("%s",s1);
        op[s1[0]-'a']=s1[1]-'a';
        op[s1[1]-'a']=s1[0]-'a';
    }
    n=strlen(s); memset(dp,-1,sizeof(dp));
    printf("%d\n",cal(0,0));

    return 0;
}

int cal(int ind,int last){

    if(ind==n) return 0;
    if(dp[ind][last]!=-1) return dp[ind][last];

    int ret,now;
    now=s[ind]-'a';
    if(ind==0) return dp[ind][last]=cal(ind+1,now);
    if(op[now]==last){
        return dp[ind][last]=mini(cal(ind+1,now),cal(ind+1,last))+1;
    }
    ret=mini(cal(ind+1,now),cal(ind+1,last))+1;
    ret=mini(cal(ind+1,now),ret);

    return dp[ind][last]=ret;
}


#include<stdio.h>
#include<string.h>

#define maxm 100010

int dp[maxm][30],op[30],n;
char s[maxm],s1[10];
int cal(int ind,int last);
int mini(int a,int b){
    if(a<b) return a; return b;
}

int main(){

    int i,j,k,l;

    scanf("%s",s);
    scanf("%d",&k);  memset(op,-1,sizeof(op));
    for(i=1;i<=k;i++){
        scanf("%s",s1);
        op[s1[0]-'a']=s1[1]-'a';
        op[s1[1]-'a']=s1[0]-'a';
    }
    n=strlen(s); memset(dp,-1,sizeof(dp));
    printf("%d\n",cal(0,0));

    return 0;
}

int cal(int ind,int last){

    if(ind==n) return 0;
    if(dp[ind][last]!=-1) return dp[ind][last];

    int ret,now;
    now=s[ind]-'a';
    if(ind==0) return dp[ind][last]=cal(ind+1,now);
    if(op[now]==last){
        return dp[ind][last]=mini(cal(ind+1,now),cal(ind+1,last))+1;
    }
    ret=mini(cal(ind+1,now),cal(ind+1,last))+1;
    ret=mini(cal(ind+1,now),ret);

    return dp[ind][last]=ret;
}


#include<stdio.h>
#include<string.h>

#define maxm 100010
#define inf (1<<29)

int dp[maxm][30],op[30],n;
char s[maxm],s1[10];
int cal(int ind,int last);
int mini(int a,int b){
    if(a<b) return a; return b;
}

int main(){

    int i,j,k,l;

    //freopen("in.txt","r",stdin);

    scanf("%s",s);
    scanf("%d",&k);  memset(op,-1,sizeof(op));
    for(i=1;i<=k;i++){
        scanf("%s",s1);
        op[s1[0]-'a']=s1[1]-'a';
        op[s1[1]-'a']=s1[0]-'a';
    }
    n=strlen(s); memset(dp,-1,sizeof(dp));
    printf("%d\n",cal(0,27));

    return 0;
}

int cal(int ind,int last){

    if(ind==n) return 0;
    if(dp[ind][last]!=-1) return dp[ind][last];

    int ret,now;
    now=s[ind]-'a';
    //if(ind==0) return dp[ind][last]=cal(ind+1,now);

    if(op[now]==last){
        return dp[ind][last]=cal(ind+1,last)+1;
    }
    ret=cal(ind+1,last)+1;
    ret=mini(ret,cal(ind+1,now));

    return dp[ind][last]=ret;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstdlib>

using namespace std;

#define FOR(i, a, b) for(int i = a; i < b; i++)
#define REP(i, n) FOR(i, 0, n)
#define X first
#define Y second

typedef vector<int> VI;
typedef vector<VI> VVI;

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    string s, s1;
    getline(cin, s);
    int K, n, cnt = 0;
    cin >> K;
    getline(cin, s1);
    n = s.length();
    REP(i, K) {
        getline(cin, s1);
        pair<int, int> sch, nul;
        nul.first = 0;
        nul.second = 0;
        sch = nul;
        REP(j, n)
            if(s[j] == s1[0])
                sch.first++;
            else if(s[j] == s1[1])
                sch.second++;
            else if(sch.first > 0 && sch.second > 0) {
                cnt += min(sch.first, sch.second);
                sch = nul;
            }
        if(sch.first > 0 && sch.second > 0)
            cnt += min(sch.first, sch.second);
    }
    cout << cnt;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstdlib>

using namespace std;

#define FOR(i, a, b) for(int i = a; i < b; i++)
#define REP(i, n) FOR(i, 0, n)
#define X first
#define Y second

typedef vector<int> VI;
typedef vector<VI> VVI;

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    string s, s1;
    getline(cin, s);
    int K, n, cnt = 0;
    cin >> K;
    getline(cin, s1);
    n = s.length();
    REP(i, K) {
        getline(cin, s1);
        pair<int, int> sch, nul;
        nul.first = 0;
        nul.second = 0;
        sch = nul;
        REP(j, n)
            if(s[j] == s1[0])
                sch.first++;
            else if(s[j] == s1[1])
                sch.second++;
            else  {
                if(sch.first > 0 && sch.second > 0)
                    cnt += min(sch.first, sch.second);
                sch = nul;
            }
        if(sch.first > 0 && sch.second > 0)
            cnt += min(sch.first, sch.second);
    }
    cout << cnt;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstdlib>

using namespace std;

#define FOR(i, a, b) for(int i = a; i < b; i++)
#define REP(i, n) FOR(i, 0, n)
#define X first
#define Y second

typedef vector<int> VI;
typedef vector<VI> VVI;

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    string s, s1;
    getline(cin, s);
    int K, n;
    cin >> K;
    getline(cin, s1);
    n = s.size();
    int cnt = 0;
    REP(i, K) {
        getline(cin, s1);
        pair<int, int> sch, big, nul;
        nul.first = 0;
        nul.second = 0;
        sch = big = nul;
        REP(j, n)
            if(s[j] == s1[0])
                sch.first++;
            else if(s[j] == s1[1])
                sch.second++;
            else {
                if(sch.first > 0 && sch.second > 0) {
                    big.first += sch.first;
                    big.second += sch.second;
                }
                sch = nul;
            }
        if(sch.first > 0 && sch.second > 0) {
            big.first += sch.first;
            big.second += sch.second;
        }
        cnt += min(big.first, big.second);
    }
    cout << cnt;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <string.h>
#include <math.h>
#include <fstream>
using namespace std;
#define fon(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
#define re return
#define ll long long
const double EPS = 1E-9;
const int INF = 1000000000;
const ll INF64 = (ll)1E18;
const double PI = 3.1415926535897932384626433832795;

ll n,m,h;

typedef map<ll,ll>tmap;
typedef set<ll>tset;
set<ll>::iterator it,itt,itlow,itup;
set<ll>::reverse_iterator irev;

typedef struct {char a,b;}tpair;

int main()
{
	#ifdef _DINARISIO
	ifstream cin("input.txt");
	ofstream cout("output.txt");
	#endif

	ll i,k,j,n,lmax=-1,lmin=-1,sum=0;
	char *s=new char[200000];
	tpair t[20];
	cin>>s;cin>>k;
	int cc[500]={0};
	fon(i,k){
		cin>>t[i].a>>t[i].b;
		cc[t[i].a+100]=i+1;
		cc[t[i].b+100]=i+1;
	}
	ll slen=strlen(s),plen=0,ul=0,alen=0,blen=0;
	fon(i,slen){
		if(i>0&&cc[s[i]+100]==cc[s[i-1]+100]&&cc[s[i-1]+100]>0)
		{
			ul=cc[s[i]+100];
			if(alen+blen==0){
				if(t[cc[s[i]+100]-1].a==s[i-1])alen++;
				else blen++;
			}
			if(t[cc[s[i]+100]-1].a==s[i])alen++;
			else blen++;
		}
		else{
			if(ul>0){
				if(t[ul-1].a==t[ul-1].b){
					sum+=alen+blen-1;
				}else
				{
					if(blen<alen)alen=blen;
					sum+=alen;
				}
				ul=0;alen=0;blen=0;
			}
		}
	}
	if(ul>0){
				if(t[ul-1].a==t[ul-1].b){
					sum+=alen+blen-1;
				}else
				{
					if(blen<alen)alen=blen;
					sum+=alen;
				}
				ul=0;
			}
	cout<<sum;
	re 0;
}


#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <string.h>
#include <math.h>
#include <fstream>
using namespace std;
#define fon(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
#define re return
#define ll long long
const double EPS = 1E-9;
const int INF = 1000000000;
const ll INF64 = (ll)1E18;
const double PI = 3.1415926535897932384626433832795;

ll n,m,h;

typedef map<ll,ll>tmap;
typedef set<ll>tset;
set<ll>::iterator it,itt,itlow,itup;
set<ll>::reverse_iterator irev;

typedef struct {char a,b;}tpair;

int main()
{
	#ifdef _DINARISIO
	ifstream cin("input.txt");
	ofstream cout("output.txt");
	#endif

	ll i,k,j,n,lmax=-1,lmin=-1,sum=0;
	char *s=new char[200000];
	tpair t[20];
	cin>>s;cin>>k;
	int cc[500]={0};
	fon(i,k){
		cin>>t[i].a>>t[i].b;
		cc[t[i].a+100]=i+1;
		cc[t[i].b+100]=i+1;
	}
	ll slen=strlen(s),plen=0;
	fon(i,slen){
		if(i>0&&cc[s[i]+100]>0&&cc[s[i-1]+100]==cc[s[i]+100])plen++;
		else{
			if(plen>0){
				if(plen==1)sum+=1;
				else sum+=plen/2;
				plen=0;
			}
		}
	}if(plen>0){if(plen==1)sum+=1;
				else sum+=plen/2;
	}
	cout<<sum;
	re 0;
}


#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <string.h>
#include <math.h>
#include <fstream>
using namespace std;
#define fon(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
#define re return
#define ll long long
const double EPS = 1E-9;
const int INF = 1000000000;
const ll INF64 = (ll)1E18;
const double PI = 3.1415926535897932384626433832795;

ll n,m,h;

typedef map<ll,ll>tmap;
typedef set<ll>tset;
set<ll>::iterator it,itt,itlow,itup;
set<ll>::reverse_iterator irev;

typedef struct {char a,b;}tpair;

int main()
{
	#ifdef _DINARISIO
	ifstream cin("input.txt");
	ofstream cout("output.txt");
	#endif

	ll i,k,j,n,lmax=-1,lmin=-1,sum=0;
	char *s=new char[200000];
	tpair t[20];
	cin>>s;cin>>k;
	int cc[500]={0};
	fon(i,k){
		cin>>t[i].a>>t[i].b;
		cc[t[i].a+100]=i+1;
		cc[t[i].b+100]=i+1;
	}
	ll slen=strlen(s),plen=0,ul=0;
	fon(i,slen){
		if(i>0&&cc[s[i]+100]>0&&cc[s[i-1]+100]==cc[s[i]+100]&&(s[i-1]!=s[i]||t[cc[s[i]+100]-1].a==t[cc[s[i]+100]-1].b)){
			if(t[cc[s[i]+100]-1].a==t[cc[s[i]+100]-1].b)ul=1;
			if(plen==0)plen++;
			plen++;
		}
		else{
			if(plen>0){
				if(plen==1)sum+=1;
				else {
					if(ul==0)sum+=plen/2;
					else sum+=plen-1;
				}
				plen=0;ul=0;
			}
		}
	}if(plen>0){
				if(plen==1)sum+=1;
				else {
					if(ul==0)sum+=plen/2;
					else sum+=plen-1;
				}
				plen=0;ul=0;
			}
	cout<<sum;
	re 0;
}


#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <string.h>
#include <math.h>
#include <fstream>
using namespace std;
#define fon(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
#define re return
#define ll long long
const double EPS = 1E-9;
const int INF = 1000000000;
const ll INF64 = (ll)1E18;
const double PI = 3.1415926535897932384626433832795;

ll n,m,h;

typedef map<ll,ll>tmap;
typedef set<ll>tset;
set<ll>::iterator it,itt,itlow,itup;
set<ll>::reverse_iterator irev;

typedef struct {char a,b;}tpair;

int main()
{
	#ifdef _DINARISIO
	ifstream cin("input.txt");
	ofstream cout("output.txt");
	#endif

	ll i,k,j,n,lmax=-1,lmin=-1,sum=0;
	char *s=new char[200000];
	tpair t[20];
	cin>>s;cin>>k;
	int cc[500]={0};
	fon(i,k){
		cin>>t[i].a>>t[i].b;
		cc[t[i].a+100]=i+1;
		cc[t[i].b+100]=i+1;
	}
	ll slen=strlen(s),plen=0,ul=0,alen=0,blen=0;
	fon(i,slen){
		if(i>0&&cc[s[i]+100]==cc[s[i-1]+100]&&cc[s[i-1]+100]>0)
		{
			ul=cc[s[i]+100];
			if(alen+blen==0){
				if(t[cc[s[i]+100]-1].a==s[i-1])alen++;
				else blen++;
			}
			if(t[cc[s[i]+100]-1].a==s[i])alen++;
			else blen++;
		}
		else{
			if(ul>0){
				if(t[ul-1].a==t[ul-1].b){
					sum+=alen+blen-1;
				}else
				{
					if(blen<alen)alen=blen;
					sum+=blen;
				}
				ul=0;alen=0;blen=0;
			}
		}
	}
	if(ul>0){
				if(t[ul-1].a==t[ul-1].b){
					sum+=alen+blen-1;
				}else
				{
					if(blen<alen)alen=blen;
					sum+=blen;
				}
				ul=0;
			}
	cout<<sum;
	re 0;
}


#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <string.h>
#include <math.h>
#include <fstream>
using namespace std;
#define fon(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
#define re return
#define ll long long
const double EPS = 1E-9;
const int INF = 1000000000;
const ll INF64 = (ll)1E18;
const double PI = 3.1415926535897932384626433832795;

ll n,m,h;

typedef map<ll,ll>tmap;
typedef set<ll>tset;
set<ll>::iterator it,itt,itlow,itup;
set<ll>::reverse_iterator irev;

typedef struct {char a,b;}tpair;

int main()
{
	#ifdef _DINARISIO
	ifstream cin("input.txt");
	ofstream cout("output.txt");
	#endif

	ll i,k,j,n,lmax=-1,lmin=-1,sum=0;
	char *s=new char[200000];
	tpair t[20];
	cin>>s;cin>>k;
	int cc[500]={0};
	fon(i,k){
		cin>>t[i].a>>t[i].b;
		cc[t[i].a+100]=i+1;
		cc[t[i].b+100]=i+1;
	}
	ll slen=strlen(s),plen=0;
	fon(i,slen){
		if(i>0&&cc[s[i]+100]>0&&cc[s[i-1]+100]==cc[s[i]+100]&&(s[i-1]!=s[i]||t[cc[s[i]+100]-1].a==t[cc[s[i]+100]-1].b)){
			if(plen==0)plen++;
			plen++;
		}
		else{
			if(plen>0){
				if(plen==1)sum+=1;
				else sum+=plen/2;
				plen=0;
			}
		}
	}if(plen>0){
		if(plen==1)sum+=1;
		else sum+=plen/2;
	}
	cout<<sum;
	re 0;
}


#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <string.h>
#include <math.h>
#include <fstream>
using namespace std;
#define fon(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
#define re return
#define ll long long
const double EPS = 1E-9;
const int INF = 1000000000;
const ll INF64 = (ll)1E18;
const double PI = 3.1415926535897932384626433832795;

ll n,m,h;

typedef map<ll,ll>tmap;
typedef set<ll>tset;
set<ll>::iterator it,itt,itlow,itup;
set<ll>::reverse_iterator irev;

typedef struct {char a,b;}tpair;

int main()
{
	#ifdef _DINARISIO
	ifstream cin("input.txt");
	ofstream cout("output.txt");
	#endif

	ll i,k,j,n,lmax=-1,lmin=-1,sum=0;
	char *s=new char[200000];
	tpair t[20];
	cin>>s;cin>>k;
	int cc[500]={0};
	fon(i,k){
		cin>>t[i].a>>t[i].b;
		cc[t[i].a+100]=i+1;
		cc[t[i].b+100]=i+1;
	}
	ll slen=strlen(s),plen=0,ul=0,alen=0,blen=0;
	fon(i,slen){
		if(i>0&&cc[s[i]+100]==cc[s[i-1]+100]&&cc[s[i-1]+100]>0)
		{
			ul=cc[s[i]+100];
			if(alen+blen==0){
				if(t[cc[s[i]+100]-1].a==s[i-1])alen++;
				else blen++;
			}
			if(t[cc[s[i]+100]-1].a==s[i])alen++;
			else blen++;
		}
		else{
			if(ul>0){
				if(t[ul-1].a==t[ul-1].b){
					sum+=alen+blen-1;
				}else
				{
					if(blen<alen)alen=blen;
					sum+=blen;
				}
				ul=0;
			}
		}
	}
	if(ul>0){
				if(t[ul-1].a==t[ul-1].b){
					sum+=alen+blen-1;
				}else
				{
					if(blen<alen)alen=blen;
					sum+=blen;
				}
				ul=0;
			}
	cout<<sum;
	re 0;
}


#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <string.h>
#include <math.h>
#include <fstream>
using namespace std;
#define fon(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
#define re return
#define ll long long
const double EPS = 1E-9;
const int INF = 1000000000;
const ll INF64 = (ll)1E18;
const double PI = 3.1415926535897932384626433832795;

ll n,m,h;

typedef map<ll,ll>tmap;
typedef set<ll>tset;
set<ll>::iterator it,itt,itlow,itup;
set<ll>::reverse_iterator irev;

typedef struct {char a,b;}tpair;

int main()
{
	#ifdef _DINARISIO
	ifstream cin("input.txt");
	ofstream cout("output.txt");
	#endif

	ll i,k,j,n,lmax=-1,lmin=-1,sum=0;
	char *s=new char[200000];
	tpair t[20];
	cin>>s;cin>>k;
	int cc[500]={0};
	fon(i,k){
		cin>>t[i].a>>t[i].b;
		cc[t[i].a+100]=i+1;
		cc[t[i].b+100]=i+1;
	}
	ll slen=strlen(s),plen=0;
	fon(i,slen){
		if(i>0&&cc[s[i]+100]>0&&cc[s[i-1]+100]==cc[s[i]+100])plen++;
		else{
			if(plen>0){
				if(plen==1)sum+=1;
				else sum+=plen/2;
				plen=0;
			}
		}
	}if(plen>0)sum+=plen/2;
	cout<<sum;
	re 0;
}


// C24.02.12.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#include <cmath>
#include <map>
#include <algorithm>
#include <vector>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <iostream>

using namespace std;

int main()
{
	char s[100003];
	int k;
	char space[2],b,c;
	scanf("%s",&s);
	scanf("%d",&k);
	map<char,char> list;
	char last1,last2;
	for(int i=0;i<k;i++)
	{
		scanf("%1c",&space[0]);
		scanf("%1c",&b);
		scanf("%1c",&c);
		list[b]=c;
		list[c]=b;
	}

	int len=strlen(s);
	int res=0;
	int cnt1=0,cnt2;
	for(int i=0;i<len;i++)
	{
		if(i)
		{
			if((s[i]!=last1)&&(s[i]!=last2))
			{
				res+=min(cnt1,cnt2);
				last1=s[i];
				last2=list[last1];
				cnt1=1;
				cnt2=0;
			}
			else
			{
				if(s[i]==last1)
				{
					cnt1++;
				}
				else
				{
					cnt2++;
				}

				if(i==len-1)
				{
					res+=min(cnt1,cnt2);
				}
			}
		}///if(i)
		else
		{
			last1=s[0];
			last2=list[last1];
			cnt1=1;
			cnt2=0;
		}
	}

	printf("%d",res);
	return 0;
}


//#define _CRT_SECURE_NO_WARNINGS

#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <ctime>
#include <cmath>
#include <math.h>
#include <set>
#include <map>
using namespace std;

#define p system("pause");
#define ct double ttme = clock()
#define p_ct cout << (clock() - ttme) / CLOCKS_PER_SEC << endl
#define all(c) c.begin(),c.end()
#define fft ifstream fin("input.txt"); ofstream fout("output.txt")
#define fft_c FILE *fin, *fout
#define fft_c_open fin = fopen("input.txt","r"); fout = fopen("output.txt","w");
#define fft_c_cl fclose(fin); fclose(fout)

int main()
{
	string s;
	cin >> s;

	int k;
	cin >> k;

	char a[13], b[13];

	for(int i = 0; i < k; i++)
		cin >> a[i] >> b[i];

	vector<int> errors;
	long long int ans = 0;

	for(int i = 0; i < k; i++)
	{
		string error = "";
		bool br = false;
		int aa = 0, bb = 0;

		for(int z = 0; z < s.size(); z++)
		{
			if(s[z] == a[i] || s[z] == b[i])
			{
				if(s[z] == a[i])
					aa++;
				else
					bb++;
			}
			else
				br = true;

			if(br)
			{
				br = false;

				ans += min(aa,bb);
				aa = 0;
				bb = 0;
			}
		}

		if(!br)
			ans += min(aa,bb);
	}

	cout << ans;

	//p;
}


#include<cstdio>

#include<cstdlib>

#include<cstring>

char st[100010];

char map[13][3];

int i,j,n,ans,a,b,len,flag;

int main()

{

         scanf("%s",st);

         len=strlen(st);

         scanf("%d",&n);

         ans=0;

         for (i=0;i<n;i++)

         {

                  scanf("%s",map[i]);

                  flag=a=b=0;

                  for (j=0;j<len;j++)

                  {

                          if (st[j]==map[i][0]|| st[j]==map[i][1])

                          {

                                   if(st[j]==map[i][0])a++;

                                   else b++;

                          }

                          else

                          {

                                   if(a<b)ans+=a;

                                   else ans+=b;

                                   a=b=0;

                          }

                  }

        if (a<b)ans+=a;

        else ans+=b;

//        printf("%d\n",ans);

         }

         printf("%d\n",ans);

         return 0;

}


#include<cstdio>

#include<cstdlib>

#include<cstring>

char st[100010];

char map[13][3];

inti,j,n,ans,a,b,len,flag;

int main()

{

         scanf("%s",st);

         len=strlen(st);

         scanf("%d",&n);

         ans=0;

         for (i=0;i<n;i++)

         {

                  scanf("%s",map[i]);

                  flag=a=b=0;

                  for (j=0;j<len;j++)

                  {

                          if (st[j]==map[i][0]|| st[j]==map[i][1])

                          {

                                   if(st[j]==map[i][0])a++;

                                   else b++;

                          }

                          else

                          {

                                   if(a<b)ans+=a;

                                   else ans+=b;

                                   a=b=0;

                          }

                  }

        if (a<b)ans+=a;

        else ans+=b;

        printf("%d\n",ans);

         }

         printf("%d\n",ans);

         return 0;

}


#include<cstdio>

#include<cstdlib>

#include<cstring>

char st[100010];

char map[13][3];

int i,j,n,ans,a,b,len,flag;

int main()

{

         scanf("%s",st);

         len=strlen(st);

         scanf("%d",&n);

         ans=0;

         for (i=0;i<n;i++)

         {

                  scanf("%s",map[i]);

                  flag=a=b=0;

                  for (j=0;j<len;j++)

                  {

                          if (st[j]==map[i][0]|| st[j]==map[i][1])

                          {

                                   if(st[j]==map[i][0])a++;

                                   else b++;

                          }

                          else

                          {

                                   if(a<b)ans+=a;

                                   else ans+=b;

                                   a=b=0;

                          }

                  }

        if (a<b)ans+=a;

        else ans+=b;

        printf("%d\n",ans);

         }

         printf("%d\n",ans);

         return 0;

}


#include <stdlib.h>
#include <time.h>
#include <vector>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <fstream>
#include <string>
#include <stack>
#include <utility>
#include <map>
#include <set>
#include <queue>

using namespace std;

const int M=100000+10;
typedef long long int ll;

int a[M];

string f[30];

int main()
{
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);

	string s;
	cin>>s;

	int slen=s.length();

	int k;
	cin>>k;
	for (int i=0; i<k; i++)
	{
		cin>>f[i];
	}

	int num=0;
	for (int i=0; i<k; i++)
	{
		for (int j=0; j<slen; j++)
		{
			a[j]=0;
		}

		for (int j=0; j+1<slen; j++)
		{
			if (s[j]==f[i][0] && s[j+1]==f[i][1])
			{
				a[j]=a[j+1]=1;
			}
			if (s[j]==f[i][1] && s[j+1]==f[i][0])
			{
				a[j]=a[j+1]=1;
			}
		}

		int id=0;
		while (id<slen)
		{
			while (id<slen && a[id]==0)
			{
				id++;
			}
			int num1=0;
			while (id<slen && a[id]==1)
			{
				id++;
				num1++;
			}
			num=num+num1/2;
		}

	}

	cout<<num<<endl;

	return 0;

}


#include <stdlib.h>
#include <time.h>
#include <vector>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <fstream>
#include <string>
#include <stack>
#include <utility>
#include <map>
#include <set>
#include <queue>

using namespace std;

const int M=100000+10;
typedef long long int ll;

int a[M];

string f[30];

int main()
{
	freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);

	string s;
	cin>>s;

	int slen=s.length();

	int k;
	cin>>k;
	for (int i=0; i<k; i++)
	{
		cin>>f[i];
	}

	int num=0;
	for (int i=0; i<k; i++)
	{
		int id=0;

		while (id<slen)
		{
			int num1=0;
			int num2=0;
			while (id<slen && s[id]!=f[i][0] && s[id]!=f[i][1])
			{
				id++;
			}

			while (id<slen && (s[id]==f[i][0] || s[id]==f[i][1]) )
			{
				if (s[id]==f[i][0])
				{
					num1++;
				}
				else
				{
					num2++;
				}
				id++;
			}
			num=num+min(num1, num2);
		}

	}

	cout<<num<<endl;

	return 0;

}


#include <stdlib.h>
#include <time.h>
#include <vector>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <fstream>
#include <string>
#include <stack>
#include <utility>
#include <map>
#include <set>
#include <queue>

using namespace std;

const int M=100000+10;
typedef long long int ll;

int a[M];

string f[30];

int main()
{
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);

	string s;
	cin>>s;

	int slen=s.length();

	int k;
	cin>>k;
	for (int i=0; i<k; i++)
	{
		cin>>f[i];
	}

	int num=0;
	for (int i=0; i<k; i++)
	{
		int id=0;

		while (id<slen)
		{
			int num1=0;
			int num2=0;
			while (id<slen && s[id]!=f[i][0] && s[id]!=f[i][1])
			{
				id++;
			}

			while (id<slen && (s[id]==f[i][0] || s[id]==f[i][1]) )
			{
				if (s[id]==f[i][0])
				{
					num1++;
				}
				else
				{
					num2++;
				}
				id++;
			}
			num=num+min(num1, num2);
		}

	}

	cout<<num<<endl;

	return 0;

}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:16777216")
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
#include<cmath>
#include<memory.h>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<iomanip>
#include<string.h>
#include<sstream>
#include<queue>
using namespace std;
#define mp make_pair
#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define vi vector<int>
#define vll vector<ll>
#define vvll vector<vll>
#define matrix vvll
#define vpii vector<pii>
#define ppll pair<pll,pll>
#define PI (ld)acos(-1.0)
#define E 2.71828182845904523536
#define FOR(i,x,y) for(ll i=x;i<=y;++i)
#define REP(i,n) FOR(i,0,n-1)
#define NOMOD 1000000000000000007 // 1e18+7
ll power(ll x, ll y, ll m = NOMOD)
{
	if (!y) return 1;
	if (y % 2) return x*power(x, y - 1, m) % m;
	ll k = power(x, y / 2, m);
	return k*k %m;
}
vvll multy(vvll x, vvll y, ll m = NOMOD)
{
	vvll z;
	z.resize(x.size());
	for (int i = 0; i < z.size(); ++i) z[i].assign(y[0].size(), 0);

	for (int k = 0; k < y.size(); ++k)
	for (int i = 0; i < x.size(); ++i)
	for (int j = 0; j < y[0].size(); ++j)
		z[i][j] = (z[i][j] + x[i][k] * y[k][j]) % m;
	return z;
}
vvll power(vvll x, ll y, ll m = NOMOD)
{
	if (y == 1) return x;
	if (y % 2) return multy(x, power(x, y - 1, m), m);
	vvll k = power(x, y / 2, m);
	return multy(k, k, m);
}
ld power(ld x, ll y)
{
	return exp(log(x)*(ld)y);
}
ll gcd(ll x, ll y)
{
	return y ? gcd(y, x%y) : x;
}
ll gcdex(ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
		x = 0; y = 1;
		return b;
	}
	ll x1, y1;
	ll d = gcdex(b%a, a, x1, y1);
	x = y1 - (b / a) * x1;
	y = x1;
	return d;
}
ll lcm(ll x, ll y, ll m = NOMOD)
{
	return x / gcd(x, y)*y%m;
}
ll phi(ll n) {
	ll result = n;
	for (ll i = 2; i*i <= n; ++i)
	if (n % i == 0) {
		while (n % i == 0)
			n /= i;
		result -= result / i;
	}
	if (n > 1)
		result -= result / n;
	return result;
}
ll rev_el(ll a, ll m)
{
	ll x, y;
	ll g = gcdex(a, m, x, y);
	if (g != 1)
		return power(x, phi(m) - 1, m);
	else {
		x = (x % m + m) % m;
		return x;
	}

}
bool isPrime(ll x)
{
	if (x <= 1) return false;
	for (ll i = 2; i*i <= x; ++i)
	if (x%i == 0) return false;
	return true;
}
int cnt_dig(ll x)
{
	if (!x) return 1;
	int ans = 0;
	while (x)
	{
		x /= 10;
		ans++;
	}
	return ans;
}
int sum_dig(ll x)
{
	ll ans = 0;
	while (x)
	{
		ans += x % 10;
		x /= 10;
	}
	return ans;
}

#define INF 1000000007 // 1e9+7
#define EPS 1e-7
#define MOD 1000000007
#define maxn 100010
#define ALP 26

string s;
char a[maxn];
vector<pair<char, char> > v;

bool ok(char a, char b)
{
	if (a > b) swap(a, b);
	for (int i = 0; i < v.size();++i)
	if (v[i].first == a && v[i].second == b) return false;
	return true;
}

int main()
{

	//ios::sync_with_stdio(0);
	//cin.tie(0);
	getline(cin, s);
	{
		int k;
		scanf("%d\n", &k);
		v.resize(k);
		for (int i = 0; i < k; ++i){
			scanf("%c%c\n", &v[i].first, &v[i].second);
			if (v[i].first>v[i].second) swap(v[i].first, v[i].second);
		}
		int t = 0;
		a[t++] = s[0];
		for (int i = 1; i < s.length();++i)
		if (ok(a[t - 1], s[i])) a[t++] = s[i];
		cout << s.length() - t << endl;
	}
}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:16777216")
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
#include<cmath>
#include<memory.h>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<iomanip>
#include<string.h>
#include<sstream>
#include<queue>
using namespace std;
#define mp make_pair
#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define vi vector<int>
#define vll vector<ll>
#define vvll vector<vll>
#define matrix vvll
#define vpii vector<pii>
#define ppll pair<pll,pll>
#define PI (ld)acos(-1.0)
#define E 2.71828182845904523536
#define FOR(i,x,y) for(ll i=x;i<=y;++i)
#define REP(i,n) FOR(i,0,n-1)
#define NOMOD 1000000000000000007 // 1e18+7
ll power(ll x, ll y, ll m = NOMOD)
{
	if (!y) return 1;
	if (y % 2) return x*power(x, y - 1, m) % m;
	ll k = power(x, y / 2, m);
	return k*k %m;
}
vvll multy(vvll x, vvll y, ll m = NOMOD)
{
	vvll z;
	z.resize(x.size());
	for (int i = 0; i < z.size(); ++i) z[i].assign(y[0].size(), 0);

	for (int k = 0; k < y.size(); ++k)
	for (int i = 0; i < x.size(); ++i)
	for (int j = 0; j < y[0].size(); ++j)
		z[i][j] = (z[i][j] + x[i][k] * y[k][j]) % m;
	return z;
}
vvll power(vvll x, ll y, ll m = NOMOD)
{
	if (y == 1) return x;
	if (y % 2) return multy(x, power(x, y - 1, m), m);
	vvll k = power(x, y / 2, m);
	return multy(k, k, m);
}
ld power(ld x, ll y)
{
	return exp(log(x)*(ld)y);
}
ll gcd(ll x, ll y)
{
	return y ? gcd(y, x%y) : x;
}
ll gcdex(ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
		x = 0; y = 1;
		return b;
	}
	ll x1, y1;
	ll d = gcdex(b%a, a, x1, y1);
	x = y1 - (b / a) * x1;
	y = x1;
	return d;
}
ll lcm(ll x, ll y, ll m = NOMOD)
{
	return x / gcd(x, y)*y%m;
}
ll phi(ll n) {
	ll result = n;
	for (ll i = 2; i*i <= n; ++i)
	if (n % i == 0) {
		while (n % i == 0)
			n /= i;
		result -= result / i;
	}
	if (n > 1)
		result -= result / n;
	return result;
}
ll rev_el(ll a, ll m)
{
	ll x, y;
	ll g = gcdex(a, m, x, y);
	if (g != 1)
		return power(x, phi(m) - 1, m);
	else {
		x = (x % m + m) % m;
		return x;
	}

}
bool isPrime(ll x)
{
	if (x <= 1) return false;
	for (ll i = 2; i*i <= x; ++i)
	if (x%i == 0) return false;
	return true;
}
int cnt_dig(ll x)
{
	if (!x) return 1;
	int ans = 0;
	while (x)
	{
		x /= 10;
		ans++;
	}
	return ans;
}
int sum_dig(ll x)
{
	ll ans = 0;
	while (x)
	{
		ans += x % 10;
		x /= 10;
	}
	return ans;
}

#define INF 1000000007 // 1e9+7
#define EPS 1e-7
#define MOD 1000000007
#define maxn 100010
#define ALP 27

string s;
int dp[maxn][ALP];
int can[ALP][ALP];
vector<pair<char, char> > v;

bool ok(char a, char b)
{
	return can[a - 'a'][b - 'a'];
}

int solve(int p, char last)
{
	if (dp[p][last - 'a'] != -1) return dp[p][last - 'a'];
	if (p == s.length()) return 0;
	int ans = INF;
	if (ok(s[p], last)) ans = min(ans, solve(p + 1, s[p]));
	ans = min(ans, 1 + solve(p + 1, last));
	return dp[p][last - 'a'] = ans;
}

int main()
{

	//ios::sync_with_stdio(0);
	//cin.tie(0);
	getline(cin, s);
	{
		int n = s.length();
		int k;
		scanf("%d\n", &k);
		v.resize(k);
		for (int i = 0; i < ALP;++i)
		for (int j = 0; j < ALP; ++j)
			can[i][j] = 1;
		for (int i = 0; i < k; ++i){
			scanf("%c%c\n", &v[i].first, &v[i].second);
			can[v[i].first - 'a'][v[i].second - 'a'] = 0;
			can[v[i].second - 'a'][v[i].first - 'a'] = 0;
		}
		memset(dp, -1, sizeof(dp));
		cout << solve(0,'z'+1) << endl;
	}
}


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int getRes (int l, int r, char a, char b, string &s) {
    int x=0,y=0;
    for (;l<=r;++l)
		s[l]==a? x++ : y++;
    return min(x,y);
}

int main() {
    string s;
    cin>>s;
    int n;
    cin>>n;
    int k=0,l=s.length();
    for (int i=0;i<n;++i) {
        char a,b;
        cin>>a>>b;
        for (size_t i=0;i<l;)
            if (s[i]==a || s[i]==b) {
                int j=i;
                for(;j<l && (s[j]==a || s[j]==b);++j);
                --j;
                k+=getRes(i,j,a,b,s);
                i=j+1;
            }
            else
                ++i;
    }
    cout<<k;
}


#include<iostream>
//#include<cstdio>
//#include<iomanip>
//#include<cmath>
//#include<algorithm>
#include<map>
//#include<vector>
//#include<set>
//#include<string>
//#include<queue>
//#include <stack>
//#pragma comment(linker, "/STACK:6291456")
using namespace std;
typedef long long ll;

char s[100001];
char s2[100001];
int count[100000];

int len;
int k;

char c1, c2;
map<char,char> bad;

int p, len2, q;
char c;
int num;
int z1, z2, z;

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);

	scanf("%s", &s);
	len = 0;
	while(s[len]) len++;

	scanf("%d", &k);
	for(int i=0; i<k; i++)
	{
		cin >> c1 >> c2;
		bad[c1] = c2;
		bad[c2] = c1;
	}

	p = 1, len2 = 0;
	c = s[0];
	num = 1;
	while(p < len)
	{
		if(s[p] == c) num++;
		else
		{
			s2[len2] = c;
			count[len2] = num;
			c = s[p];
			num = 1;
			len2++;
		}
		p++;
	}

	s2[len2] = c;
	count[len2] = num;
	len2++;
	s2[len2] = 0;

	p = 0;
	z = 0;
	while(p < len2)
	{
		q = p;
		while(q+1 < len2 && bad[s2[q]] == s2[q+1]) q++;
		q++;
		if(q - p > 1)
		{
			z1 = 0, z2 = 0;
			for(int i=p; i<q; i+=2) z1 += count[i];
			for(int i=p+1; i<q; i+=2) z2 += count[i];
			z += min(z1, z2);
		}
		p = q;
	}

	printf("%d", z);

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int MA = 100010;
char buf[MA];
char pa[10];
const int MACH = 300;
int forbid[MACH];
int occ[MACH];

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
    while (scanf("%s", buf) != EOF) {
        for (int i = 0; i < MACH; ++i) {
            forbid[i] = MA;
            occ[i] = 0;
        }

        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; ++i) {
            scanf("%s", &pa);
            forbid[pa[0]] = pa[1];
            forbid[pa[1]] = pa[0];
        }
        for (int i = 0; buf[i] != '\0'; ++i) {
            //left
            if (i > 0 && buf[i - 1] == forbid[buf[i]]) {
                ++occ[buf[i]];
                continue;
            }
            //right
            if (buf[i + 1] != '\0' && buf[i + 1] == forbid[buf[i]]) {
                ++occ[buf[i]];
                continue;
            }
        }
        int sum = 0;
        for (int i = 0; i < MACH; ++i) {
            if (forbid[i] == MA)
                continue;

            char x = i;
            char y = forbid[i];
            int t = min(occ[x], occ[y]);
            sum += t;
        }
        printf("%d\n", sum/2);
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int MA = 100010;
char buf[MA];
char pa[10];
int forbid[128];
int occ[128];

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
    while (scanf("%s", buf) != EOF) {
        memset(forbid, -1, sizeof(forbid));
        memset(occ, 0, sizeof(occ));

        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; ++i) {
            scanf("%s", &pa);
            forbid[pa[0]] = pa[1];
            forbid[pa[1]] = pa[0];
        }
        for (int i = 0; buf[i] != '\0'; ++i) {
            //left
            if (i > 0 && buf[i - 1] == forbid[buf[i]]) {
                ++occ[buf[i]];
                continue;
            }
            //right
            if (buf[i + 1] != '\0' && buf[i + 1] == forbid[buf[i]]) {
                ++occ[buf[i]];
                continue;
            }
        }
        int sum = 0;
        for (int i = 0; i < 128; ++i) {
            if (forbid[i] == -1)
                continue;

            char x = i;
            char y = forbid[i];
            int t = min(occ[x], occ[y]);
            sum += t;
        }
        cout << sum/2 << endl;
    }
    return 0;
}


#include <memory.h>
#include <stdio.h>
#include <assert.h>

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <algorithm>

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector< pii > vpii;

#define all(x) (x).begin(), (x).end()
#define clear(x) (x).clear()
#define clearm(x) memset((x), 0, sizeof(x))

#define FOR(i, b, e) for (int i = (b); i < (e); ++i)
#define RE(i, n) FOR(i, 0, (n))

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
    string str;
    int n;
    while (cin >> str >> n) {
        int sum = 0;
        for (int i = 0; i < n; ++i) {
            char x,y;
            cin >> x >> y;
            for (int j = 0; j < str.size(); ++j) {
                if (str[j] == x || str[j] == y) {
                    int cx = 0;
                    int cy = 0;
                    int k = j;
                    for (; k < str.size(); ++k) {
                        if (str[k] == x) {
                            ++cx;
                        } else if (str[k] == y) {
                            ++cy;
                        } else {
                            break;
                        }
                    }
                    j = k;
                    sum += min(cx, cy);
                }
            }
        }
        cout << sum << endl;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int MA = 100010;
char buf[MA];
char pa[10];
const int MACH = 300;
int forbid[MACH];
int occ[MACH];

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
    while (scanf("%s", buf) != EOF) {
        memset(forbid, -1, sizeof(forbid));
        memset(occ, 0, sizeof(occ));

        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; ++i) {
            scanf("%s", &pa);
            forbid[pa[0]] = pa[1];
            forbid[pa[1]] = pa[0];
        }
        for (int i = 0; buf[i] != '\0'; ++i) {
            //left
            if (i > 0 && buf[i - 1] == forbid[buf[i]]) {
                ++occ[buf[i]];
                continue;
            }
            //right
            if (buf[i + 1] != '\0' && buf[i + 1] == forbid[buf[i]]) {
                ++occ[buf[i]];
                continue;
            }
        }
        int sum = 0;
        for (int i = 0; i < MACH; ++i) {
            if (forbid[i] == -1)
                continue;

            char x = i;
            char y = forbid[i];
            int t = min(occ[x], occ[y]);
            sum += t;
        }
        printf("%d\n", sum/2);
    }
    return 0;
}


#include <iostream>
#include <math.h>
#include <string>
using namespace std;

struct str{
	char a,b;
};

int main(){
	int k,ans=0,a=1,b=0;
	str m[13];
	string s;
	cin >> s >> k;
	s+='!';
	for(int i=0; i<k; i++)
		cin >> m[i].a >> m[i].b;

	bool founded=false,odd=true;
	char pre=s[0];
	for (int i=1; i<s.length(); i++){
		founded=false;
		for (int j=0; j<k; j++)
			if ((s[i]==m[j].a && s[i-1]==m[j].b) ||
				(s[i]==m[j].b && s[i-1]==m[j].a)){
				odd=!odd;
				if (odd)
					a++;
				else
					b++;
				founded=true;
			}
		if (s[i]==pre)
			if (odd)
				a++;
			else
				b++;
		else {
			pre=s[i];
			if (!founded){
				ans+=min(a,b);
				a=1;
				b=0;
				odd=true;
			}
		}
	}

	cout << ans;
	return 0;
}


#include <iostream>
#include <math.h>
#include <string>
using namespace std;

struct str{
	char a,b;
};

int main(){
	int k,ans=0;
	str m[13];
	string s;
	cin >> s >> k;
	for(int i=0; i<k; i++)
		cin >> m[i].a >> m[i].b;
	for (int i=1; i<s.length(); i++)
		for (int j=0; j<k; j++)
			if ((s[i]==m[j].a && s[i-1]==m[j].b) ||
				(s[i]==m[j].b && s[i-1]==m[j].a)){
				s[i]='!';
				ans++;
			}

	cout << ans;
	return 0;
}


#include <iostream>
#include <math.h>
#include <string>
using namespace std;

struct str{
	char a,b;
};

int main(){
	int k,ans=0,a=1,b=0;
	str m[13];
	string s;
	cin >> s >> k;
	s+='!';
	for(int i=0; i<k; i++)
		cin >> m[i].a >> m[i].b;

	bool founded=false,odd=true;
	char pre=s[0];
	for (int i=1; i<s.length(); i++){
		founded=false;
		for (int j=0; j<k; j++)
			if ((s[i]==m[j].a && s[i-1]==m[j].b) ||
				(s[i]==m[j].b && s[i-1]==m[j].a)){
				odd=!odd;
				if (odd)
					a++;
				else
					b++;
				founded=true;
			}
		if (s[i]==pre)
			if (odd)
				a++;
			else
				b++;
		else {
			pre=s[i];
			if (!founded){
				ans+=min(a,b);
				a=1;
				b=0;
			}
		}
	}

	cout << ans;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <queue>
#include <deque>
#include <cmath>

#pragma comment(linker, "/STACK:128000000")

using namespace std;

char s[100001] = {0};

map <char, char> a;

int main(){
    int x, y, z, n, m, p, v, l, r, t, i, k, j, fl;
    scanf("%s", &s);
    l = strlen(s);
    scanf("%i", &n);
    char c1, c2;
    for(i = 1; i <= n; i++) {
        getchar();
        c1 = getchar();
        c2 = getchar();
        a[c1] = c2;
        a[c2] = c1;
    }
    t = 0;
    for(i = 0; i < l - 1; i++) {
        if(a[s[i]] != s[i+1]) continue;
        j = i;
        k = i+1;
        p = x = 0;
        while(j >= 0 && (s[j] == s[i] || s[j] == s[i+1])) {
            j--;
        }

        while(k < l && (s[k] == s[i+1] || s[k] == s[i])) {
            k++;
        }

        for(y = j + 1; y <= k - 1; y++)
            if(s[y] == s[i]) p++;
            else x++;
        t += min(p, x);
        i = k;
    }

    printf("%i\n", t);

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <queue>
#include <deque>
#include <cmath>

#pragma comment(linker, "/STACK:128000000")

using namespace std;

char s[100001] = {0};

map <char, char> a;

int main(){
    int x, y, z, n, m, p, v, l, r, t, i, k, j, fl;
    scanf("%s", &s);
    l = strlen(s);
    scanf("%i", &n);
    char c1, c2;
    for(i = 1; i <= n; i++) {
        cin >> c1 >> c2;
        a[c1] = c2;
        a[c2] = c1;
    }
    t = 0;
    for(i = 0; i < l - 1; i++) {
        if(a[s[i]] != s[i+1]) continue;
        j = i;
        k = i+1;
        p = x = 0;
        while(j >= 0 && (s[j] == s[i] || s[j] == s[i+1])) {
            j--;
        }

        while(k < l && (s[k] == s[i+1] || s[k] == s[i])) {
            k++;
        }

        for(y = j + 1; y <= k - 1; y++)
            if(s[y] == s[i]) p++;
            else x++;
        t += min(p, x);
        i = k - 1;
    }

    printf("%i\n", t);

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <queue>
#include <deque>
#include <cmath>

#pragma comment(linker, "/STACK:128000000")

using namespace std;

char s[100001] = {0};

map <char, char> a;

int main(){
    int x, y, z, n, m, p, v, l, r, t, i, k, j, fl;
    scanf("%s", &s);
    l = strlen(s);
    scanf("%i", &n);
    char c1, c2;
    for(i = 1; i <= n; i++) {
        getchar();
        c1 = getchar();
        c2 = getchar();
        a[c1] = c2;
        a[c2] = c1;
    }
    t = 0;
    for(i = 0; i < l - 1; i++) {
        if(a[s[i]] != s[i+1]) continue;
        j = i;
        k = i+1;
        p = x = 0;
        while(j >= 0 && s[j] == s[i]) {
            p++;
            j--;
        }
        while(k < l && s[k] == s[i+1]) {
            x++;
            k++;
        }
        t += min(p, x);
        i = k;
    }

    printf("%i\n", t);

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <queue>
#include <deque>
#include <cmath>

#pragma comment(linker, "/STACK:128000000")

using namespace std;

char s[100001] = {0};

map <char, char> a;

int main(){
    int x, y, z, n, m, p, v, l, r, t, i, k, j, fl;
    scanf("%s", &s);
    l = strlen(s);
    scanf("%i", &n);
    char c1, c2;
    for(i = 1; i <= n; i++) {
        cin >> c1 >> c2;
        a[c1] = c2;
        a[c2] = c1;
    }
    t = 0;
    for(i = 0; i < l - 1; i++) {
        if(a[s[i]] != s[i+1]) continue;
        j = i;
        k = i+1;
        p = x = 0;
        while(j >= 0 && (s[j] == s[i] || s[j] == s[i+1])) {
            j--;
        }

        while(k < l && (s[k] == s[i+1] || s[k] == s[i])) {
            k++;
        }

        for(y = j + 1; y <= k - 1; y++)
            if(s[y] == s[i]) p++;
            else x++;
        t += min(p, x);
        i = k;
    }

    printf("%i\n", t);

    return 0;
}


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

int main(){
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        vector<int> arr(n);
        for(int i=0;i<n;i++){
            cin >> arr[i];
        }
        reverse(arr.begin(),arr.end());
        for(int i=0;i<n;i++){
            cout << arr[i] << " ";
        }
        cout << endl;
    }
}


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

int main(){
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        int arr[n];
        for(int i=0;i<n;i++){
            cin >> arr[i];
        }
        swap(arr[0],arr[1]);
        for(int i=0;i<n;i++){
            cout << arr[i] << " ";
        }
        cout << endl;
    }
}


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

int main(){
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        int arr[n];
        for(int i=0;i<n;i++){
            cin >> arr[i];
        }
        sort(arr,arr+n);
        for(int i=0;i<n;i++){
            cout << arr[i] << " ";
        }
        cout << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
  ll t;
  cin>>t;
  while(t--)
  {
    ll n;
    cin>>n;
    ll a[n+1];
    for(ll i=0;i<n;i++)
    {
     cin>>a[i];
    }
    if(n%2)
    {
        ll r=a[n-1];
        ll x=n/2;
        for(ll i=x;i<n;i++)
        {

                cout<<a[i]<<" ";

        }
        for(ll i=0;i<x;i++)
        {

                cout<<a[i]<<" ";
        }
        cout<<endl;

    }
    else
    {
        for(ll i=0;i<n;i++)
        {
            if(i%2)
            {
                cout<<a[i-1]<<" ";
            }
            else
            {
                cout<<a[i+1]<<" ";
            }
        }
        cout<<endl;
    }
  }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
  ll t;
  cin>>t;
  while(t--)
  {
    ll n;
    cin>>n;
    ll a[n+1];
    for(ll i=0;i<n;i++)
    {
     cin>>a[i];
    }
    if(n%2)
    {
         for(ll i=0;i<n-1;i++)
        {
            if(i%2)
            {
                cout<<a[i-1]<<" ";
            }
            else
            {
                cout<<a[i+1]<<" ";
            }
        }
        cout<<a[n-1]<<endl;
    }
    else
    {
        for(ll i=0;i<n;i++)
        {
            if(i%2)
            {
                cout<<a[i-1]<<" ";
            }
            else
            {
                cout<<a[i+1]<<" ";
            }
        }
        cout<<endl;
    }
  }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
  ll t;
  cin>>t;
  while(t--)
  {
    ll n;
    cin>>n;
    ll a[n+1];
    for(ll i=0;i<n;i++)
    {
     cin>>a[i];
    }
    cout<<a[1]<<" "<<a[0]<<" ";
    for(ll i=2;i<n;i++)
    {
        cout<<a[i]<<" ";
    }
    cout<<endl;

}
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
  ll t;
  cin>>t;
  while(t--)
  {
    ll n;
    cin>>n;
    ll a[n+1];
    for(ll i=0;i<n;i++)
    {
     cin>>a[i];
    }
    if(n%2)
    {
        ll r=a[n-1];
        ll x=n/2;
        for(ll i=0;i<x;i++)
        {
            if(i%2)
            {
                cout<<a[i-1]<<" ";
            }
            else
            {
                cout<<a[i+1]<<" ";
            }
        }
        cout<<r<<" ";
        for(ll i=x;i<n-1;i++)
        {
                        if(i%2)
            {
                cout<<a[i-1]<<" ";
            }
            else
            {
                cout<<a[i+1]<<" ";
            }
        }
        cout<<endl;
    }
    else
    {
        for(ll i=0;i<n;i++)
        {
            if(i%2)
            {
                cout<<a[i-1]<<" ";
            }
            else
            {
                cout<<a[i+1]<<" ";
            }
        }
        cout<<endl;
    }
  }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
  ll t;
  cin>>t;
  while(t--)
  {
    ll n;
    cin>>n;
    ll a[n+1];
    for(ll i=0;i<n;i++)
    {
     cin>>a[i];
    }
       for(ll i=n-1;i>=0;i--)
       {
           cout<<a[i]<<" ";
       }
       cout<<endl;

  }

}


#include <bits/stdc++.h>
#define tmg ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define mod 1000000007
#define endl "\n"
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = 1e5+5 , M = 505;
using namespace std;

int main() {
    tmg
    int t; cin >> t;
    while (t--) {
        int n;
        cin >> n;
        int arr[n];
        for (int i = 0 ;i<n ; i++) cin >>arr[i];
        swap(arr[0] , arr[1]);
        for (int i = 0 ;i<n ; i++) cout << arr[i] << " ";
        cout << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define tmg ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define mod 1000000007
#define endl "\n"
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = 1e5+5 , M = 505;
using namespace std;

int main() {
    tmg
    int t; cin >> t;
    while (t--) {
        int n; cin >> n;
        int arr[n];
        for (int i = 0 ; i<n ; i++) {
            cin >> arr[i];
        }
        if (n%2==0) {
            for (int i = 0 ; i<n ; i+=2) {
                swap(arr[i] , arr[i+1]);
            }
            for (int i = 0 ; i<n ; i+=2) {
                cout << arr[i] << " ";
            }
            cout << endl;
            continue;
        }
        else {
            for (int i = n/2 ; i<n ; i++) {
                cout << arr[i] << " ";
            }
            for (int i = 0 ; i<n/2 ; i++) {
                cout << arr[i] << " ";
            }
            cout << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#define tmg ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define mod 1000000007
#define endl "\n"
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = 1e5+5 , M = 505;
using namespace std;

int main() {
    tmg
    int t; cin >> t;
    while (t--) {
        int n; cin >> n;
        int arr[n];
        for (int i = 0 ; i<n ; i++) {
            cin >> arr[i];
        }
        for (int i = n-1 ; i>=0 ; i--) {
            cout << arr[i] << " ";
        }
        cout << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define tmg ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define mod 1000000007
#define endl "\n"
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = 1e5+5 , M = 505;
using namespace std;

int main() {
    tmg
    int t; cin >> t;
    while (t--) {
        int n; cin >> n;
        int arr[n];
        for (int i = 0 ; i<n ; i++) {
            cin >> arr[i];
        }
        if (n%2==0) {
            for (int i = 0 ; i<n ; i+=2) {
                swap(arr[i] , arr[i+1]);
            }
            for (int i = 0 ; i<n ; i++) {
                cout << arr[i] << " ";
            }
            cout << endl;
            continue;
        }
        else {
            for (int i = n/2 ; i<n ; i++) {
                cout << arr[i] << " ";
            }
            for (int i = 0 ; i<n/2 ; i++) {
                cout << arr[i] << " ";
            }
            cout << endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>

#define Rushia_mywife ios::sync_with_stdio(0);cin.tie(0);
#define rep(i,head,n) for(int i=(head);i<n;i++)
#define F first
#define S second
#define FF first.first
#define FS first.second
#define SF second.first
#define SS second.second
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define all(x) (x).begin(),(x).end()
#define SZ(x) (int)(x).size()
#define mem(x,i) memset((x),(i),sizeof((x)))
#define odd(x) ((x)&1)

using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<long long,long long>;
using ld = long double;

mt19937 mt_rand(time(0));
const int mod = 1000000007;
const int hnum = 998244353;
const ld PI = acos(-1);

//#define int long long //int != long long

int n;
int a[1010];
int cnt[1010];

void solve(){
    cin >> n;
    rep(i,0,n)
        cin >> a[i];
    rep(i,0,n-1){
        int x = a[i]+a[n-1];
        rep(j,i,n-1){
            if(a[j]+a[j+1]==x){
                rep(k,i+1,n)
                    cout << a[k] << ' ';
                rep(k,0,i+1)
                    cout << a[k] << ' ';
                cout << '\n';
                return;
            }
        }
    }
    cout << "Loser\n";
}

signed main(){
    Rushia_mywife
    int t; cin >> t;
    while(t--)solve();
}


#include<bits/stdc++.h>

#define Rushia_mywife ios::sync_with_stdio(0);cin.tie(0);
#define rep(i,head,n) for(int i=(head);i<n;i++)
#define F first
#define S second
#define FF first.first
#define FS first.second
#define SF second.first
#define SS second.second
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define all(x) (x).begin(),(x).end()
#define SZ(x) (int)(x).size()
#define mem(x,i) memset((x),(i),sizeof((x)))
#define odd(x) ((x)&1)

using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<long long,long long>;
using ld = long double;

mt19937 mt_rand(time(0));
const int mod = 1000000007;
const int hnum = 998244353;
const ld PI = acos(-1);

//#define int long long //int != long long

int n;
int a[1010];
int cnt[1010];

void solve(){
    cin >> n;
    rep(i,0,n)
        cin >> a[i];
    rep(i,0,n-1){
        int x = a[i]+a[n-1];
        rep(j,i,n-1){
            if(a[j]+a[j+1]==x){
                rep(k,i+1,n)
                    cout << a[k] << ' ';
                for(int k=i;k>=0;k--)
                    cout << a[k] << ' ';
                cout << '\n';
                return;
            }
        }
    }
    cout << "Loser\n";
}

signed main(){
    Rushia_mywife
    int t; cin >> t;
    while(t--)solve();
}


#include<bits/stdc++.h>

#define Rushia_mywife ios::sync_with_stdio(0);cin.tie(0);
#define rep(i,head,n) for(int i=(head);i<n;i++)
#define F first
#define S second
#define FF first.first
#define FS first.second
#define SF second.first
#define SS second.second
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define all(x) (x).begin(),(x).end()
#define SZ(x) (int)(x).size()
#define mem(x,i) memset((x),(i),sizeof((x)))
#define odd(x) ((x)&1)

using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<long long,long long>;
using ld = long double;

mt19937 mt_rand(time(0));
const int mod = 1000000007;
const int hnum = 998244353;
const ld PI = acos(-1);

//#define int long long //int != long long

int n;
int a[1010];
int cnt[1010];

void solve(){
    cin >> n;
    rep(i,0,n)
        cin >> a[i];
    rep(i,0,n-1){
        int x = a[i]+a[n-1];
        rep(j,i,n-1){
            if(a[j]+a[j+1]==x){
                rep(k,i+1,n)
                    cout << a[k] << ' ';
                for(int k=i;k>=0;k--)
                    cout << a[k] << ' ';
                return;
            }
        }
    }
    cout << "Loser\n";
}

signed main(){
    Rushia_mywife
    int t; cin >> t;
    while(t--)solve();
}


#include<bits/stdc++.h>

#define Rushia_mywife ios::sync_with_stdio(0);cin.tie(0);
#define rep(i,head,n) for(int i=(head);i<n;i++)
#define F first
#define S second
#define FF first.first
#define FS first.second
#define SF second.first
#define SS second.second
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define all(x) (x).begin(),(x).end()
#define SZ(x) (int)(x).size()
#define mem(x,i) memset((x),(i),sizeof((x)))
#define odd(x) ((x)&1)

using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<long long,long long>;
using ld = long double;

mt19937 mt_rand(time(0));
const int mod = 1000000007;
const int hnum = 998244353;
const ld PI = acos(-1);

//#define int long long //int != long long

int n;
int a[1010];
int cnt[1010];

void solve(){
    cin >> n;
    rep(i,0,n)
        cin >> a[i];
    rep(i,0,n-1){
        int x = a[i]+a[n-1];
            if(a[i]+a[i+1]==x){
                rep(k,i+1,n)
                    cout << a[k] << ' ';
                for(int k=i;k>=0;k--)
                    cout << a[k] << ' ';
                cout << '\n';
                return;
            }
    }
    cout << "Loser\n";
}

signed main(){
    Rushia_mywife
    int t; cin >> t;
    while(t--)solve();
}


#include<bits/stdc++.h>

#define Rushia_mywife ios::sync_with_stdio(0);cin.tie(0);
#define rep(i,head,n) for(int i=(head);i<n;i++)
#define F first
#define S second
#define FF first.first
#define FS first.second
#define SF second.first
#define SS second.second
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define all(x) (x).begin(),(x).end()
#define SZ(x) (int)(x).size()
#define mem(x,i) memset((x),(i),sizeof((x)))
#define odd(x) ((x)&1)

using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<long long,long long>;
using ld = long double;

mt19937 mt_rand(time(0));
const int mod = 1000000007;
const int hnum = 998244353;
const ld PI = acos(-1);

//#define int long long //int != long long

int n;
int a[1010];
int cnt[1010];

void solve(){
    cin >> n;
    rep(i,0,n)
        cin >> a[i];
    rep(i,0,n-1){
        int x = a[i]+a[n-1];
        rep(j,i,n-1){
            if(a[j]+a[j+1]==x){
                rep(k,i+1,n)
                    cout << a[k] << ' ';
                rep(k,0,i+1)
                    cout << a[i] << ' ';
                cout << '\n';
                return;
            }
        }
    }
    cout << "Loser\n";
}

signed main(){
    Rushia_mywife
    int t; cin >> t;
    while(t--)solve();
}


#include <bits/stdc++.h>

using namespace std;
void print(vector<int> v){
 for(int i = 0;i<v.size();i++)
    cout<<v[i]<<" ";

    cout<<endl;
}
int main()
{
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int* arr = new int[n];
        for(int i = 0;i<n;i++)
            cin>>arr[i];
        vector<int> ans;
        if(n&1){
            int mid = n/2;
            ans.push_back(arr[mid]);
            int i = mid + 1;
            while(i<n){
                ans.push_back(arr[i]);
                ans.push_back(arr[i+1]);
                i = i + 2;
            }
            i = 0;
            while(i<mid){
                ans.push_back(arr[i]);
                ans.push_back(arr[i+1]);
                i = i + 2;
            }
        }
        else{
            int i = 1;
            while(i<n){
                ans.push_back(arr[i]);
                ans.push_back(arr[i-1]);
                i = i + 2;
            }
        }
        print(ans);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
void print(vector<int> v){
 for(int i = 0;i<v.size();i++)
    cout<<v[i]<<" ";

    cout<<endl;
}
int main()
{
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int* arr = new int[n];
        for(int i = 0;i<n;i++)
            cin>>arr[i];
        vector<int> ans;
        if(n&1){
            int mid = n/2;
            ans.push_back(arr[mid]);
            int i = mid + 1;
            while(i<n-1){
                ans.push_back(arr[i]);
                ans.push_back(arr[i+1]);
                i = i + 2;
            }
            i = 0;
            while(i<mid-1){
                ans.push_back(arr[i]);
                ans.push_back(arr[i+1]);
                i = i + 2;
            }
        }
        else{
            int i = 1;
            while(i<n){
                ans.push_back(arr[i]);
                ans.push_back(arr[i-1]);
                i = i + 2;
            }
        }
        print(ans);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
void print(vector<int> v){
 for(int i = 0;i<v.size();i++)
    cout<<v[i]<<" ";

    cout<<endl;
}
int main()
{
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int* arr = new int[n];
        for(int i = 0;i<n;i++)
            cin>>arr[i];
        vector<int> ans;

           int i = n-1;
           while(i>=0){
            ans.push_back(arr[i]);
            i--;
           }

        print(ans);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
void print(vector<int> v){
 for(int i = 0;i<v.size();i++)
    cout<<v[i]<<" ";

    cout<<endl;
}
int main()
{
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int* arr = new int[n];
        for(int i = 0;i<n;i++)
            cin>>arr[i];
        vector<int> ans;
        if(n&1){
           int i = n-1;
           while(i>=0){
            ans.push_back(arr[i]);
            i--;
           }
        }
        else{
            int i = 1;
            while(i<n){
                ans.push_back(arr[i]);
                ans.push_back(arr[i-1]);
                i = i + 2;
            }
        }
        print(ans);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long int ll;

int main() {
	// your code goes here
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,i;
        cin>>n;
        ll a[n];
        for(i=0;i<n;i++)
            cin>>a[i];
        for(i=n-1;i>=0;i--)
            cout<<a[i]<<" ";
        cout<<"\n";
    }

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long int ll;

int main() {
	// your code goes here
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,i;
        cin>>n;
        ll a[n];
        for(i=0;i<n;i++)
            cin>>a[i];
        if(n%2==0)
        {
            for(i=0;i<n;i=i+2)
            {
                cout<<a[i+1]<<" "<<a[i]<<" ";
            }
            cout<<"\n";
        }
        else
        {
            for(i=(n/2);i<n;i++)
                cout<<a[i]<<" ";
            for(i=0;i<(n/2);i++)
                cout<<a[i]<<" ";
            cout<<"\n";
        }
    }

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long int ll;

int main() {
	// your code goes here
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,i;
        cin>>n;
        ll a[n],b[n],c[n],p,pv,f=0,j,k,sumn[n-1],sum[n-1];
        for(i=0;i<n;i++)
        {
            cin>>a[i];
            if(i>0)
                sum[i-1]=a[i]+a[i-1];
            b[i]=a[i];
        }
        sort(sum,sum+(n-1));
        sort(b, b+ n);

        // Find all possible permutations

        do {
            ll f1=0,f2=1;
            for(i=0;i<n-1;i++)
                sumn[i]=0;
            for(i=0;i<n;i++)
                if(i>0)
                    sumn[i-1]=b[i]+b[i-1];
            sort(sumn,sumn+(n-1));
            for(i=0;i<n;i++)
            {
               if(b[i]!=a[i])
                    f1=1;
                if(i>0)
                {
                    if(sumn[i-1]!=sum[i-1])
                        f2=0;
                }
            }
            if(f1==1 && f2==1)
                break;
        } while (next_permutation(b, b + n));
        for(i=0;i<n;i++)
            cout<<b[i]<<" ";
        cout<<"\n";
    }

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long int ll;

int main() {
	// your code goes here
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,i;
        cin>>n;
        ll a[n],b[n],c[n],p,pv,f=0,j,k,sumn[n-1],sum[n-1];
        for(i=0;i<n;i++)
        {
            cin>>a[i];
            if(i>0)
                sum[i-1]=a[i]+a[i-1];
            b[i]=a[i];
        }
        sort(sum,sum+(n-1));
        sort(b, b+ n);

        // Find all possible permutations

        do {
            ll f1=0,f2=1;
            for(i=0;i<n;i++)
                if(i>0)
                    sumn[i-1]=b[i]+b[i-1];
            sort(sumn,sumn+(n-1));
            for(i=0;i<n;i++)
            {
               if(b[i]!=a[i])
                    f1=1;
                if(i>0)
                {
                    if(sumn[i-1]!=sum[i-1])
                        f2=0;
                }
            }
            if(f1==1 && f2==1)
                break;
        } while (next_permutation(b, b + n));
        for(i=0;i<n;i++)
            cout<<b[i]<<" ";
        cout<<"\n";
    }

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int;
int main(){
   ll t;
    cin>>t;
    while(t--){
    ll n,i;
    cin>>n;
    ll a[n];
    for(i=0;i<n;i++)
    cin>>a[i];
    for(i=0;i<n;i++)
    cout<<a[n-i-1]<<" ";
        cout<<'\n';
    }
}


#include<bits/stdc++.h>
using namespace std;
int main(){
   long long int t;
    cin>>t;
    while(t--){
    long long int n,i;
    cin>>n;
    long long int a[n];
    for(i=0;i<n;i++)
    cin>>a[i];
    for(i=0;i<n;i++)
    cout<<a[n-i-1]<<" ";
        cout<<'\n';
    }
}


#include<bits/stdc++.h>
using namespace std;
define ll long long;
int main(){
   ll int t;
    cin>>t;
    while(t--){
    ll int n,i;
    cin>>n;
    long long int a[n];
    for(i=0;i<n;i++)
    cin>>a[i];
    for(i=0;i<n;i++)
    cout<<a[n-i-1]<<" ";
        cout<<'\n';
    }
}


#include<bits/stdc++.h>
using namespace std;
define ll long long int;
int main(){
   ll t;
    cin>>t;
    while(t--){
    ll n,i;
    cin>>n;
    ll a[n];
    for(i=0;i<n;i++)
    cin>>a[i];
    for(i=0;i<n;i++)
    cout<<a[n-i-1]<<" ";
        cout<<'\n';
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
   ll int t;
    cin>>t;
    while(t--){
    ll int n,i;
    cin>>n;
    long long int a[n];
    for(i=0;i<n;i++)
    cin>>a[i];
    for(i=0;i<n;i++)
    cout<<a[n-i-1]<<" ";
        cout<<'\n';
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long;
int main(){
   ll int t;
    cin>>t;
    while(t--){
    ll int n,i;
    cin>>n;
    long long int a[n];
    for(i=0;i<n;i++)
    cin>>a[i];
    for(i=0;i<n;i++)
    cout<<a[n-i-1]<<" ";
        cout<<'\n';
    }
}


#include<bits/stdc++.h>
using namespace std;
int main(){
   long long int t;
    cin>>t;
    while(t--){
    long long int n;
    cin>>n;
    long long int a[n];
    for(i=0;i<n;i++)
    cin>>a[i];
    for(i=0;i<n;i++)
    cout<<a[n-i-1]<<" ";
        cout<<'\n';
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main(){
   ll t;
    cin>>t;
    while(t--){
    ll n,i;
    cin>>n;
    ll a[n];
    for(i=0;i<n;i++)
    cin>>a[i];
    for(i=0;i<n;i++)
    cout<<a[n-i-1]<<" ";
        cout<<'\n';
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int;
int main(){
   ll int t;
    cin>>t;
    while(t--){
    ll int n,i;
    cin>>n;
    long long int a[n];
    for(i=0;i<n;i++)
    cin>>a[i];
    for(i=0;i<n;i++)
    cout<<a[n-i-1]<<" ";
        cout<<'\n';
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef ll long long;
int main(){
   ll int t;
    cin>>t;
    while(t--){
    ll int n,i;
    cin>>n;
    long long int a[n];
    for(i=0;i<n;i++)
    cin>>a[i];
    for(i=0;i<n;i++)
    cout<<a[n-i-1]<<" ";
        cout<<'\n';
    }
}


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

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int arr[n];
        for(int i=0;i<n;i++)
        {
            cin>>arr[i];
        }

        for(int i=n-1;i>=0;i--)
        {
            cin>>arr[i]<<" ";
        }
        cout<<endl;
    }
	return 0;
}


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

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int arr[n];
        for(int i=0;i<n;i++)
        {
            cin>>arr[i];
        }

        for(int i=n-1;i>=0;i--)
        {
            cout<<arr[i]<<" ";
        }
        cout<<endl;

    }

	return 0;
}


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

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
	    int n;
	    cin>>n;
	    int arr[n];
	    for(int i=0;i<n;i++)
	    {
	        cin>>arr[i];
	    }
	    for(int i=0;i<n;i++)
	    {
	        cout<<arr[i]<<" ";
	    }
	    cout<<endl;
	}
	return 0;
}


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

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int arr[n];
        for(int i=0;i<n;i++)
        {
            cin>>arr[i];
        }

        if(n%2==0)
        {
            for(int i=n-1;i>=0;i--)
            {
                cout<<arr[i]<<" ";
            }
        }
        else
        {
            for(int i=n/2;i<n;i++)
            {
                cout<<arr[i]<<" ";
            }
            for(int i=0;i<n/2;i++)
            {
                cout<< arr[i]<<" ";
            }
        }
        cout<<endl;
    }

	return 0;
}


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

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int arr[n];
        for(int i=0;i<n;i++)
        {
            cin>>arr[i];
        }

        for(int i=n-1;i>=0;i--)
        {
            cout<<arr[i]<<" ";
        }
        cout<<endl;
    }
	cout<<"GfG!";
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
typedef pair<lli, lli> pii;
#define ff first
#define ss second
#define MOD 1000000007
#define pb push_back
#define EPS 1e-6
const lli MAX = 1e9 + 7;

int main()
{
    fast;
    int t;
    cin >> t;
    while (t--)
    {
        lli n;
        cin >> n;
        vector<int> v(n);

        for (int i = 0; i < n; i++)
        {
            cin >> v[i];
        }
        reverse(v.begin(), v.end());

        for (int i = 0; i < n; i++)
            cout << v[i] << " ";
        cout << "\n";
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
typedef pair<lli, lli> pii;
#define ff first
#define ss second
#define MOD 1000000007
#define pb push_back
#define EPS 1e-6
const lli MAX = 1e9 + 7;

int main()
{
    fast;
    int t;
    cin >> t;
    while (t--)
    {
        lli n;
        cin >> n;
        vector<int> v(n);

        for (int i = 0; i < n; i++)
            cin >> v[i];
        if (n > 2)
            rotate(v.begin(), v.begin() + 2, v.end());
        else if (n == 2)
            swap(v[0], v[1]);

        for (int i = 0; i < n; i++)
            cout << v[i] << " ";
        cout << "\n";
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
typedef pair<lli, lli> pii;
#define ff first
#define ss second
#define MOD 1000000007
#define pb push_back
#define EPS 1e-6
const lli MAX = 1e9 + 7;

int main()
{
    fast;
    int t;
    cin >> t;
    while (t--)
    {
        lli n;
        cin >> n;
        vector<int> v(n);
        if (n % 2 == 0)
        {
            for (int i = 0; i < n; i++)
            {
                cin >> v[i];
                if (i % 2 == 1)
                    swap(v[i], v[i - 1]);
            }
        }
        else
        {
            for (int i = 0; i < n; i++)
                cin >> v[i];
            if (n > 1)
                rotate(v.begin(), v.begin() + 2, v.end());
        }
        for (int i = 0; i < n; i++)
            cout << v[i] << " ";
        cout << "\n";
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
typedef pair<lli, lli> pii;
#define ff first
#define ss second
#define MOD 1000000007
#define pb push_back
#define EPS 1e-6
const lli MAX = 1e9 + 7;

int main()
{
    fast;
    int t;
    cin >> t;
    while (t--)
    {
        lli n;
        cin >> n;
        vector<int> v(n);

        for (int i = 0; i < n; i++)
        {
            cin >> v[i];
            if (n % 2 == 0 && i % 2)
                swap(v[i], v[i - 1]);
        }
        if (n % 2)
        {
            int sum = v[0] + v[n - 1], idx=0;
            for (int i = 0; i < n - 1; i++)
            {
                if (i > 0 && v[i] + v[i + 1] == sum)
                {
                    idx = i + 1;
                    break;
                }
            }
            rotate(v.begin(), v.begin() + idx, v.end());
        }

        for (int i = 0; i < n; i++)
            cout << v[i] << " ";
        cout << "\n";
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
typedef pair<lli, lli> pii;
#define ff first
#define ss second
#define MOD 1000000007
#define pb push_back
#define EPS 1e-6
const lli MAX = 1e9 + 7;

int main()
{
    fast;
    int t;
    cin >> t;
    while (t--)
    {
        lli n;
        cin >> n;
        vector<int> v(n);

        for (int i = 0; i < n; i++)
        {
            cin >> v[i];
            if (n % 2 == 0 && i % 2)
                swap(v[i], v[i - 1]);
        }
        if (n % 2)
        {
            int sum = v[0] + v[n - 1], idx;
            for (int i = 0; i < n - 1; i++)
            {
                if (i > 0 && v[i] + v[i + 1] == sum)
                {
                    idx = i + 1;
                    break;
                }
            }
            rotate(v.begin(), v.begin() + idx, v.end());
        }

        for (int i = 0; i < n; i++)
            cout << v[i] << " ";
        cout << "\n";
    }
    return 0;
}


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

#define endl '\n'
#define f first
#define s second
#define ll long long
#define lli long long int
#define pf push_front
#define pb push_back
const int mod = 1e9 +7, INF = 1e9 + 7;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int t;
    cin >> t;

    while (t--) {
        int n;
        cin >> n;

        vector <int> v(n);
        for (auto &i : v) cin >> i;

        if (n & 1) {
            for (int i = (n-1)/2; i < n-1; ++i) cout << v[i] << " ";
            cout << v[n-1] << " ";
            for (int i = 0; i < (n-1)/2; ++i) cout << v[i] << " ";
            cout << endl;
        }
        else {
            for (int i = 0; i < n-1; ++i) {
                swap(v[i], v[i+1]);
                ++i;
            }

            for (auto i : v) cout << i << " ";
            cout << endl;
        }
    }
}


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

#define endl '\n'
#define f first
#define s second
#define ll long long
#define lli long long int
#define pf push_front
#define pb push_back
const int mod = 1e9 +7, INF = 1e9 + 7;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int t;
    cin >> t;

    while (t--) {
        int n;
        cin >> n;

        vector <int> v(n);
        for (auto &i : v) cin >> i;

        if (n & 1) {
            int i = 0, j = n-1;
            while (i < j) {
                swap(v[i], v[j]);
                ++i, --j;
            }

            for (auto i : v) cout << i << " ";
            cout << endl;
        }
        else {
            for (int i = 0; i < n-1; ++i) {
                swap(v[i], v[i+1]);
                ++i;
            }

            for (auto i : v) cout << i << " ";
            cout << endl;
        }
    }
}


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

#define endl '\n'
#define f first
#define s second
#define ll long long
#define lli long long int
#define pf push_front
#define pb push_back
const int mod = 1e9 +7, INF = 1e9 + 7;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int t;
    cin >> t;

    while (t--) {
        int n;
        cin >> n;

        vector <int> v(n);
        for (auto &i : v) cin >> i;

        for (int i = n-1; i >= 0; --i) cout << v[i] << " ";
        cout << endl;
    }
}


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

#define endl '\n'
#define f first
#define s second
#define ll long long
#define lli long long int
#define pf push_front
#define pb push_back
const int mod = 1e9 +7, INF = 1e9 + 7;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int t;
    cin >> t;

    while (t--) {
        int n;
        cin >> n;

        vector <int> v(n);
        for (auto &i : v) cin >> i;

        if (n & 1) {
            for (int i = (n-1)/2; i < n-1; ++i) cout << v[i] << " ";
            cout << v[n-1] << " ";
            for (int i = 0; i < (n-1)/2; ++i) cout << v[i] << " ";
            cout << endl;
        }
        else {
            for (int i = n-1; i >= 0; --i) cout << v[i] << " ";
            cout << endl;
        }
    }
}