"QAQ" is a word to denote an expression of crying. Imagine "Q" as eyes with tears and "A" as a mouth.Now Diamond has given Bort a string consisting of only uppercase English letters of length n. There is a great number of "QAQ" in the string (Diamond is so cute!).  illustration by 猫屋 https://twitter.com/nekoyaliu Bort wants to know how many subsequences "QAQ" are in the string Diamond has given. Note that the letters "QAQ" don't have to be consecutive, but the order of letters should be exact.

The only line contains a string of length n (1 ≤ n ≤ 100). It's guaranteed that the string only contains uppercase English letters.

Print a single integer — the number of subsequences "QAQ" in the string.

NoteIn the first example there are 4 subsequences "QAQ": "QAQAQYSYIOIWIN", "QAQAQYSYIOIWIN", "QAQAQYSYIOIWIN", "QAQAQYSYIOIWIN".

using System;
using System.Collections.Generic;
using System.Linq;

namespace Codeforces
{
	class Program
	{
		static void Main(string[] args)
		{
			string input1 = Console.ReadLine();
			//string input2 = Console.ReadLine();

			//string input3 = Console.ReadLine();
			//string input4 = Console.ReadLine();

			//var inputs1 = input1.Split(new[] { ' ', '}', '{' , ',' }, StringSplitOptions.RemoveEmptyEntries).Select(long.Parse).ToArray();
			//var inputs2 = input2.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(long.Parse).ToArray();

			int count = 0;
			var firstQIndex = input1.IndexOf("Q", StringComparison.CurrentCulture);
			while (firstQIndex >= 0)
			{
				var aIndex = input1.IndexOf("A", firstQIndex + 1, StringComparison.CurrentCulture);
				while (aIndex >= 0)
				{
					var secondQIndex = input1.IndexOf("Q", aIndex + 1, StringComparison.CurrentCulture);
					while (secondQIndex >= 0)
					{
						count++;
						secondQIndex = input1.IndexOf("Q", secondQIndex + 1, StringComparison.CurrentCulture);
					}

					aIndex = input1.IndexOf("A", aIndex + 1, StringComparison.CurrentCulture);
				}

				firstQIndex = input1.IndexOf("Q", firstQIndex + 1, StringComparison.CurrentCulture);
			}

			Console.WriteLine(count);
		}
	}
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var line = Console.ReadLine();
            var cntBefor = new int[line.Length];
            var cnt = 0;
            cntBefor[0] = line[0] == 'Q'? 1:0;
            for (int i = 1; i < line.Length; i++)
            {
                cntBefor[i] = cntBefor[i-1];
                if (line[i] == 'Q')
                    cntBefor[i]++;
            }

            var res = 0;
            for (int i = 0; i < line.Length; i++)
            {
                if (line[i] == 'A')
                {
                    res += (cntBefor[i]*(cntBefor[cntBefor.Length - 1] - cntBefor[i]));
                }
            }

            Console.WriteLine(res);
        }
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var line = Console.ReadLine();
            var cntQ = new int[line.Length];
            var cnt = 0;
            for (int i = line.Length-1; i >= 0; i--)
            {
                cntQ[i] = cnt;
                if (line[i] == 'Q')
                    cnt++;
            }

            var res = 0;
            for (int i = 0; i < line.Length; i++)
            {
                if (line[i] == 'Q')
                {
                    for (int j = i; j < line.Length; j++)
                    {
                        if (line[j] == 'A')
                            res += cntQ[j];
                    }
                }
            }

            Console.WriteLine(res);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ContestSolutions
{
    public static class ProblemG
    {
        static void Main(string[] args)
        {
            var line = ReadLine();
            var result = Solve(line);
            Console.WriteLine(result);
        }

        public static long Solve(string line)
        {
            var n = line.Length;
            int counter = 0;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    for (int k = 0; k < n; k++)
                    {
                        if(j > i || k > i || k > j)
                        {
                            continue;
                        }
                        if(line[i].ToString() + line[j] + line[k] == "QAQ")
                        {
                            counter++;
                        }
                    }
                }
            }
            return counter;
        }

        #region helpers
        static long Readlong()
        {
            return long.Parse(Console.ReadLine());
        }
        static string ReadLine()
        {
            return Console.ReadLine();
        }
        static long[] Readlongs()
        {
            long[] arr = Console.ReadLine().Split(' ')
                .Select(s => long.Parse(s))
                .ToArray();
            return arr;
        }
        static void Readlongs(out long i0, out long i1)
        {
            long[] arr = Readlongs();
            i0 = arr[0];
            i1 = arr[1];
        }
        static void Readlongs(out long i0, out long i1, out long i2)
        {
            long[] arr = Readlongs();
            i0 = arr[0];
            i1 = arr[1];
            i2 = arr[2];
        }
        static void WriteYesNo(bool value)
        {
            Console.WriteLine(value ? "YES" : "NO");
        }
        #endregion helpers
    }
}

using System;
using System.Linq;

namespace ConsoleApp
{
    class Program
    {
        static void Main()
        {
            string input = Console.ReadLine();
            int[] qCounts = new int[input.Length];
            if (input[0] == 'Q')
            {
                qCounts[0] = 1;
            }
            else
            {
                qCounts[0] = 0;
            }

            for (int i = 1; i < input.Length; i++)
            {
                if (input[i] == 'Q')
                {
                    qCounts[i] = qCounts[i - 1] + 1;
                }
                else
                {
                    qCounts[i] = qCounts[i - 1];
                }
            }
            int result = 0;
            int last = qCounts[qCounts.Length - 1];
            for (int i = 1; i < input.Length; i++)
            {
                if (input[i] == 'A')
                {
                    result += qCounts[i] * (last - qCounts[i]);
                }
            }
            Console.WriteLine(result);
        }
    }
}

using System;
namespace ConsoleApplication
{
    sealed class Program
    {
        static void Main(string[] args)
        {
            var s = Console.ReadLine();
            var result = 0;
            for (int i = 0; i < s.Length; i++)
            {
                for (int j = i + 1; j < s.Length; j++)
                {
                    for (int z = j + 1; z < s.Length; z++)
                    {
                        if (s[i] == 'Q' && s[j] == 'A' && s[z] == 'Q')
                            result++;
                    }
                }
            }

            Console.WriteLine(result);
        }
    }
}

using System;
using System.Linq;

namespace codeforces
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] ch = Console.ReadLine().Where(x => x == 'Q' || x == 'A').Select(x=>x).ToArray();
            string str = string.Join("", ch);
            int rez = 0;
            int countOfQ = str.Count(x => x == 'Q');
            while(str.Contains('A'))
            {
                int counter = 0;
                for (int i = 0; i < str.IndexOf('A'); i++)
                {
                    if (str[i] == 'Q')
                        counter++;
                }
                rez += counter * (countOfQ - counter);
                str = str.Remove(str.IndexOf('A'), 1);
            }

            Console.WriteLine(rez);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace a
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "QAQ";
            string t = Console.ReadLine();
            var d = new int[s.Length + 1, t.Length + 1];
            for (int i = 0; i < s.Length + 1; i++)
            {
                for(int j = 0; j < t.Length + 1; j++)
                {
                    if (i == 0)
                    {
                        d[i, j] = 1;
                        continue;
                    }
                    if (j == 0)
                    {
                        d[i, j] = 0;
                        continue;
                    }
                    d[i, j] = d[i, j - 1] + (s[i - 1] == t[j - 1] ? d[i - 1, j - 1] : 0);
                }
            }

            Console.WriteLine(d[s.Length,  t.Length].ToString());
            //Console.ReadKey();

        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static string s, t;
        static int n;
        static long solve(int idx,int j)
        {
            if (j == 3) return 1;
            if (idx == n) return 0;
            long s1 = solve(idx + 1, j);
            if (s[idx] == t[j])
                s1 += solve(idx + 1, j + 1);
            return s1;
        }
        static void Main(string[] args)
        {
            s = Console.ReadLine();
            n = s.Length;
            t = "QAQ";
            Console.WriteLine(solve(0, 0));
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static string s, t;
        static int n;
        static long[,] dp;
        static long solve(int idx,int j)
        {
            if (j == 3) return 1;
            if (idx == n) return 0;
            long s1 = solve(idx + 1, j);
            if (s[idx] == t[j])
                s1 += solve(idx + 1, j + 1);
            return s1;
        }
        static void Main(string[] args)
        {
            s = Console.ReadLine();
            n = s.Length;
            t = "QAQ";
            dp = new long[101, 4];
            for(int i=0; i<=n; i++)
            {
                for(int j=0; j<=3; j++)
                {
                    if (j == 0) dp[i, j] = 1;
                    else if (i == 0) dp[i, j] = 0;
                    else
                    {
                        dp[i, j] = dp[i-1,j];
                        if (s[i-1] == t[j-1])
                            dp[i, j] += dp[i - 1, j - 1];
                    }
                }
            }
            Console.WriteLine(dp[n,3]);
        }
    }
}

using System;
using System.Collections.Generic;
namespace problem_solve_with_csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            long i = 0, j = 0, c = 0, k = 0;
            string s1 = Console.ReadLine();
            char[] s = s1.ToCharArray();
            for (i = 0; i < s.Length; i++)
                if (s[i] == 'Q')
                    for (j = i + 1; j < s.Length; j++)
                        if (s[j] == 'A')
                            for (k = j + 1; k < s.Length; k++)
                                if (s[k] == 'Q')
                                    c++;
            Console.WriteLine(c);
        }
        }
    }

using System;

namespace CSharp
{
    public class _894A
    {
        public static void Main(string[] args)
        {
            string s = Console.ReadLine();

            int[] left = new int[s.Length];
            left[0] = s[0] == 'Q' ? 1 : 0;

            for (int i = 1; i < s.Length; i++)
            {
                left[i] = s[i] == 'Q' ? left[i - 1] + 1 : left[i - 1];
            }

            int[] right = new int[s.Length];
            right[s.Length - 1] = s[s.Length - 1] == 'Q' ? 1 : 0;

            for (int i = s.Length - 2; i >= 0; i--)
            {
                right[i] = s[i] == 'Q' ? right[i + 1] + 1 : right[i + 1];
            }

            int result = 0;

            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == 'A')
                {
                    result += left[i] * right[i];
                }
            }

            Console.WriteLine(result);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Scanner
{
    private readonly char Separator = ' ';
    private int Index = 0;
    private string[] Line = new string[0];
    public string Next()
    {
        if (Index >= Line.Length)
        {
            Line = Console.ReadLine().Split(Separator);
            Index = 0;
        }
        var ret = Line[Index];
        Index++;
        return ret;
    }

    public int NextInt()
    {
        return int.Parse(Next());
    }

}
class Program
{
    public void Solve()
    {
        var sc = new Scanner();
        var S = sc.Next();
        long ans = 0;
        for (int i = 0; i < S.Length; i++)
        {
            if (S[i] == 'Q')
            {
                for (int j = i + 1; j < S.Length; j++)
                {
                    if(S[j]=='A')
                    {
                        for(int k=j+1;k<S.Length;k++)
                        {
                            if(S[k]=='Q')
                            {
                                ans++;
                            }
                        }
                    }
                }
            }
        }
        Console.WriteLine(ans);
    }

    static void Main(string[] args)
    {
        new Program().Solve();
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Solution
{
    class Program
    {
        static void Main(string[] args)
        {
            var qaq = Console.ReadLine().Where( x => (x == 'Q' || x == 'A')).ToArray();
            var total = 0;
            for (var i = 0; i < qaq.Length; i ++)
            for (var k = i+1; k < qaq.Length; k++)
            for (var j = k+1; j < qaq.Length; j++)
            {
                total+=(qaq[i] == 'Q' && qaq[k] == 'A' && qaq[j] == 'Q' )? 1 : 0;
            }
            Console.WriteLine(total);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO.Pipes;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

class Time
{
    public int hour;
    public int min;
}
class Test
{
    static long MOD = 1000000007;
    static long[] fact = new long[500001];
    static long[] factInv = new long[500001];
    // Driver code
    public static void Main()
    {
        // int t = int.Parse(Console.ReadLine());
        // while (t > 0)
        {
            // t--;
            // var input1 = Console.ReadLine().Split(' ').Select(xx => long.Parse(xx)).ToArray();
            // var n = input1[0];

            var str = Console.ReadLine();
            int ans = 0;
            for (int i = 0; i < str.Length; i++)
            {
                for (int j = i + 1; j < str.Length; j++)
                {
                    for (int k = j + 1; k < str.Length; k++)
                    {
                        if (str[i] == 'Q' && str[j] == 'A' && str[k] == 'Q')
                        {
                            ans++;
                        }

                    }
                }
            }

            Console.WriteLine(ans);
        }
    }

    public static int Ways(string str, int start, char letter, string ans)
    {
        if (start >= str.Length)
            return 0;
        int count = 0;
        if (ans.Length == 3)
        {
            if (ans.Contains("QAQ"))
            {
                count++;
                ans = ans.Substring(0, ans.Length - 1);
            }
        }
        for (int j = start; j < str.Length; j++)
        {
            if (str[start] == 'Q')
            {

                Ways(str, start + 1, 'Q', ans);

            }
        }

        return count;

    }

    public static int GCD(int a, int b)
    {
        if (b == 0)
            return a;

        return GCD(b, a % b);
    }

    static bool
        Coprime(int a, int b)
    {
        return (GCD(a, b) == 1);
    }

    //}

    public static long FastPower(long a, long b)
    {
        if (b == 0)
            return 1;

        var val = FastPower(a, b / 2);

        if (b % 2 == 0)
            return val * val % MOD;
        else
            return val * val % MOD * a % MOD;
    }

    public static void Fact()
    {
        fact[0] = 1;
        factInv[0] = FastPower(fact[0], MOD - 2);

        for (int i = 1; i <= 500000; i++)
        {
            fact[i] = i * fact[i - 1] % MOD;
            factInv[i] = FastPower(fact[i], MOD - 2);
        }

    }

    public static long NCR(long n, long r)
    {
        return fact[n] * factInv[r] % MOD * factInv[n - r] % MOD;
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static string str = "QAQ";
        static string str1;
        static int[,] dp;
        static int Q(int n,int m)
        {
            if (m==str.Length)
            {
                return 1;
            }
            if (str1.Length == n)
            {
                return 0;
            }
            if (dp[n,m] != -1)
            {
                return dp [n , m];
            }
            int sol = Q(n + 1, m);
            if  (str[m] == str1[n])
            {
                sol +=  Q( n+1, m + 1) ;
            }
            dp[n, m] = sol;
            return sol;
        }
        static void Main(string[] args)
        {
            str1 = Console.ReadLine();
            dp = new int[str1.Length+1, str.Length+1];
            for (int i =str1.Length; i >= 0 ; i--)
            {
                for (int j=str.Length; j>=0; j--)
                {
                    if (j==str.Length)
                    {
                        dp[i, j] = 1;
                    }
                    else if (i==str1.Length)
                    {
                        dp[i, j] = 0;
                    }
                    else
                    {
                        int sol = dp[i + 1, j];
                        if (str1[i] == str[j])
                        {
                            sol += dp[i + 1, j + 1];
                        }
                        dp[i, j] = sol;
                    }
                }
            }
            Console.WriteLine(dp[0, 0]);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static string str = "QAQ";
        static string str1;
        static int[,] dp;
        static int Q(int n,int m)
        {
            if (m==str.Length)
            {
                return 1;
            }
            if (str1.Length == n)
            {
                return 0;
            }
            if (dp[n,m] != -1)
            {
                return dp [n , m];
            }
            int sol = Q(n + 1, m);
            if  (str[m] == str1[n])
            {
                sol +=  Q( n+1, m + 1) ;
            }
            dp[n, m] = sol;
            return sol;
        }
        static void Main(string[] args)
        {
            str1 = Console.ReadLine();
            dp = new int[str1.Length, str.Length];
            for (int i=0;i<str1.Length; i++)
            {
                for (int j=0; j<str.Length; j++)
                {
                    dp[i,j] = -1;
                }
            }
            Console.WriteLine ( Q (0,0) );
        }
    }
}

using System;
using System.Collections.Generic;
using System.Collections;

namespace codeforces
{

    public class Program
    {
        private static void Main(string[] args)
        {
            string s = Console.ReadLine();
            int count = 0;
            for (int i = 0; i < s.Length; ++i)
            {
                if (s[i] != 'Q')
                {
                    continue;
                }
                for (int j = i + 1; j < s.Length; ++j)
                {
                    if (s[j] != 'A')
                    {
                        continue;
                    }
                    for (int k = j + 1; k < s.Length; ++k)
                    {
                        if (s[k] == 'Q')
                        {
                            count++;
                        }
                    }
                }
            }
            Console.WriteLine(count.ToString());
            Console.Read();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Collections;

namespace codeforces
{

    public class Program
    {
        private static void Main(string[] args)
        {
            string s = Console.ReadLine();
            int count = 0;
            var pref = new int[s.Length];
            var suf = new int[s.Length];
            pref[0] = s[0] == 'Q' ? 1 : 0;
            for (int i = 1; i < s.Length; ++i)
            {
                pref[i] = pref[i - 1] + (s[i] == 'Q' ? 1 : 0);
            }
            suf[s.Length - 1] = s[s.Length - 1] == 'Q' ? 1 : 0;
            for (int i = s.Length - 2; i >= 0; --i)
            {
                suf[i] = suf[i + 1] + (s[i] == 'Q' ? 1 : 0);
            }
            for (int i = 0; i < s.Length; ++i)
            {
                if (s[i] == 'A' && i > 0 && i < s.Length - 1)
                {
                    count += suf[i + 1] * pref[i - 1];
                }
            }
            Console.WriteLine(count.ToString());
            Console.Read();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

class Procon {
    static void Main (string[] agrs) {
        char[] S = Chars();

        int[] Q1 = new int[S.Length+2];
        int[] Q2 = new int[S.Length+2];

        for (int i = 0; i < S.Length; i++) {
            if (S[i] == 'Q') {
                Q1[i + 1] = Q1[i] + 1;
            } else {
                Q1[i + 1] = Q1[i];
            }

            if (S[S.Length - 1 - i] == 'Q') {
                Q2[S.Length - i] = Q2[S.Length - i + 1] + 1;
            } else {
                Q2[S.Length - i] = Q2[S.Length - i + 1];
            }
        }

        int ret = 0;
        for (int i = 1; i <= S.Length; i++) {
            if (S[i - 1] != 'A') {
              continue;
            }
            ret += Q1[i - 1] * Q2[i + 1];
        }

        Console.WriteLine(ret);
    }

    static string String () => Scanner.nextString ();
    static int Int () => int.Parse (Scanner.nextString ());
    static long Long () => long.Parse (Scanner.nextString ());
    static double Double () => double.Parse (Scanner.nextString ());
    static char[] Chars () => String ().ToCharArray ();
    static string[] Strings () => Console.ReadLine ().Split (' ');
    static int[] Ints () => Strings ().Select (v => int.Parse (v)).ToArray ();
    static long[] Longs () => Strings ().Select (v => long.Parse (v)).ToArray ();
    static double[] Doubles () => Strings ().Select (v => double.Parse (v)).ToArray ();
    const int M = 1000000007;
}

class PriorityQueue<T> where T : IComparable<T> {
    public T[] heap;
    public int size;
    public int sign;
    public PriorityQueue (int N, bool descend = false) {
        heap = new T[N];
        sign = 1;
        if (descend) sign = -1;
    }
    public int Compare (T x, T y) {
        return x.CompareTo (y) * sign;
    }
    public void Push (T x) {
        int i = size++;
        while (i > 0) {
            int p = (i - 1) / 2;
            if (Compare (x, heap[p]) >= 0) {
                break;
            }
            heap[i] = heap[p];
            i = p;
        }
        heap[i] = x;
    }
    public T Pop () {
        T ret = heap[0];
        T x = heap[--size];
        int i = 0;
        while (i * 2 + 1 < size) {
            int a = i * 2 + 1;
            int b = i * 2 + 2;
            if (b < size && Compare (heap[a], heap[b]) > 0) {
                a = b;
            }
            if (Compare (heap[a], x) >= 0) {
                break;
            }
            heap[i] = heap[a];
            i = a;
        }
        heap[i] = x;
        return ret;
    }
    public int Count () {
        return size;
    }
}

class Scanner {
    static string[] s = new string[0];
    static int i = 0;
    static int max_i = 0;
    static public string nextString () {
        if (i >= s.Length) {
            s = Console.ReadLine ().Split (' ');
            max_i = s.Length;
            i = 0;
            if (max_i == 0) {
                return "";
            }
            return s[i++];
        }
        return s[i++];
    }
}

using System;
using System.Collections.Generic;

namespace QAQ
{
    class Ctecka
    {
        public static int PrectiCislo()
        {
            int Znak = Console.Read();
            int x = 0;
            bool Znaminko = false;

            while ((Znak < '0') || (Znak > '9')) //TODO hlidat konec vstupu!
            {
                if (Znak == '-') Znaminko = true;
                else Znaminko = false;

                Znak = Console.Read();
            }

            while ((Znak >= '0') && (Znak <= '9'))
            {
                x = 10 * x + (Znak - '0');
                Znak = Console.Read();
            }

            if (Znaminko)
            {
                x = -x;
                Znaminko = false;
            }

            return x;
        }
    }
    class Qaq
    {
        public string word;
        public char[] wordArray;
        public int[] results = new int[101];
        public int qCount = 0;
        public int secondQCount = 0;
        public int ACount = 0;
        public int QAQCount = 0;
        public bool Aflag = false;
        public int qBeforeA = 0;
        public int tempSum = 0;
        public List<int> aCounting = new List<int>();

        public int countOccurence()
        {
            for (int i = 0; i < word.Length; i++)
            {
                if (word[i] == 'Q')
                {
                    qCount++;
                    if (aCounting.Count > 0)
                    {
                        tempSum = 0;
                        foreach (int occ in aCounting)
                        {
                            tempSum += occ;
                        }
                        QAQCount += tempSum;
                    }
                }
                else if (word[i] == 'A')
                {
                    aCounting.Add(qCount);
                }
            }
            return QAQCount;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Qaq q = new Qaq();
            q.word = Console.ReadLine();
            Console.WriteLine(q.countOccurence());

        }
    }
}

// Based on azukin (https://codeforces.com/profile/azukun) C# starter
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;

public class Solver
{
    private int NumberOfQAQ(string str)
    {
        int total = 0;

        for (int i = 0; i < str.Length; i++)
        {
            if (str[i] == 'Q') total += CountQAQFrom(str, i, 1);
        }

        return total;
    }

    private int CountQAQFrom(string str, int start, int letter)
    {
        if (letter == 3)
        {
            return 1;
        }

        int total = 0;
        for (int i = start; i < str.Length; i++)
        {
            if (str[i] == 'Q' && letter % 2 == 0)
            {
                total += CountQAQFrom(str, i + 1, letter + 1);
            }
            else if (str[i] == 'A' && letter % 2 == 1)
            {
                total += CountQAQFrom(str, i + 1, letter + 1);
            }
        }
        return total;
    }

    public void Solve()
    {
        var line = ReadLine();

        Write(NumberOfQAQ(line));
    }

    #region Main

    protected static TextReader reader;
    protected static TextWriter writer;
    static void Main()
    {
#if DEBUG
        // reader = new StreamReader("..\\..\\input");
        reader = new StreamReader(Console.OpenStandardInput());
        writer = new StreamWriter(Console.OpenStandardOutput());
        // writer = Console.Out;
        //writer = new StreamWriter("..\\..\\output");
#else
        reader = new StreamReader(Console.OpenStandardInput());
        writer = new StreamWriter(Console.OpenStandardOutput());
        //reader = new StreamReader("duty.in");
        //writer = new StreamWriter("duty.out");
#endif
        try
        {
            // var thread = new Thread(new Solver().Solve, 1024 * 1024 * 512);
            // thread.Start();
            // thread.Join();
            new Solver().Solve();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
#if DEBUG
#else
            throw;
#endif
        }
        reader.Close();
        writer.Close();
    }

    #endregion

    #region Read / Write
    private static Queue<string> currentLineTokens = new Queue<string>();
    private static string[] ReadAndSplitLine() { return reader.ReadLine().Split(new[] { ' ', '\t', }, StringSplitOptions.RemoveEmptyEntries); }
    public static string ReadToken() { while (currentLineTokens.Count == 0) currentLineTokens = new Queue<string>(ReadAndSplitLine()); return currentLineTokens.Dequeue(); }
    public static int ReadInt() { return int.Parse(ReadToken()); }
    public static long ReadLong() { return long.Parse(ReadToken()); }
    public static double ReadDouble() { return double.Parse(ReadToken(), CultureInfo.InvariantCulture); }
    public static int[] ReadIntArray() { return ReadAndSplitLine().Select(int.Parse).ToArray(); }
    public static long[] ReadLongArray() { return ReadAndSplitLine().Select(long.Parse).ToArray(); }
    public static double[] ReadDoubleArray() { return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray(); }
    public static int[][] ReadIntMatrix(int numberOfRows) { int[][] matrix = new int[numberOfRows][]; for (int i = 0; i < numberOfRows; i++) matrix[i] = ReadIntArray(); return matrix; }
    public static int[][] ReadAndTransposeIntMatrix(int numberOfRows)
    {
        int[][] matrix = ReadIntMatrix(numberOfRows); int[][] ret = new int[matrix[0].Length][];
        for (int i = 0; i < ret.Length; i++) { ret[i] = new int[numberOfRows]; for (int j = 0; j < numberOfRows; j++) ret[i][j] = matrix[j][i]; }
        return ret;
    }
    public static string ReadLine() { return reader.ReadLine().Trim(); }
    public static string[] ReadLines(int quantity) { string[] lines = new string[quantity]; for (int i = 0; i < quantity; i++) lines[i] = ReadLine(); return lines; }
    public static void WriteArray<T>(IEnumerable<T> array) { writer.WriteLine(string.Join(" ", array)); }
    public static void Write(params object[] array) { WriteArray(array); }
    public static void WriteLines<T>(IEnumerable<T> array) { foreach (var a in array) writer.WriteLine(a); }
    private class SDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
        public new TValue this[TKey key]
        {
            get { return ContainsKey(key) ? base[key] : default(TValue); }
            set { base[key] = value; }
        }
    }
    private static T[] Init<T>(int size) where T : new() { var ret = new T[size]; for (int i = 0; i < size; i++) ret[i] = new T(); return ret; }
    #endregion
}

using System;
class a
{
    static void Main()
    {
        var s = Console.ReadLine();
        string p = "";
        for (int i = 0; i < s.Length; i++) if (s[i] == 'Q' || s[i] == 'A') p += s[i];
        int r = 0;
        for (int i = 0; i < p.Length; i++)
        {
            for (int j = i+1; j < p.Length; j++)
            {
                for (int k = j+1; k < p.Length; k++)
                {
                    if (p[i].ToString() + p[j].ToString() + p[k].ToString() == "QAQ") r++;
                }
            }
        }
        Console.WriteLine(r);
    }
}

using System;
using System.Text;
using System.Linq;

namespace AlgoTasks
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder(string.Join("", Console.ReadLine().Where(ch => ch == 'Q' || ch == 'A')));
            int c = 0;
            for (int i = 0; i < sb.Length; i++)
            {
                for (int j = i + 1; j < sb.Length; j++)
                {
                    for (int k = j + 1; k < sb.Length; k++)
                    {
                        if (new string(new char[] { sb[i], sb[j], sb[k] }) == "QAQ")
                            c++;
                    }
                }
            }
            Console.WriteLine(c);
        }
    }
}

using System;
using System.Collections.Generic;

namespace Algorithms
{
    class Program
    {
        static void Main(string[] args)
        {
            string n = Console.ReadLine();

            var result = GetNumberOfQAQ(n);

            Console.WriteLine(result);
        }

        private static int GetNumberOfQAQ(string inputString)
        {
            int numberOfQAQ = 0, numberOfQ = 0, numberOfQA = 0;

            for (int i = 0; i < inputString.Length; i++)
            {
                if (inputString[i] == 'A')
                {
                    if (numberOfQ > 0)
                        numberOfQA += numberOfQ;
                }

                if (inputString[i] == 'Q')
                {
                    numberOfQAQ += numberOfQA;
                    numberOfQ++;
                }
            }

            return numberOfQAQ;
        }

    }
}

using System;
namespace Codeforces
{
    class Program
    {
        static void Main(string[] args)
        {

            string aa = Console.ReadLine();
            int c = 0;
            for (int i = 0; i < aa.Length; i++)
            {

                if (aa[i] == 'Q')
                {
                    int hh = i + 1;

                    while (hh < aa.Length) {
                        if(aa[hh] == 'A')
                        {
                            int tt = hh + 1;
                            while(tt < aa.Length)
                            {
                                if(aa[tt] == 'Q')
                                {
                                    c++;
                                }
                                tt++;
                            }
                        }
                        hh++;
                    }
                }
            }
            Console.WriteLine(c);
        }

        static int convertToInt(string n)
        {
            return int.Parse(n);
        }
    }
}

using System;

class Solution
{
    static void Main(string[] args)
    {
        char[] input = Console.ReadLine().ToCharArray();
        int[] prefix = new int[input.Length];
        int count = 0;

        for(int i = 0; i < input.Length; i++)
        {
            if (input[i] == 'Q')
            {
                prefix[i] = 1;
            }
        }
        for(int i = 0; i < input.Length; i++)
        {
            if (input[i] == 'A')
            {
                int left = 0;
                int right = 0;

                for (int j = 0; j < i; j++)
                {
                    left += prefix[j];
                }

                for (int k = i + 1; k < input.Length; k++)
                {
                    right += prefix[k];
                }
                count += left * right;
            }
        }
        Console.WriteLine(count);
    }
}

using System;

namespace QAQ
{
    class Program
    {
        static void Main(string[] args)
        {
            string line = Console.ReadLine();
            int n = line.Length;
            int[] countQto = new int[n];
            int numberOfQ = 0;
            for (int i = 0; i < n; i++)
            {
                if (line[i] == 'Q')
                {
                    numberOfQ++;
                }
                countQto[i] = numberOfQ;
            }
            int numberOfSubseq = 0;
            for (int i = 0; i < n; i++)
            {
                if (line[i] == 'A')
                {
                    numberOfSubseq+= countQto[i] * (numberOfQ - countQto[i]);
                }
            }
            Console.WriteLine(numberOfSubseq);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QAQ
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = Console.ReadLine();
            List<int> indexQ = new List<int>();
            List<int> indexA = new List<int>();
            int sum = 0;
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == 'Q')
                {
                    indexQ.Add(i);
                }
                else if (s[i] == 'A')
                {
                    indexA.Add(i);
                }
            }
            int count1 = indexQ.Count;
            int count2 = indexA.Count;

            for (int i = 0; i < indexA.Count; i++)
            {
                for (int j = 0; j < indexQ.Count; j++)
                {
                    if (indexA[i] < indexQ[j])
                    {
                        sum += (count1 - j) * (j);
                        break;
                    }
                }
            }
            Console.WriteLine(sum);
            Console.ReadLine();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QAQ_2._0
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = Console.ReadLine();
            int br = 0;
            int[] sumaPrefiksa = new int[s.Length];
            if (s[0] == 'Q') sumaPrefiksa[0] = 1;
            for (int i = 1; i < s.Length; i++)
            {
                sumaPrefiksa[i] = sumaPrefiksa[i - 1];
                if (s[i] == 'Q') sumaPrefiksa[i]++;
            }
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == 'A')
                {
                    int pre = sumaPrefiksa[i];
                    int posle = sumaPrefiksa[s.Length - 1] - pre;
                    br += pre * posle;
                }
            }
            Console.WriteLine(br);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QAQ
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = Console.ReadLine();
            int br = 0;
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == 'Q')
                {
                    for (int j = i + 1; j < s.Length; j++)
                    {
                        if (s[j] == 'A')
                        {
                            for (int k = j + 1; k < s.Length; k++)
                            {
                                if (s[k] == 'Q') br++;
                            }
                        }
                    }
                }
            }
            Console.WriteLine(br);
        }
    }
}

using System;
using System.Linq;
namespace a
{
    class Program
    {
        static void Main(string[] args)
        {
            var input = Console.ReadLine().Where(i => i == 'Q' || i == 'A').ToArray();
            var sum = 0;
            for (int i=0;i<input.Length;i++) {
                int cura = 0;
                int cur = 0;
                if (input[i] == 'A')
                    continue;
                for (int j=i+1;j<input.Length;j++) {
                    switch (input[j])
                    {
                        case 'A':
                            cura++;
                            break;
                        case 'Q':
                            cur += cura;
                            break;
                    }
                }
                sum += cur;
            }
            System.Console.Write(sum);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeForceProblemSolve
{
	class MainClass
	{

		public static void Main(string[] args)
		{
			String s = Console.ReadLine();
			int count = 0;

			for (int i = 0; i < s.Length; i++)
			{

				for (int j = i+1; j <s.Length ; j++)
				{

					for (int k = j+1; k < s.Length; k++)
					{
						if ((s[i]+""+s[j]+""+s[k]).Equals("QAQ"))
						{
							count++;
						}
					}
				}
			}

			Console.WriteLine(count);
		}
	}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Training
{
    class Program
    {
       public static string s = Console.ReadLine();
       public static string qq = "QAQ";
        public static int[,] arr = new int[s.Length+1, 4]  ;

        public static  int Q(int m,int n)
        {
            if (arr[n , m ] != -1)
                return arr[n , m ];
            if (m == 0)
                return 1;
            if (n == 0)
                return 0;
            int i=0, j=0;
            if (qq[m-1]==s[n-1])
             i = Q( m - 1,  n - 1);

             j = Q(m, n - 1);
            arr[n , m ] = i + j;
            return i+j;

        }
        static int Qq(int m,int n)
        {
            for (int i = 0; i <= n; i++)
            {
                for (int j = 0; j <= m; j++)
                {
                     if (j == 0)
                        arr[i, j] = 1;
                    else if (i == 0)
                        arr[i, j] = 0;

                    else
                    {
                        arr[i, j] = arr[i - 1, j];
                        if (qq[j - 1] == s[i - 1])
                            arr[i, j] += arr[i - 1, j - 1];
                        //if (qq[j] == s[i])
                        // arr[i, j] = Qq(j, i + 1);
                    }
                }
            }
            return arr[n, m];

        }

        static void Main(string[] args)
        {
            for (int i = 0; i < s.Length+1; i++)
            {
                for (int j = 0; j < 4; j++)

                    arr[i, j] = -1;
            }

            //Console.WriteLine(Q(3, s.Length));
            Console.WriteLine(Qq(3, s.Length));

        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Training
{
    class Program
    {
       public static string s  ;
       public static string qq = "QAQ";
        public static  int Q(int m,int n)
        {
            if (m == 0)
                return 1;
            if (n == 0)
                return 0;
            int i=0, j=0;
            if (qq[m-1]==s[n-1])
             i = Q( m - 1,  n - 1);

             j = Q(m, n - 1);

            return i+j;

        }

        static void Main(string[] args)
        {
            s = Console.ReadLine();
            Console.WriteLine(Q(3, s.Length));

        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Training
{
    class Program
    {
       public static string s = Console.ReadLine();
       public static string qq = "QAQ";
        public static int[,] arr = new int[s.Length+1, 4]  ;

        public static  int Q(int m,int n)
        {
            if (arr[n , m ] != -1)
                return arr[n , m ];
            if (m == 0)
                return 1;
            if (n == 0)
                return 0;
            int i=0, j=0;
            if (qq[m-1]==s[n-1])
             i = Q( m - 1,  n - 1);

             j = Q(m, n - 1);
            arr[n , m ] = i + j;
            return i+j;

        }

        static void Main(string[] args)
        {
            for (int i = 0; i < s.Length+1; i++)
            {
                for (int j = 0; j < 4; j++)

                    arr[i, j] = -1;
            }

            Console.WriteLine(Q(3, s.Length));

        }
    }
}

using System;
using System.Collections.Generic;

namespace CodeForces {

    class RW {

        public static string RStrLn() {
            return Console.ReadLine();
        }

        public static int RIntStdin() {
            // Read only one integer, infinite input
            int prev = ' '; int res = 0;
            int symb = Console.Read();
            while ((symb < '0') || (symb > '9')) {
                prev = symb;
                symb = Console.Read();
            }
            while ((symb >= '0') && (symb <= '9')) {
                res = 10 * res + symb - '0';
                symb = Console.Read();
            }
            if (prev == '-') { return -res; }
            else { return res; }
        }

        public static List<int> RIntsStdin(int n) {
            // Read bunch of integers, infinite input
            List<int> res = new List<int>(); int cnt = 0;
            while(cnt < n) {
                res.Add(RIntStdin());
                cnt++;
            }
            return res;
        }

        public static int ConvStrToInt(string str) {
            int res = 0; int starti = 0; int sign = 1;
            if (str[0] == '-') { starti = 1; sign = -1;}
            for (int i = starti; i < str.Length; i++) {
                res = 10 * res + sign * (str[i] - '0');
            }
            return res;
        }

        public static List<string> SplitStrByDelim(string str,
                                                   char delim) {
            List<string> res = new List<string>(); string buf = "";
            for (int i = 0; i < str.Length; i++) {
                if (str[i] != delim) { buf = buf + str[i]; }
                else { if (buf != "") { res.Add(buf); buf = ""; } }
            }
            if (buf != "") { res.Add(buf); }
            return res;
        }

        public static int RIntLn() {
            // Read the whole line and extract only one integer
            int prev = ' '; int starti = 0; int res = 0;
            string s = System.Console.ReadLine();
            while ((s[starti] < '0' ) || s[starti] > '9' ) {
                prev = s[starti]; starti++;
            }
            while ((starti < s.Length) &&
                   (s[starti] >= '0')  &&
                   (s[starti] <= '9')) {
                res = 10 * res + s[starti] - '0';
                starti++;
            }
            if (prev == '-') {
                return -res;
            }
            else {
                return res;
            }
        }

        public static List<int> RIntsLn() {
            // Read the whole line and extract all integers
            List<int> res = new List<int>();
            List<string> s = SplitStrByDelim(
                             Console.ReadLine(), ' ');
            foreach (string i in s) {
                res.Add(ConvStrToInt(i));
            }
            return res;
        }

        public static void ShowObjs(params object[] objs) {
            for (int i = 0; i < objs.Length; i++) {
                Console.Write(objs[i] + " ");
            }
            Console.Write('\n');
        }

        public static void ShowArr<T>(ref T[] arr, int si) {
            for (int i=si; i<arr.Length; i++) {
                Console.Write("{0} ", arr[i]);
            }
            Console.Write("\n");
        }

        public static void ShowList<T>(ref List<T> lst, int si) {
            // si = start index
            for (int i=si; i<lst.Count; i++) {
                Console.Write("{0} ", lst[i]);
            }
            Console.Write("\n");
        }

    }

    class Program {
        static void Main(string[] args) {

            string s = Console.ReadLine();
            int res = 0;
            int q_last = 0; int q_occur = 0;
            int[] a = new int[s.Length];
            int i;

            for (i=0; i<s.Length; i++) {
                if (s[i] == 'Q') {
                    q_occur++;
                    q_last = i;
                }
                else if (s[i] == 'A') {
                    a[i] = q_occur;
                }
                //RW.ShowObjs(s[i], a[i], q_occur, q_last);
            }
            //RW.ShowArr(ref a, 0);

            i = 0;
            while (i < q_last) {
                if (s[i] == 'A') {
                    res += a[i]*(q_occur-a[i]);
                }
                i++;
            }
            Console.WriteLine("{0}", res);
        }
    }

}

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        string str = Console.ReadLine();

        List<int> q = new List<int>();
        List<int> a = new List<int>();

        for (int i = 0; i < str.Length; i++)
        {
            if (str[i] == 'A')
                a.Add(i);
            else if (str[i] == 'Q')
                q.Add(i);
        }

        int result = 0;
        foreach (int i in a)
        {
            int left = 0;
            int right = 0;
            foreach (int j in q)
            {
                if (j < i)
                    left++;
                else
                    right++;
            }
            result += left * right;
        }

        Console.WriteLine(result);
    }
}

using System;

namespace Elephant
{
    class Program
    {
        public static int Func(char[] sum)
        {
            int count = sum.Length;
            int ret = 0;
            for(int i = 0; i < count; i++)
            {
                if (sum[i] == 'Q')
                {
                    for(int k = i; k < count; k++)
                    {
                        if (sum[k] == 'A')
                        {
                            for (int j = k; j < count; j++)
                            {
                                if (sum[j] == 'Q')
                                {
                                    ret++;
                                }
                            }
                        }
                    }
                }
            }

            return ret;
        }
        static void Main(string[] args)
        {
            char[] sum = Console.ReadLine().ToCharArray();
            Console.Write(Func(sum));
        }
    }
}

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

class Simple {
    string str;
    void Solve() {
        str = io.String;
        var ans = 0;
        for (var i = 0; i < str.Length; ++i) {
            if (str[i] != 'Q') continue;
            for (int j = i + 1; j < str.Length; ++j) {
                if (str[j] != 'A') continue;
                for (int k = j + 1; k < str.Length; ++k) {
                    if (str[k] == 'Q')
                        ans++;
                }
            }
        }
        io.o(ans);
    }
    SimpleIO io = new SimpleIO();
    public static void Main(string[] args) { new Simple().Stream(); }
    void Stream() {
        Solve();
        io.writeFlush();
    }
}

class SimpleIO {
    string[] nextBuffer;
    int BufferCnt;
    char[] cs = new char[] { ' ' };
    StreamWriter sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
    public SimpleIO() {
        nextBuffer = new string[0];
        BufferCnt = 0;
        Console.SetOut(sw);
    }
    public string Next() {
        if (BufferCnt < nextBuffer.Length)
            return nextBuffer[BufferCnt++];
        string st = Console.ReadLine();
        while (st == "")
            st = Console.ReadLine();
        nextBuffer = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
        BufferCnt = 0;
        return nextBuffer[BufferCnt++];
    }
    public string String => Next();
    public char Char => char.Parse(String);
    public int Int => int.Parse(String);
    public long Long => long.Parse(String);
    public double Double => double.Parse(String);
    public void o<T>(T v) { Console.WriteLine(v); }
    public void writeFlush() { Console.Out.Flush(); }
}

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Numerics;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Codeforces.TaskA
{
    public class Task
    {
        void Solve()
        {
            Input.Next(out string value);
            var d = new int[value.Length+1, 4];
            // 0 - Q
            // 1 - QA
            var count = 0;
            for (var i = 1; i <= value.Length; i++)
            {
                if (value[i-1] == 'Q')
                {
                    count += d[i - 1, 1];
                    d[i, 0] = d[i - 1, 0] + 1;
                    d[i, 1] = d[i - 1, 1];
                }
                else if (value[i-1] == 'A')
                {
                    d[i, 0] = d[i - 1, 0];
                    d[i, 1] = d[i - 1, 1] + d[i - 1, 0];
                }
                else
                {
                    d[i, 0] = d[i - 1, 0];
                    d[i, 1] = d[i - 1, 1];
                }
            }
            Console.WriteLine(count);
        }

        public static void Main()
        {
            var task = new Task();
            #if DEBUG
            task.Solve();
            #else
            try
            {
                task.Solve();
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex);
            }
            #endif
        }
    }
}

namespace Codeforces
{
    public class Input
    {
        private static string _line;

        public static bool Next()
        {
            _line = Console.ReadLine();
            return _line != null;
        }

        public static bool Next(out long a)
        {
            var ok = Next();
            a = ok ? long.Parse(_line) : 0;
            return ok;
        }

        public static bool Next(out long a, out long b)
        {
            var ok = Next();
            if (ok)
            {
                var array = _line.Split(' ').Select(long.Parse).ToArray();
                a = array[0];
                b = array[1];
            }
            else
            {
                a = b = 0;
            }

            return ok;
        }

        public static bool Next(out long a, out long b, out long c)
        {
            var ok = Next();
            if (ok)
            {
                var array = _line.Split(' ').Select(long.Parse).ToArray();
                a = array[0];
                b = array[1];
                c = array[2];
            }
            else
            {
                a = b = c = 0;
            }
            return ok;
        }

        public static bool Next(out long a, out long b, out long c, out long d)
        {
            var ok = Next();
            if (ok)
            {
                var array = _line.Split(' ').Select(long.Parse).ToArray();
                a = array[0];
                b = array[1];
                c = array[2];
                d = array[3];
            }
            else
            {
                a = b = c = d = 0;
            }
            return ok;
        }

        public static bool Next(out long a, out long b, out long c, out long d, out long e)
        {
            var ok = Next();
            if (ok)
            {
                var array = _line.Split(' ').Select(long.Parse).ToArray();
                a = array[0];
                b = array[1];
                c = array[2];
                d = array[3];
                e = array[4];
            }
            else
            {
                a = b = c = d = e = 0;
            }
            return ok;
        }

        public static bool Next(out int a)
        {
            var ok = Next();
            a = ok ? int.Parse(_line) : 0;
            return ok;
        }

        public static bool Next(out int a, out int b)
        {
            var ok = Next();
            if (ok)
            {
                var array = _line.Split(' ').Select(int.Parse).ToArray();
                a = array[0];
                b = array[1];
            }
            else
            {
                a = b = 0;
            }

            return ok;
        }

        public static bool Next(out int a, out int b, out int c)
        {
            var ok = Next();
            if (ok)
            {
                var array = _line.Split(' ').Select(int.Parse).ToArray();
                a = array[0];
                b = array[1];
                c = array[2];
            }
            else
            {
                a = b = c = 0;
            }
            return ok;
        }

        public static bool Next(out int a, out int b, out int c, out int d)
        {
            var ok = Next();
            if (ok)
            {
                var array = _line.Split(' ').Select(int.Parse).ToArray();
                a = array[0];
                b = array[1];
                c = array[2];
                d = array[3];
            }
            else
            {
                a = b = c = d = 0;
            }
            return ok;
        }

        public static bool Next(out int a, out int b, out int c, out int d, out int e)
        {
            var ok = Next();
            if (ok)
            {
                var array = _line.Split(' ').Select(int.Parse).ToArray();
                a = array[0];
                b = array[1];
                c = array[2];
                d = array[3];
                e = array[4];
            }
            else
            {
                a = b = c = d = e = 0;
            }
            return ok;
        }

        public static bool Next(out int a, out int b, out int c, out int d, out int e, out int f)
        {
            var ok = Next();
            if (ok)
            {
                var array = _line.Split(' ').Select(int.Parse).ToArray();
                a = array[0];
                b = array[1];
                c = array[2];
                d = array[3];
                e = array[4];
                f = array[5];
            }
            else
            {
                a = b = c = d = e = f = 0;
            }
            return ok;
        }

        public static IEnumerable<long> ArrayLong()
        {
            return !Next() ? new List<long>() : _line.Split().Select(long.Parse);
        }

        public static IEnumerable<int> ArrayInt()
        {
            return !Next() ? new List<int>() : _line.Split().Select(int.Parse);
        }

        public static bool Next(out string value)
        {
            value = string.Empty;
            if (!Next()) return false;
            value = _line;
            return true;
        }
    }

    public class DisjointSet
    {
        private readonly int[] _parent;
        private readonly int[] _rank;
        public int Count { get; private set; }

        public DisjointSet(int count, int initialize = 0)
        {
            Count = count;
            _parent = new int[Count];
            _rank = new int[Count];
            for (var i = 0; i < initialize; i++) _parent[i] = i;
        }

        private DisjointSet(int count, int[] parent, int[] rank)
        {
            Count = count;
            _parent = parent;
            _rank = rank;
        }

        public void Add(int v)
        {
            _parent[v] = v;
            _rank[v] = 0;
        }

        public int Find_Recursive(int v)
        {
            if (_parent[v] == v) return v;
            return _parent[v] = Find(_parent[v]);
        }

        public int Find(int v)
        {
            if (_parent[v] == v) return v;
            var last = v;
            while (_parent[last] != last) last = _parent[last];
            while (_parent[v] != v)
            {
                var t = _parent[v];
                _parent[v] = last;
                v = t;
            }
            return last;
        }

        public int this[int v]
        {
            get { return Find(v); }
            set { Union(v, value); }
        }

        public void Union(int a, int b)
        {
            a = Find(a);
            b = Find(b);
            if (a == b) return;
            if (_rank[a] < _rank[b])
            {
                var t = _rank[a];
                _rank[a] = _rank[b];
                _rank[b] = t;
            }
            _parent[b] = a;
            if (_rank[a] == _rank[b]) _rank[a]++;
        }

        public int GetSetCount()
        {
            var result = 0;
            for (var i = 0; i < Count; i++)
            {
                if (_parent[i] == i) result++;
            }
            return result;
        }

        public DisjointSet Clone()
        {
            var rank = new int[Count];
            _rank.CopyTo(rank, 0);
            var parent = new int[Count];
            _parent.CopyTo(parent, 0);
            return new DisjointSet(Count, parent, rank);
        }

        public override string ToString()
        {
            return string.Join(",", _parent.Take(50));
        }
    }

    public class Matrix
    {
        public static Matrix Create(int i, int j)
        {
            var m = new Matrix(i, j);
            return m;
        }

        public static Matrix Create(int i)
        {
            var m = new Matrix(i, i);
            return m;
        }

        public long Modulo { get; set; }

        private readonly int _width;
        private readonly int _height;
        private readonly long[,] _data;
        public int Width { get { return _width; } }
        public int Height { get { return _height; } }

        private Matrix(int i, int j)
        {
            Modulo = 1000000000000000000L;
            _width = j;
            _height = i;
            _data = new long[_height, _width];
        }

        public static Matrix operator *(Matrix m1, Matrix m2)
        {
            if (m1.Width != m2.Height) throw new InvalidDataException("m1.Width != m2.Height");
            var m = Create(m2.Width, m1.Height);
            m.Modulo = m1.Modulo;
            for (var i = 0; i < m2.Width; i++)
                for (var j = 0; j < m1.Height; j++)
                {
                    for (var k = 0; k < m1.Width; k++)
                        m[j, i] += (m1[j, k] * m2[k, i]) % m1.Modulo;
                    m[j, i] %= m1.Modulo;
                }
            return m;
        }

        public static Matrix operator +(Matrix m1, Matrix m2)
        {
            var m = m1.Clone();
            for (var i = 0; i < m2.Width; i++)
                for (var j = 0; j < m1.Height; j++)
                    m[j, i] = (m[j, i] + m2[j, i]) % m1.Modulo;
            return m;
        }

        public static Matrix operator -(Matrix m1, Matrix m2)
        {
            var m = m1.Clone();
            for (var i = 0; i < m2.Width; i++)
                for (var j = 0; j < m1.Height; j++)
                    m[j, i] = (m[j, i] - m2[j, i]) % m1.Modulo;
            return m;
        }

        public static Matrix operator *(Matrix m1, long l)
        {
            var m = m1.Clone();
            for (var i = 0; i < m1.Width; i++)
                for (var j = 0; j < m1.Height; j++)
                    m[j, i] = (m[j, i] * l) % m.Modulo;
            return m;
        }

        public static Matrix operator *(long l, Matrix m1)
        {
            return m1 * l;
        }

        public static Matrix operator +(Matrix m1, long l)
        {
            var m = m1.Clone();
            for (var i = 0; i < m1.Width; i++)
                m[i, i] = (m[i, i] + l) % m.Modulo;
            return m;
        }

        public static Matrix operator +(long l, Matrix m1)
        {
            return m1 + l;
        }

        public static Matrix operator -(Matrix m1, long l)
        {
            return m1 + (-l);
        }

        public static Matrix operator -(long l, Matrix m1)
        {
            var m = m1.Clone() * -1;
            return m + l;
        }

        public Matrix BinPower(long l)
        {
            var n = 1;
            var m = Clone();
            var result = new Matrix(m.Height, m.Width) + 1;
            result.Modulo = m.Modulo;
            while (l != 0)
            {
                var i = l & ~(l - 1);
                l -= i;
                while (n < i)
                {
                    m = m * m;
                    n <<= 1;
                }
                result *= m;
            }
            return result;
        }

        public void Fill(long l)
        {
            l %= Modulo;
            for (var i = 0; i < _height; i++)
                for (var j = 0; j < _width; j++)
                    _data[i, j] = l;
        }

        public Matrix Clone()
        {
            var m = new Matrix(_width, _height);
            Array.Copy(_data, m._data, _data.Length);
            m.Modulo = Modulo;
            return m;
        }

        public long this[int i, int j]
        {
            get { return _data[i, j]; }
            set { _data[i, j] = value % Modulo; }
        }
    }

    public class RedBlackTree<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>
    {
        private readonly List<Node> _items;
        private Node _root;
        private readonly IComparer<TKey> _comparer;

        public RedBlackTree(int capacity = 10)
        {
            _items = new List<Node>(capacity);
            _comparer = Comparer<TKey>.Default;
        }

        private Node _search(TKey key)
        {
            var node = _root;
            while (node != null)
            {
                var i = _comparer.Compare(key, node.Key);
                if (i == 0) return node;
                node = i > 0 ? node.Right : node.Left;
            }
            return null;
        }

        /// <summary>
        /// Returns lower or equal key
        /// </summary>
        public TKey Lower(TKey key, TKey notFound = default(TKey))
        {
            var node = _root;
            Node good = null;
            while (node != null)
            {
                var i = _comparer.Compare(key, node.Key);
                if (i == 0) return key;
                if (i > 0)
                {
                    good = node;
                    node = node.Right;
                }
                else
                    node = node.Left;
            }
            return good == null ? notFound : good.Key;
        }

        /// <summary>
        /// Returns higher or equal key
        /// </summary>
        public TKey Higher(TKey key, TKey notFound = default(TKey))
        {
            var node = _root;
            Node good = null;
            while (node != null)
            {
                var i = _comparer.Compare(key, node.Key);
                if (i == 0) return key;
                if (i < 0)
                {
                    good = node;
                    node = node.Left;
                }
                else
                    node = node.Right;
            }
            return good == null ? notFound : good.Key;
        }

        /// <summary>
        /// Returns minimum region, which encloses given key
        /// </summary>
        public Tuple<TKey, TKey> Bounds(TKey key, TKey notFound = default(TKey))
        {
            var node = _root;
            Node lower = null;
            Node higher = null;
            while (node != null)
            {
                var i = _comparer.Compare(key, node.Key);
                if (i == 0) return new Tuple<TKey, TKey>(key, key);
                if (i < 0)
                {
                    higher = node;
                    node = node.Left;
                }
                else
                {
                    lower = node;
                    node = node.Right;
                }
            }
            return new Tuple<TKey, TKey>(lower == null ? notFound : lower.Key, higher == null ? notFound : higher.Key);
        }

        public void Add(TKey key, TValue value)
        {
            var node = new Node { Key = key, Value = value, Red = true };
            _items.Add(node);
            if (_root == null)
            {
                _root = node;
                _root.Red = false;
                return;
            }
            var x = _root;
            Node y = null;
            while (x != null)
            {
                y = x;
                x = _comparer.Compare(key, x.Key) > 0 ? x.Right : x.Left;
            }
            node.Parent = y;
            if (y == null) _root = node;
            else if (_comparer.Compare(key, y.Key) > 0) y.Right = node;
            else y.Left = node;
            InsertFixup(node);
        }

        private void InsertFixup(Node node)
        {
            while (true)
            {
                var parent = node.Parent;
                if (parent == null || !parent.Red || parent.Parent == null) break;
                var grand = parent.Parent;

                if (parent == grand.Left)
                {
                    var uncle = grand.Right;
                    if (uncle != null && uncle.Red) /* parent and uncle both red: we could swap colors of them with grand */
                    {
                        parent.Red = false;
                        uncle.Red = false;
                        grand.Red = true;
                        node = grand; /* and continue fixing tree for grand */
                    }
                    else if (node == parent.Right) /* we rotate around the parent */
                    {
                        node = parent;
                        RotateLeft(node);
                    }
                    else /* rotate around grand and switch colors of grand and parent */
                    {
                        parent.Red = false;
                        grand.Red = true;
                        RotateRight(grand);
                        if (grand == _root) _root = parent;
                        break; /* and finish since tree is fixed */
                    }
                }
                else
                {
                    if (_root.Parent != null) Debugger.Break();

                    var uncle = grand.Left;
                    if (uncle != null && uncle.Red) /* parent and uncle both red: we could swap colors of them with grand */
                    {
                        parent.Red = false;
                        uncle.Red = false;
                        grand.Red = true;
                        node = grand; /* and continue fixing tree for grand */
                    }
                    else if (node == parent.Left) /* we rotate around the parent */
                    {
                        node = parent;
                        RotateRight(node);
                    }
                    else /* rotate around grand and switch colors of grand and parent */
                    {
                        parent.Red = false;
                        grand.Red = true;
                        RotateLeft(grand);
                        if (grand == _root) _root = parent;
                        break; /* and finish since tree is fixed */
                    }
                }
            }
            _root.Red = false;
        }

        private void RotateLeft(Node node)
        {
            if (node.Right == null)
                throw new NotSupportedException("Cannot rotate left: right node is missing");
            var parent = node.Parent;
            var right = node.Right;
            right.Parent = parent;
            if (parent != null)
            {
                if (parent.Left == node) parent.Left = right;
                else parent.Right = right;
            }
            node.Right = right.Left;
            if (node.Right != null)
                node.Right.Parent = node;
            right.Left = node;
            node.Parent = right;
        }

        private void RotateRight(Node node)
        {
            if (node.Left == null)
                throw new NotSupportedException("Cannot rotate right: left node is missing");
            var parent = node.Parent;
            var left = node.Left;
            left.Parent = parent;
            if (parent != null)
            {
                if (parent.Left == node) parent.Left = left;
                else parent.Right = left;
            }
            node.Left = left.Right;
            if (node.Left != null)
                node.Left.Parent = node;
            left.Right = node;
            node.Parent = left;
        }

        public void Remove(TKey key)
        {
            var node = _search(key);
            if (node == null) return;

            var deleted = node.Left == null || node.Right == null ? node : Next(node);
            var child = deleted.Left ?? deleted.Right;
            if (child != null)
                child.Parent = deleted.Parent;
            if (deleted.Parent == null) /* root */
                _root = child;
            else if (deleted == deleted.Parent.Left)
                deleted.Parent.Left = child;
            else
                deleted.Parent.Right = child;
            if (node != deleted)
            {
                node.Key = deleted.Key;
                node.Value = deleted.Value;
            }

            if (!deleted.Red)
            {
                DeleteFixup(child, deleted.Parent);
            }
        }

        private void DeleteFixup(Node node, Node parent)
        {
            while (parent != null && (node == null || !node.Red))
            {
                if (node == parent.Left)
                {
                    var brother = parent.Right;
                    if (brother.Red)
                    {
                        brother.Red = false;
                        parent.Red = true;
                        RotateLeft(parent);
                        brother = parent.Right;
                    }
                    if ((brother.Left == null || !brother.Left.Red) && (brother.Right == null || !brother.Right.Red))
                    {
                        node = parent.Red ? _root : parent;
                        brother.Red = true;
                        parent.Red = false;
                    }
                    else
                    {
                        if (brother.Right == null || !brother.Right.Red)
                        {
                            if (brother.Left != null)
                                brother.Left.Red = false;
                            brother.Red = true;
                            RotateRight(brother);
                            brother = parent.Right;
                        }
                        if (!brother.Right.Red) Debugger.Break();
                        brother.Red = parent.Red;
                        parent.Red = false;
                        if (brother.Right != null)
                            brother.Right.Red = false;
                        RotateLeft(parent);
                        node = _root;
                    }
                }
                else
                {
                    var brother = parent.Left;
                    if (brother.Red)
                    {
                        brother.Red = false;
                        parent.Red = true;
                        RotateRight(parent);
                        brother = parent.Left;
                    }
                    if ((brother.Right == null || !brother.Right.Red) && (brother.Left == null || !brother.Left.Red))
                    {
                        node = parent.Red ? _root : parent;
                        brother.Red = true;
                        parent.Red = false;
                    }
                    else
                    {
                        if (brother.Left == null || !brother.Left.Red)
                        {
                            if (brother.Right != null)
                                brother.Right.Red = false;
                            brother.Red = true;
                            RotateLeft(brother);
                            brother = parent.Left;
                        }
                        brother.Red = parent.Red;
                        parent.Red = false;
                        if (brother.Left != null)
                            brother.Left.Red = false;
                        RotateRight(parent);
                        node = _root;
                    }
                }
                parent = node.Parent;
            }

            node.Red = false;
        }

        public TValue this[TKey key]
        {
            get
            {
                var node = _search(key);
                return node == null ? default(TValue) : node.Value;
            }
            set
            {
                var node = _search(key);
                if (node == null) Add(key, value);
                else node.Value = value;
            }
        }

        public bool Check()
        {
            int count;
            return _root == null || _root.Check(out count);
        }

        private Node Minimum(Node node)
        {
            var result = node;
            while (result.Left != null) result = result.Left;
            return result;
        }

        private Node Maximum(Node node)
        {
            var result = node;
            while (result.Right != null) result = result.Right;
            return result;
        }

        private Node Next(Node node)
        {
            Node result;
            if (node.Right != null)
            {
                result = Minimum(node.Right);
            }
            else
            {
                while (node.Parent != null && node.Parent.Right == node)
                    node = node.Parent;
                result = node.Parent;
            }
            return result;
        }

        private Node Previous(Node node)
        {
            Node result;
            if (node.Left != null)
            {
                result = Maximum(node.Left);
            }
            else
            {
                while (node.Parent != null && node.Parent.Left == node)
                    node = node.Parent;
                result = node.Parent;
            }
            return result;
        }

        public void Print()
        {
            var node = Minimum(_root);
            while (node != null)
            {
                Console.Write(node.Red ? '*' : ' ');
                Console.Write(node.Key);
                Console.Write('\t');
                node = Next(node);
            }
        }

        #region Nested classes

        [DebuggerDisplay("{ToString()}")]
        private sealed class Node
        {
            public TKey Key;
            public TValue Value;
            public bool Red;

            public Node Parent;
            public Node Left;
            public Node Right;

            public bool Check(out int count)
            {
                count = 0;
                if (Red)
                {
                    if (Left != null && Left.Red) return false;
                    if (Right != null && Right.Red) return false;
                }
                var left = 0;
                var right = 0;
                if (Left != null && !Left.Check(out left)) return false;
                if (Right != null && !Right.Check(out right)) return false;
                count = left + (Red ? 0 : 1);
                if (left != right)
                {
                    ;
                }
                return left == right;
            }

            public override string ToString()
            {
                return ToShortString(2);
            }

            private string ToShortString(int depth = 0)
            {
                return depth == 0
                    ? string.Format("{0}{1}", Red ? "*" : "", Key)
                    : string.Format("({1}<{0}>{2})", ToShortString(), Left == null ? "null" : Left.ToShortString(depth - 1), Right == null ? "null" : Right.ToShortString(depth - 1));
            }
        }

        #endregion

        #region IEnumerable

        public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
        {
            throw new NotImplementedException();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion
    }

    public class RedBlackTreeSimple<TKey>
    {
        private readonly List<Node> _items;
        private Node _root;
        private readonly IComparer<TKey> _comparer;

        public RedBlackTreeSimple(int capacity = 10)
        {
            _items = new List<Node>(capacity);
            _comparer = Comparer<TKey>.Default;
        }

        private Node _search(TKey key)
        {
            var node = _root;
            while (node != null)
            {
                var i = _comparer.Compare(key, node.Key);
                if (i == 0) return node;
                node = i > 0 ? node.Right : node.Left;
            }
            return null;
        }

        /// <summary>
        /// Returns lower or equal key
        /// </summary>
        public TKey Lower(TKey key, TKey notFound = default(TKey))
        {
            var node = _root;
            Node good = null;
            while (node != null)
            {
                var i = _comparer.Compare(key, node.Key);
                if (i == 0) return key;
                if (i > 0)
                {
                    good = node;
                    node = node.Right;
                }
                else
                    node = node.Left;
            }
            return good == null ? notFound : good.Key;
        }

        /// <summary>
        /// Returns higher or equal key
        /// </summary>
        public TKey Higher(TKey key, TKey notFound = default(TKey))
        {
            var node = _root;
            Node good = null;
            while (node != null)
            {
                var i = _comparer.Compare(key, node.Key);
                if (i == 0) return key;
                if (i < 0)
                {
                    good = node;
                    node = node.Left;
                }
                else
                    node = node.Right;
            }
            return good == null ? notFound : good.Key;
        }

        /// <summary>
        /// Returns minimum region, which encloses given key
        /// </summary>
        public Tuple<TKey, TKey> Bounds(TKey key, TKey notFound = default(TKey))
        {
            var node = _root;
            Node lower = null;
            Node higher = null;
            while (node != null)
            {
                var i = _comparer.Compare(key, node.Key);
                if (i == 0) return new Tuple<TKey, TKey>(key, key);
                if (i < 0)
                {
                    higher = node;
                    node = node.Left;
                }
                else
                {
                    lower = node;
                    node = node.Right;
                }
            }
            return new Tuple<TKey, TKey>(lower == null ? notFound : lower.Key, higher == null ? notFound : higher.Key);
        }

        public void Add(TKey key)
        {
            var node = new Node(key);
            _items.Add(node);
            if (_root == null)
            {
                _root = node;
                _root.Red = false;
                return;
            }
            var x = _root;
            Node y = null;
            while (x != null)
            {
                y = x;
                x = _comparer.Compare(key, x.Key) > 0 ? x.Right : x.Left;
            }
            node.Parent = y;
            if (y == null) _root = node;
            else if (_comparer.Compare(key, y.Key) > 0) y.Right = node;
            else y.Left = node;
            InsertFixup(node);
        }

        private void InsertFixup(Node node)
        {
            while (true)
            {
                var parent = node.Parent;
                if (parent == null || !parent.Red || parent.Parent == null) break;
                var grand = parent.Parent;

                if (parent == grand.Left)
                {
                    var uncle = grand.Right;
                    if (uncle != null && uncle.Red) /* parent and uncle both red: we could swap colors of them with grand */
                    {
                        parent.Red = false;
                        uncle.Red = false;
                        grand.Red = true;
                        node = grand; /* and continue fixing tree for grand */
                    }
                    else if (node == parent.Right) /* we rotate around the parent */
                    {
                        node = parent;
                        RotateLeft(node);
                    }
                    else /* rotate around grand and switch colors of grand and parent */
                    {
                        parent.Red = false;
                        grand.Red = true;
                        RotateRight(grand);
                        if (grand == _root) _root = parent;
                        break; /* and finish since tree is fixed */
                    }
                }
                else
                {
                    if (_root.Parent != null) Debugger.Break();

                    var uncle = grand.Left;
                    if (uncle != null && uncle.Red) /* parent and uncle both red: we could swap colors of them with grand */
                    {
                        parent.Red = false;
                        uncle.Red = false;
                        grand.Red = true;
                        node = grand; /* and continue fixing tree for grand */
                    }
                    else if (node == parent.Left) /* we rotate around the parent */
                    {
                        node = parent;
                        RotateRight(node);
                    }
                    else /* rotate around grand and switch colors of grand and parent */
                    {
                        parent.Red = false;
                        grand.Red = true;
                        RotateLeft(grand);
                        if (grand == _root) _root = parent;
                        break; /* and finish since tree is fixed */
                    }
                }
            }
            _root.Red = false;
        }

        private void RotateLeft(Node node)
        {
            if (node.Right == null)
                throw new NotSupportedException("Cannot rotate left: right node is missing");
            var parent = node.Parent;
            var right = node.Right;
            right.Parent = parent;
            if (parent != null)
            {
                if (parent.Left == node) parent.Left = right;
                else parent.Right = right;
            }
            node.Right = right.Left;
            if (node.Right != null)
                node.Right.Parent = node;
            right.Left = node;
            node.Parent = right;
        }

        private void RotateRight(Node node)
        {
            if (node.Left == null)
                throw new NotSupportedException("Cannot rotate right: left node is missing");
            var parent = node.Parent;
            var left = node.Left;
            left.Parent = parent;
            if (parent != null)
            {
                if (parent.Left == node) parent.Left = left;
                else parent.Right = left;
            }
            node.Left = left.Right;
            if (node.Left != null)
                node.Left.Parent = node;
            left.Right = node;
            node.Parent = left;
        }

        public void Remove(TKey key)
        {
            var node = _search(key);
            if (node == null) return;

            var deleted = node.Left == null || node.Right == null ? node : Next(node);
            var child = deleted.Left ?? deleted.Right;
            if (child != null)
                child.Parent = deleted.Parent;
            if (deleted.Parent == null) /* root */
                _root = child;
            else if (deleted == deleted.Parent.Left)
                deleted.Parent.Left = child;
            else
                deleted.Parent.Right = child;
            if (node != deleted)
            {
                node.Key = deleted.Key;
            }

            if (!deleted.Red)
            {
                DeleteFixup(child, deleted.Parent);
            }
        }

        private void DeleteFixup(Node node, Node parent)
        {
            while (parent != null && (node == null || !node.Red))
            {
                if (node == parent.Left)
                {
                    var brother = parent.Right;
                    if (brother.Red)
                    {
                        brother.Red = false;
                        parent.Red = true;
                        RotateLeft(parent);
                        brother = parent.Right;
                    }
                    if ((brother.Left == null || !brother.Left.Red) && (brother.Right == null || !brother.Right.Red))
                    {
                        node = parent.Red ? _root : parent;
                        brother.Red = true;
                        parent.Red = false;
                    }
                    else
                    {
                        if (brother.Right == null || !brother.Right.Red)
                        {
                            if (brother.Left != null)
                                brother.Left.Red = false;
                            brother.Red = true;
                            RotateRight(brother);
                            brother = parent.Right;
                        }
                        if (!brother.Right.Red) Debugger.Break();
                        brother.Red = parent.Red;
                        parent.Red = false;
                        if (brother.Right != null)
                            brother.Right.Red = false;
                        RotateLeft(parent);
                        node = _root;
                    }
                }
                else
                {
                    var brother = parent.Left;
                    if (brother.Red)
                    {
                        brother.Red = false;
                        parent.Red = true;
                        RotateRight(parent);
                        brother = parent.Left;
                    }
                    if ((brother.Right == null || !brother.Right.Red) && (brother.Left == null || !brother.Left.Red))
                    {
                        node = parent.Red ? _root : parent;
                        brother.Red = true;
                        parent.Red = false;
                    }
                    else
                    {
                        if (brother.Left == null || !brother.Left.Red)
                        {
                            if (brother.Right != null)
                                brother.Right.Red = false;
                            brother.Red = true;
                            RotateLeft(brother);
                            brother = parent.Left;
                        }
                        brother.Red = parent.Red;
                        parent.Red = false;
                        if (brother.Left != null)
                            brother.Left.Red = false;
                        RotateRight(parent);
                        node = _root;
                    }
                }
                parent = node.Parent;
            }

            node.Red = false;
        }

        private Node Minimum(Node node)
        {
            var result = node;
            while (result.Left != null) result = result.Left;
            return result;
        }

        private Node Maximum(Node node)
        {
            var result = node;
            while (result.Right != null) result = result.Right;
            return result;
        }

        private Node Next(Node node)
        {
            Node result;
            if (node.Right != null)
            {
                result = Minimum(node.Right);
            }
            else
            {
                while (node.Parent != null && node.Parent.Right == node)
                    node = node.Parent;
                result = node.Parent;
            }
            return result;
        }

        private Node Previous(Node node)
        {
            Node result;
            if (node.Left != null)
            {
                result = Maximum(node.Left);
            }
            else
            {
                while (node.Parent != null && node.Parent.Left == node)
                    node = node.Parent;
                result = node.Parent;
            }
            return result;
        }

        public void Print()
        {
            var node = Minimum(_root);
            while (node != null)
            {
                Console.Write(node.Red ? '*' : ' ');
                Console.Write(node.Key);
                Console.Write('\t');
                node = Next(node);
            }
        }

        #region Nested classes

        private sealed class Node
        {
            public Node(TKey key)
            {
                Key = key;
            }

            public TKey Key;
            public bool Red = true;

            public Node Parent;
            public Node Left;
            public Node Right;
        }

        #endregion
    }

    /// <summary>
    /// Fenwick tree for summary on the array
    /// </summary>
    public class FenwickSum
    {
        public readonly int Size;
        private readonly int[] _items;
        public FenwickSum(int size)
        {
            Size = size;
            _items = new int[Size];
        }

        public FenwickSum(IList<int> items)
            : this(items.Count)
        {
            for (var i = 0; i < Size; i++)
                Increase(i, items[i]);
        }

        private int Sum(int r)
        {
            if (r < 0) return 0;
            if (r >= Size) r = Size - 1;
            var result = 0;
            for (; r >= 0; r = (r & (r + 1)) - 1)
                result += _items[r];
            return result;
        }

        private int Sum(int l, int r)
        {
            return Sum(r) - Sum(l - 1);
        }

        public int this[int r]
        {
            get { return Sum(r); }
        }

        public int this[int l, int r]
        {
            get { return Sum(l, r); }
        }

        public void Increase(int i, int delta)
        {
            for (; i < Size; i = i | (i + 1))
                _items[i] += delta;
        }
    }

    /// <summary>
    /// Prime numbers
    /// </summary>
    public class Primes
    {
        /// <summary>
        /// Returns prime numbers in O(n)
        /// Returns lowet divisors as well
        /// Memory O(n)
        /// </summary>
        public static void ImprovedSieveOfEratosthenes(int n, out int[] lp, out List<int> pr)
        {
            lp = new int[n];
            pr = new List<int>();
            for (var i = 2; i < n; i++)
            {
                if (lp[i] == 0)
                {
                    lp[i] = i;
                    pr.Add(i);
                }
                foreach (var prJ in pr)
                {
                    var prIj = i * prJ;
                    if (prJ <= lp[i] && prIj <= n - 1)
                    {
                        lp[prIj] = prJ;
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }

        /// <summary>
        /// Returns prime numbers in O(n*n)
        /// </summary>
        public static void SieveOfEratosthenes(int n, out List<int> pr)
        {
            var m = 50000;//(int)(3l*n/(long)Math.Log(n)/2);
            pr = new List<int>();
            var f = new bool[m];
            for (var i = 2; i * i <= n; i++)
                if (!f[i])
                    for (var j = (long)i * i; j < m && j * j < n; j += i)
                        f[j] = true;
            pr.Add(2);
            for (var i = 3; i * i <= n; i += 2)
                if (!f[i])
                    pr.Add(i);
        }

        /// <summary>
        /// Greatest common divisor
        /// </summary>
        public static int Gcd(int x, int y)
        {
            while (y != 0)
            {
                var c = y;
                y = x % y;
                x = c;
            }
            return x;
        }

        /// <summary>
        /// Greatest common divisor
        /// </summary>
        public static long Gcd(long x, long y)
        {
            while (y != 0)
            {
                var c = y;
                y = x % y;
                x = c;
            }
            return x;
        }

        /// <summary>
        /// Greatest common divisor
        /// </summary>
        public static BigInteger Gcd(BigInteger x, BigInteger y)
        {
            while (y != 0)
            {
                var c = y;
                y = x % y;
                x = c;
            }
            return x;
        }

        /// <summary>
        /// Returns all divisors of n in O(√n)
        /// </summary>
        public static IEnumerable<long> GetDivisors(long n)
        {
            long r;
            while (true)
            {
                var x = Math.DivRem(n, 2, out r);
                if (r != 0) break;
                n = x;
                yield return 2;
            }
            var i = 3;
            while (i <= Math.Sqrt(n))
            {
                var x = Math.DivRem(n, i, out r);
                if (r == 0)
                {
                    n = x;
                    yield return i;
                }
                else i += 2;
            }
            if (n != 1) yield return n;
        }
    }

    /// <summary>
    /// Graph matching algorithms
    /// </summary>
    public class GraphMatching
    {
        #region Kuhn algorithm

        /// <summary>
        /// Kuhn algorithm for finding maximal matching
        /// in bipartite graph
        /// Vertexes are numerated from zero: 0, 1, 2, ... n-1
        /// Return array of matchings
        /// </summary>
        public static int[] Kuhn(List<int>[] g, int leftSize, int rightSize)
        {
            Debug.Assert(g.Count() == leftSize);

            var matching = new int[leftSize];
            for (var i = 0; i < rightSize; i++)
                matching[i] = -1;
            var used = new bool[leftSize];

            for (var v = 0; v < leftSize; v++)
            {
                Array.Clear(used, 0, leftSize);
                _kuhn_dfs(v, g, matching, used);
            }

            return matching;
        }

        private static bool _kuhn_dfs(int v, List<int>[] g, int[] matching, bool[] used)
        {
            if (used[v]) return false;
            used[v] = true;
            for (var i = 0; i < g[v].Count; i++)
            {
                var to = g[v][i];
                if (matching[to] == -1 || _kuhn_dfs(matching[to], g, matching, used))
                {
                    matching[to] = v;
                    return true;
                }
            }
            return false;
        }

        #endregion
    }
}

using System;

namespace TypA
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            string a = Console.ReadLine();
            char[] S = a.ToCharArray();
            int len = a.Length;
            int counter = 0;
            for (int i = 0; i < len; i++)
            {
                if (S[i] == 'Q')
                {
                    for (int j = i; j < len; j++)
                    {
                        if (S[j] == 'A')
                        {
                            for (int h = j; h < len; h++)
                            {
                                if (S[h] == 'Q') ++counter;
                            }
                        }
                    }
                }
            }
            Console.WriteLine(counter);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;

namespace Codeforces
{
	static class Program
	{

		static void Main()
		{
			var s = Console.ReadLine();
			var pref = new int[s.Length];
			if (s[0] == 'Q') pref[0] = 1;
			for (int i = 1; i < s.Length; i++)
			{
				if (s[i] == 'Q')
					pref[i] = pref[i - 1] + 1;

				else pref[i] = pref[i - 1];
			}
			int count = 0;
			for (int i = 0; i < s.Length; i++)
			{
				if(s[i] == 'A')
				{
					if (pref[s.Length - 1] - pref[i] == 0) break;
					count +=pref[i] * (pref[s.Length - 1] - pref[i]);
				}
			}
			Console.WriteLine(count);
			Console.ReadLine();
		}

		private static List<long> ReadLongList() => Console.ReadLine().Split().Select(long.Parse).ToList();
		private static List<int> ReadIntList() => Console.ReadLine().Split().Select(int.Parse).ToList();
		private static int[] ReadIntArray() => Console.ReadLine().Split().Select(int.Parse).ToArray();
		private static long[] ReadLongArray() => Console.ReadLine().Split().Select(long.Parse).ToArray();
		private static int ReadInt() => int.Parse(Console.ReadLine());
		private static long ReadLong() => long.Parse(Console.ReadLine());
	}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ProjectContest
{
    class Test
    {
        static void Main()
        {
            string str = Console.ReadLine();
            int i = 0;
            int kq = 0;
            while (i < str.Length)
            {
                if (str[i] == 'Q')
                {
                    int j = i + 1;
                    while (j < str.Length)
                    {
                        if (str[j] == 'A')
                        {
                            int k = j + 1;
                            while (k < str.Length)
                            {
                                if (str[k] == 'Q') kq++;
                                k++;
                            }
                        }
                        j++;
                    }
                }
                i++;
            }
            Console.WriteLine(kq);
        }
    }
}

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace olymp2
{
	class MainClass
	{

		public static void Main(string[] args)
		{
			//int[] input = Console.ReadLine()
			//                     .Split(new char[] { ' ' })
			//                     .Select(x => Convert.ToInt32(x))
			//                     .ToArray();

			//int n = input[0];
			//int d = input[1];

			//string map = Console.ReadLine();

			string S = Console.ReadLine();
			int count = 0;
			for (int x = 0; x < S.Length - 2; x++)
			{
				for (int y = x + 1; y < S.Length - 1; y++)
				{
					for (int z = y + 1; z < S.Length; z++)
					{
						if (S[x] == 'Q' && S[y] == 'A' && S[z] == 'Q')
						{
							count++;
						}
					}
				}
			}

			Console.WriteLine(count);

		}
	}
}

using System;

    class Program
    {
        static void Main(string[] args)
        {
            string s = Console.ReadLine();

            int sum=0;
            for (int i = 0; i < s.Length - 2; i++)
            {
                if (s[i] == 'Q')
                {
                for (int j=i+1;j<s.Length-1;j++)
                    if (s[j] == 'A')
                    {
                        for (int k = j + 1; k < s.Length; k++)
                        {
                            if (s[k] == 'Q')
                                sum++;

                        }
                    }
                }
            }
            Console.WriteLine(sum);

        }
    }

using System;
public class Program
{
    static public void Main()
    {
        string s = Console.ReadLine();
        int cnt = 0;
        for (int i = 0; i < s.Length; i++)
        {
            if (s[i] == 'Q')
            {
                for (int j = i + 1; j < s.Length; j++)
                {
                    if (s[j] == 'A')
                    {
                        for (int k = j + 1; k < s.Length; k++)
                        {
                            if (s[k] == 'Q')
                                cnt++;
                        }
                    }
                }
            }
        }
        Console.WriteLine(cnt);
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ContestSolutions
{
    public static class Problem894A
    {
        static void Main(string[] args)
        {
            var result = Solve(Console.ReadLine());
            Console.WriteLine(result);
        }

        public static int Solve(string s)
        {
            int count = 0;
            for (int i = 0; i < s.Length - 2; i++)
            {
                for (int j = i + 1; j < s.Length - 1; j++)
                {
                    for (int k = j + 1; k < s.Length; k++)
                    {
                        if (s[i] == 'Q' && s[j] == 'A' && s[k] == 'Q')
                        {
                            count++;
                        }
                    }
                }
            }
            return count;
        }

        #region helpers
        static int ReadInt()
        {
            return int.Parse(Console.ReadLine());
        }
        static int[] ReadInts()
        {
            int[] arr = Console.ReadLine().Split(' ')
                .Select(s => int.Parse(s))
                .ToArray();
            return arr;
        }
        static void ReadInts(out int i0, out int i1)
        {
            int[] arr = ReadInts();
            i0 = arr[0];
            i1 = arr[1];
        }
        static void ReadInts(out int i0, out int i1, out int i2)
        {
            int[] arr = ReadInts();
            i0 = arr[0];
            i1 = arr[1];
            i2 = arr[2];
        }
        static void WriteYesNo(bool value)
        {
            Console.WriteLine(value ? "YES" : "NO");
        }
        #endregion helpers
    }
}

using System;
using System.Collections.Generic;
namespace problem_solve_with_csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            long i = 0, j = 0, c = 0, k = 0;
            string s1 = Console.ReadLine();
            char[] s = s1.ToCharArray();
            for (i = 0; i < s.Length; i++)
                if (s[i] == 'Q')
                    for (j = i + 1; j < s.Length; j++)
                        if (s[j] == 'A')
                            for (k = j + 1; k < s.Length; k++)
                                if (s[k] == 'Q')
                                    c++;
            Console.WriteLine(c);
        }
        }
    }

#region Usings
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;

using static System.Math;
#endregion

// https://codeforces.com/contest/894/problem/A
public class A___QAQ
{
    static string s, t;
    static int n;
    static long solve(int idx, int i)
    {
        if (i == 3)
            return 1; // Reached end of QAQ

        if (idx == n)
            return 0; // Reached end of string

        long s1 = solve(idx + 1, i);
        if (s[idx] == t[i])
            s1 += solve(idx + 1, i + 1);

        return s1;
    }

    private static void Solve()
    {
        s = Read();
        n = s.Length;
        t = "QAQ";
        Write(solve(0, 0));
    }

    #region Main

    private static TextReader reader;
    private static TextWriter writer;

    public static void Main()
    {
#if DEBUG

        //reader = new StreamReader("C:\\Users\\Axel\\Desktop\\input.txt");
        reader = new StreamReader(Console.OpenStandardInput());
        writer = Console.Out;

        //writer = new StreamWriter("..\\..\\output.txt");
#else
        reader = new StreamReader(Console.OpenStandardInput());
        writer = new StreamWriter(Console.OpenStandardOutput());

        //reader = new StreamReader("input.txt");
        //writer = new StreamWriter("output.txt");
#endif
        try
        {
            Solve();

            //var thread = new Thread(new String_Task().Solve, 1024 * 1024 * 128);
            //thread.Start();
            //thread.Join();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
#if DEBUG
#else
            throw;
#endif
        }
        reader.Close();
        writer.Close();
    }

    #endregion Main

    #region Read / Write

    private static Queue<string> currentLineTokens = new Queue<string>();

    private static string[] ReadAndSplitLine()
    {
        return reader.ReadLine().Split(new[] { ' ', '\t', }, StringSplitOptions.RemoveEmptyEntries);
    }

    public static string Read()
    {
        while (currentLineTokens.Count == 0) currentLineTokens = new Queue<string>(ReadAndSplitLine()); return currentLineTokens.Dequeue();
    }

    public static int ReadInt()
    {
        return int.Parse(Read());
    }

    public static long ReadLong()
    {
        return long.Parse(Read());
    }

    public static double ReadDouble()
    {
        return double.Parse(Read(), CultureInfo.InvariantCulture);
    }

    public static int[] ReadIntArray()
    {
        return ReadAndSplitLine().Select(int.Parse).ToArray();
    }

    public static long[] ReadLongArray()
    {
        return ReadAndSplitLine().Select(long.Parse).ToArray();
    }

    public static double[] ReadDoubleArray()
    {
        return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray();
    }

    public static int[][] ReadIntMatrix(int numberOfRows)
    {
        int[][] matrix = new int[numberOfRows][]; for (int i = 0; i < numberOfRows; i++) matrix[i] = ReadIntArray(); return matrix;
    }

    public static int[][] ReadAndTransposeIntMatrix(int numberOfRows)
    {
        int[][] matrix = ReadIntMatrix(numberOfRows); int[][] ret = new int[matrix[0].Length][];
        for (int i = 0; i < ret.Length; i++) { ret[i] = new int[numberOfRows]; for (int j = 0; j < numberOfRows; j++) ret[i][j] = matrix[j][i]; }
        return ret;
    }

    public static string[] ReadLines(int quantity)
    {
        string[] lines = new string[quantity]; for (int i = 0; i < quantity; i++) lines[i] = reader.ReadLine().Trim(); return lines;
    }

    public static void WriteArray<T>(IEnumerable<T> array)
    {
        writer.WriteLine(string.Join(" ", array));
    }

    public static void Write(params object[] array)
    {
        WriteArray(array);
    }

    public static void WriteLines<T>(IEnumerable<T> array)
    {
        foreach (var a in array) writer.WriteLine(a);
    }

    private class SDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
        public new TValue this[TKey key]
        {
            get { return ContainsKey(key) ? base[key] : default(TValue); }
            set { base[key] = value; }
        }
    }

    private static T[] Init<T>(int size) where T : new()
    {
        var ret = new T[size]; for (int i = 0; i < size; i++) ret[i] = new T(); return ret;
    }

    #endregion Read / Write
}

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using static System.Math;

namespace Olymp
{
  public class ProblemSolver
  {
    private Tokenizer input;

    public void Solve()
    {
      var s = input.ReadLine();
      int n = s.Length;
      int c = 0;
      for (int i = 0; i < n - 2; i++)
        for (int j = i + 1; j < n - 1; j++)
          for (int k = j + 1; k < n; k++)
            if (s[i] == 'Q' && s[j] == 'A' && s[k] == 'Q')
              ++c;
      Console.Write(c);
    }

    public ProblemSolver(TextReader input)
    {
      this.input = new Tokenizer(input);
    }
  }

  #region Service classes

  public class Tokenizer
  {
    private TextReader reader;

    public int ReadInt()
    {
      var c = SkipWS();
      if (c == -1)
        throw new EndOfStreamException();
      bool isNegative = false;
      if (c == '-' || c == '+')
      {
        isNegative = c == '-';
        c = this.reader.Read();
        if (c == -1)
          throw new InvalidOperationException();
      }
      if (!char.IsDigit((char)c))
        throw new InvalidOperationException();
      int result = (char)c - '0';
      c = this.reader.Read();
      while (c > 0 && !char.IsWhiteSpace((char)c))
      {
        if (!char.IsDigit((char)c))
          throw new InvalidOperationException();
        result = result * 10 + (char)c - '0';
        c = this.reader.Read();
      }
      if (isNegative)
        result = -result;
      return result;
    }

    public string ReadLine()
    {
      return reader.ReadLine();
    }

    public long ReadLong()
    {
      return long.Parse(this.ReadToken());
    }

    public double ReadDouble()
    {
      return double.Parse(this.ReadToken(), CultureInfo.InvariantCulture);
    }

    public int[] ReadIntArray(int n)
    {
      int[] a = new int[n];
      for (int i = 0; i < n; i++)
        a[i] = ReadInt();
      return a;
    }

    public long[] ReadLongArray(int n)
    {
      long[] a = new long[n];
      for (int i = 0; i < n; i++)
        a[i] = ReadLong();
      return a;
    }

    public double[] ReadDoubleArray(int n)
    {
      double[] a = new double[n];
      for (int i = 0; i < n; i++)
        a[i] = ReadDouble();
      return a;
    }

    public string ReadToken()
    {
      var c = SkipWS();
      if (c == -1)
        return null;
      var sb = new StringBuilder();
      while (c > 0 && !char.IsWhiteSpace((char)c))
      {
        sb.Append((char)c);
        c = this.reader.Read();
      }
      return sb.ToString();
    }

    private int SkipWS()
    {
      int c = this.reader.Read();
      if (c == -1)
        return c;
      while (c > 0 && char.IsWhiteSpace((char)c))
        c = this.reader.Read();
      return c;
    }

    public Tokenizer(TextReader reader)
    {
      this.reader = reader;
    }
  }

  class Program
  {
    public static void Main(string[] args)
    {
      var solver = new ProblemSolver(Console.In);
      solver.Solve();
    }
  }

  #endregion
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string st = Console.ReadLine().Trim();
            int counter=0;

            for (int i = 0; i < st.Length; i++) {
                if (st[i]== 'Q' ) {
                    for (int j = i + 1; j < st.Length; j++) {
                        if (st[j] == 'A') {
                            for (int k = j + 1; k < st.Length; k++)
                            {
                                if (st[k] == 'Q') {
                                    counter++;
                                }
                            }
                        }
                    }
                }
            }
            Console.WriteLine(counter);
            Console.ReadLine();
        }
    }

}

using System;
using System.IO;
using System.Text;

namespace QAQ
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            writer.WriteLine(Solve(args));
            writer.Flush();
        }

        private static int Solve(string[] args)
        {
            string s = reader.ReadLine();

            int count = 0;
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] != 'Q')
                    continue;
                for (int j = i + 1; j < s.Length; j++)
                {
                    if (s[j] != 'A')
                        continue;
                    for (int k = j + 1; k < s.Length; k++)
                    {
                        if (s[k] == 'Q')
                            count++;
                    }
                }
            }

            return count;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string str = Console.ReadLine();
            int a = 0, q = 0, count = 0;
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] == 'Q')
                {
                    for (int j = i + 1; j < str.Length; j++)
                    {
                        if (str[j] == 'A') a++;
                        if (str[j] == 'Q') count += a;
                    }
                    a = 0;
                    q = 0;
                }

            }
            Console.Write(count);
            //Console.ReadKey();

        }
    }
}

using System;
namespace _894A
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            string s = Console.ReadLine();
            int result = 0;
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == 'Q')
                {
                    for (int j = i + 1; j < s.Length; j++)
                    {
                        if (s[j] == 'A')
                        {
                            for (int k = j + 1; k < s.Length; k++)
                            {
                                if (s[k] == 'Q')
                                    result++;
                            }
                        }
                    }
                }
            }
            Console.WriteLine(result);
        }
    }
}

using System;
using System.Globalization;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Codeforces
{
    class Program : ProgramBase
    {
        private const long commonMod = 1000000007;

        static void Main(string[] args)
        {
            var p = new Program();
            var x = p.Solve();
            p.Out(x);
        }
        object Solve()
        {
            var a = Read();
            long res = 0;
            for (int i = 0; i < a.Length; i++)
            {
                for (int j = i + 1; j < a.Length; j++)
                {
                    for (int k = j + 1; k < a.Length; k++)
                    {
                        if (a[i] == 'Q' && a[j] == 'A' && a[k] == 'Q')
                            res++;
                    }
                }
            }
            return res;
        }

        int nod(int a, int b)
        {
            if (a < b)
                return nod(b, a);
            if (b == 0)
                return a;
            return nod(b, a % b);
        }

        class End
        {
            public long Val { get; set; }
            public bool Left { get; set; }
        }
    }

    abstract class ProgramBase
    {
        bool? prod = null;
        StreamReader f;
        Queue<string> tokens;
        protected string FileName { private get; set; }

        protected string Read()
        {
            if (f == null)
                f = string.IsNullOrEmpty(FileName)
                    ? new StreamReader(Console.OpenStandardInput(1024 * 10), Encoding.ASCII, false, 1024 * 10)
                    : new StreamReader((GetProd() ? "" : "c:\\dev\\") + FileName);
            return f.ReadLine();
        }

        protected string ReadToken()
        {
            if (tokens == null || tokens.Count == 0)
                tokens = new Queue<string>(Read().Split(' '));
            return tokens.Dequeue();
        }

        protected long ReadLong() { return long.Parse(ReadToken()); }

        protected long[] ReadLongs()
        {
            var s = Read();
            if (s == "")
                return new long[0];
            //var i = s.IndexOf(' ');
            //return new[] { long.Parse(s.Substring(0, i)), long.Parse(s.Substring(i + 1)) };
            var tokens = s.Split(' ');
            var result = new long[tokens.Length];
            for (int i = 0; i < tokens.Length; i++)
                result[i] = long.Parse(tokens[i]);
            return result;
        }

        public void Out(object r)
        {
            if (string.IsNullOrEmpty(FileName))
                Console.WriteLine(r);
            else if (GetProd())
                File.WriteAllText(FileName, r.ToString());
            else
                Console.WriteLine(r);
        }

        private bool GetProd()
        {

            if (!prod.HasValue)
                try
                {
                    prod = Environment.MachineName != "RENADEEN-W7";
                }
                catch (Exception)
                {
                    prod = true;
                }
            return prod.Value;
        }
    }

    public static class Huj
    {
        public static T MinBy<T>(this IEnumerable<T> source, Func<T, double> func) where T : class
        {
            T result = null;
            var min = double.MaxValue;
            foreach (var item in source)
            {
                var current = func(item);
                if (min > current)
                {
                    min = current;
                    result = item;
                }
            }
            return result;
        }

        public static T MaxBy<T>(this IEnumerable<T> source, Func<T, double> func, T defaultValue = default(T))
        {
            T result = defaultValue;
            var max = double.MinValue;
            foreach (var item in source)
            {
                var current = func(item);
                if (max < current)
                {
                    max = current;
                    result = item;
                }
            }
            return result;
        }

        public static string JoinStrings(this IEnumerable<string> s, string d) { return string.Join(d, s.ToArray()); }

        public static string JoinStrings(this IEnumerable<int> s, string d) { return s.Select(x => x.ToString()).JoinStrings(d); }
        public static string JoinStrings(this IEnumerable<long> s, string d) { return s.Select(x => x.ToString()).JoinStrings(d); }
    }

}

using System;

namespace _447_1QAQ
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var line = Console.ReadLine();
            var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
            if (line == null) return;
            line.Trim(letters);
            var num = 0;
            for (var i = 0; i < line.Length; i++)
                for (var j = i + 1; j < line.Length; j++)
                    for (var l = j + 1; l < line.Length; l++)
                        if (line[i] == 'Q' && line[j] == 'A' && line[l] == 'Q')
                            num++;
            Console.WriteLine(num);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CompProg.codeforces
{
    public class QAQ
    {
        static void Main(string[] args)
        {
            string s = Console.ReadLine();
            int res = 0;
            int len = s.Length;
            for (int i = 0; i < len - 2; i++)
                for (int j = i + 1; j < len - 1; j++)
                    for (int k = j + 1; k < len; k++)
                        if (s[i] == 'Q' && s[j] == 'A' && s[k] == 'Q')
                            res++;
            Console.WriteLine(res);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace CF
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            using (var sr = new InputReader(Console.In))
//			using (var sr = new InputReader(new StreamReader("input.txt")))
            using (var sw = Console.Out)
                //using (var sw = new StreamWriter("output.txt"))
            using (var task = new Task(sr, sw)) {
                task.Solve();
//                Console.ReadKey();
            }
        }
    }

    internal class Task : IDisposable
    {
        private readonly InputReader sr;
        private readonly TextWriter sw;
        private bool isDispose;

        public Task(InputReader sr, TextWriter sw)
        {
            this.sr = sr;
            this.sw = sw;
        }

        public void Solve()
        {
            var s = sr.NextString();
            var count = 0;
            for (var i = 0; i < s.Length; i++) {
                for (var j = i + 1; j < s.Length; j++) {
                    for (var k = j + 1; k < s.Length; k++) {
                        if (s[i] == 'Q' && s[j] == 'A' && s[k] == 'Q') {
                            count++;
                        }
                    }
                }
            }

            sw.WriteLine(count);
        }

        ~Task()
        {
            Dispose(false);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (!isDispose) {
                if (disposing) {
                    if(sr != null)
                        sr.Dispose();

                    if(sw != null)
                        sw.Dispose();
                }

                isDispose = true;
            }
        }
    }
}

internal class InputReader : IDisposable
{
    private bool isDispose;
    private readonly TextReader sr;
    private string[] buffer;
    private int seek;

    public InputReader(TextReader stream)
    {
        sr = stream;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    public string NextString()
    {
        var result = sr.ReadLine();
        return result;
    }

    public int NextInt32()
    {
        return Int32.Parse(ReadNextToken());
    }

    private string ReadNextToken()
    {
        if (buffer == null || seek == buffer.Length) {
            seek = 0;
            buffer = NextSplitStrings();
        }

        return buffer[seek++];
    }

    public long NextInt64()
    {
        return Int64.Parse(ReadNextToken());
    }

    public int[] ReadArrayOfInt32()
    {
        return ReadArray(Int32.Parse);
    }

    public long[] ReadArrayOfInt64()
    {
        return ReadArray(Int64.Parse);
    }

    public string[] NextSplitStrings()
    {
        return NextString()
            .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    }

    public T[] ReadArray<T>(Func<string, CultureInfo, T> func)
    {
        return NextSplitStrings()
            .Select(s => func(s, CultureInfo.InvariantCulture))
            .ToArray();
    }

    public T[] ReadArrayFromString<T>(Func<string, CultureInfo, T> func, string str)
    {
        return
            str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(s => func(s, CultureInfo.InvariantCulture))
                .ToArray();
    }

    protected void Dispose(bool dispose)
    {
        if (!isDispose)
        {
            if (dispose)
                sr.Close();

            isDispose = true;
        }
    }

    ~InputReader()
    {
        Dispose(false);
    }
}

using System;

namespace Codeforces.NET
{

    class Program
    {
        static void Main(string[] args)
        {
            string line;
            line = Console.ReadLine();
            int answer = 0;
            for (int i = 0; i < line.Length-2; i++)
            {
                for (int j = i+1; j< line.Length-1; j++)
                {
                    for (int k = j + 1; k < line.Length; k++)
                    {
                        if (line[i] == 'Q' && line[j] == 'A' && line[k] == 'Q')
                        {
                            answer++;
                        }
                    }
                }
            }

            Console.WriteLine(answer);
            Console.ReadLine();
        }
    }
}

using System;

namespace A_QAQ
{
    class Program
    {
        static void Main(string[] args)
        {
            var line = Console.ReadLine();

            int result = 0;
            for (int i = 0; i < line.Length; ++i)
            {
                if (line[i] != 'Q') continue;
                for (int j = i + 1; j < line.Length; ++j)
                {
                    if (line[j] != 'A') continue;
                    for (int k = j+1; k < line.Length; ++k)
                    {
                        if (line[k] == 'Q')
                        {
                            result++;
                        }
                    }
                }
            }

            Console.WriteLine(result);
        }
    }
}

using System;
using System.Linq;
using System.Collections.Generic;
using System.Numerics;

public class Program
{
	public static void Main()
	{
		string s = Console.ReadLine();
		int count = 0;
		for(int i = 0; i < s.Length-2; i++)
		{
			if(s[i] == 'Q')
			{
				for(int j = i+1; j < s.Length - 1; j++)
				{
					if(s[j] == 'A')
					{
						for(int k = j+1; k < s.Length; k++)
						{
							if(s[k] == 'Q')
								count++;
						}
					}
				}
			}
		}
		Console.WriteLine(count);
	}
}

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.IO;
public class CodeForces
{
#if TEST
    // To set this go to Project -> Properties -> Build -> General -> Conditional compilation symbols: -> enter 'TEST' into text box.
    const bool testing = true;
#else
const bool testing = false;
#endif

    private static int Next()
    {
        int c;
        int res = 0;
        do
        {
            c = reader.Read();
            if (c == -1)
                return res;
        } while (c < '0' || c > '9');
        res = c - '0';
        while (true)
        {
            c = reader.Read();
            if (c < '0' || c > '9')
                return res;
            res *= 10;
            res += c - '0';
        }
    }
    private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024 * 10), Encoding.ASCII, false, 1024 * 10);
    private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024 * 10), Encoding.ASCII, 1024 * 10);

    static void program(TextReader input)
    {
        var s = input.ReadLine();
        long res = 0;
        for(var i = 0; i < s.Length; i++)
        {
            if(s[i] != 'A')
            {
                continue;
            }
            for(var j = i - 1; j >= 0; j--)
            {
                if(s[j] != 'Q')
                {
                    continue;
                }
                for(var k = i + 1; k < s.Length; k++)
                {
                    if(s[k] == 'Q')
                    {
                        res++;
                    }
                }
            }
        }

        writer.WriteLine(res);
        writer.Flush();
    }

    public static void Main(string[] args)
    {
        if (!testing)
        { // set testing to false when submiting to codeforces
            program(Console.In); // write your program in 'program' function (its your new main !)
            return;
        }

        Console.WriteLine("Test Case(1) => expected :");
        Console.WriteLine("4\n");
        Console.WriteLine("Test Case(1) => found    :");
        program(new StringReader("QAQAQYSYIOIWIN\n"));
        Console.WriteLine();
        Console.ReadLine();

        Console.WriteLine("Test Case(2) => expected :");
        Console.WriteLine("3\n");
        Console.WriteLine("Test Case(2) => found    :");
        program(new StringReader("QAQQQZZYNOIWIN\n"));
        Console.WriteLine();
        Console.ReadLine();

    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

class Program
{
	static void Main(string[] args)
	{
		var S = Console.ReadLine();
		int ans = 0;
		for (int i = 0; i < S.Length; i++)
		{
			for (int j = i + 1; j < S.Length; j++)
			{
				for (int k = j + 1; k < S.Length; k++)
				{
					if (S[i] == 'Q' && S[j] == 'A' && S[k] == 'Q')
					{
						ans++;
					}
				}
			}
		}
		Console.WriteLine(ans);
	}
}

using System;

namespace CF_Problems_894_A // QAQ
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = Console.ReadLine();

            int ans = 0;
            int n = s.Length;

            for (int i = 0; i < n - 2; i++)
            {
                if (s[i] == 'Q')
                {
                    for (int j = i+1; j < n - 1; j++)
                    {
                        if (s[j] == 'A')
                        {
                            for (int k = j + 1; k < n; k++)
                            {
                                if (s[k] == 'Q')
                                    ans++;
                            }
                        }
                    }
                }
            }

            Console.WriteLine(ans);
        }
    }
}

//#define FILE_INPUT
using System;
using System.Collections.Generic;
using System.Text;
//using System.Text.RegularExpressions;
//using System.Linq;
//using System.Collections;
//using System.Collections.Specialized;
//using System.Numerics;

//2078. Игра в боулинг

namespace CodeForces
{

    class Solution
    {
        static void Main()
        {
            new Solution().solve();
            Pause();
        }

        private void solve()
        {
            int ans = 0;
            var s = cin.Line();
            int[] qc = new int[100];
            int[] qac = new int[100];

            qc[0] = (s[0] == 'Q' ? 1 : 0);
            for (int i = 1; i < s.Length; i++)
            {
                qc[i] = qc[i - 1] + (s[i] == 'Q' ? 1 : 0);
                qac[i] = qac[i - 1] + (s[i] == 'A' ? qc[i - 1] : 0);
            }

            for (int i = 2; i < s.Length; i++)
            {
                if (s[i] == 'Q')
                {
                    ans += qac[i - 1];
                }
            }

            Console.WriteLine(ans);
        }

        #region Tools

        [System.Diagnostics.Conditional("DEBUG")]
        static private void DebugOut<T>(string format, T arg)
        {
            Console.WriteLine(format, arg);
        }

        [System.Diagnostics.Conditional("DEBUG")]
        static private void DebugOut(string format, params object[] arg)
        {
            Console.WriteLine(format, arg);
        }

        [System.Diagnostics.Conditional("DEBUG")]
        static private void DebugStop()
        {
            Console.ReadKey(true);
        }

        private static T[] ga<T>(int n, Func<int, T> f)
        {
            var a = new T[n];
            for (int i = 0; i < n; ++i) a[i] = f(i);
            return a;
        }

        private static T[] ga<T>(int n, Func<T> f)
        {
            var a = new T[n];
            for (int i = 0; i < n; ++i) a[i] = f();
            return a;
        }

        private static int gi()
        { return cin.gi(); }

        private static List<T> gl<T>(int n, Func<int, T> f)
        {
            var a = new List<T>(n + 1);
            for (int i = 0; i < n; ++i) a.Add(f(i));
            return a;
        }

        [System.Diagnostics.Conditional("DEBUG")]
        static private void Pause()
        {
            Console.WriteLine("[debug]");
            Console.ReadKey(true);
        }
        private static void swap<T>(ref T a, ref T b)
        {
            T t = a; a = b; b = t;
        }

        #endregion Tools
    }

    class cin
    {
        public static int error { get; set; }

        static cin()
        {
#if (ONLINE_JUDGE || !FILE_INPUT)
                reader = new System.IO.BufferedStream(Console.OpenStandardInput(1024 * 10), 1024 * 10);
#else
            reader = new System.IO.BufferedStream((new System.IO.StreamReader("input.txt")).BaseStream, 1024 * 16);
#endif
        }

        public static System.IO.BufferedStream reader { get; private set; }
        public static char Char() { return (char)reader.ReadByte(); }

        public static int gi()
        {
            for (int c = read(); c != -1; c = read())
            {
                switch ((char)c)
                {
                    case '-':
                        return -(int)gu(read());

                    default:
                        if (char.IsDigit((char)c)) return (int)gu(c);
                        break;
                }
            }

            throw new System.IO.IOException();
        }

        public static uint gu()
        {
            return gu(read());
        }

        public static string Line()
        {
            int c;
            StringBuilder sb = new StringBuilder();
            for (c = read(); c != 10; c = read()) sb.Append((char)c);
            if (sb.Length > 0 && sb[sb.Length - 1] == 13) sb.Length -= 1;
            return sb.ToString();
        }

        public static int rawLine(byte[] a, int offset = 0)
        {
            int c, i = offset;
            for (c = reader.ReadByte(); c != 10; c = reader.ReadByte()) a[i++] = (byte)c;
            if (i > 0 && a[i - 1] == 13) i--;
            return i - offset;
        }

        public static int rawLine(char[] a, int offset = 0)
        {
            Array.ConvertAll(a, Convert.ToInt16);

            int c, i = offset;
            for (c = read(); c != 10 && c != -1; c = read()) a[i++] = (char)c;
            if (i > 0 && a[i - 1] == 13) i--;
            return i;
        }

        public static int rawLine<T>(T[] a, Func<int, T> converter) where T : IComparable<int>
        {
            int c, i = 0;
            for (c = read(); c != 10; c = read()) a[i++] = converter(c);
            if (i > 0 && a[i - 1].CompareTo(13) == 0) i--;
            return i;
        }

        public static string Scan()
        {
            int c;
            StringBuilder sb = new StringBuilder();
            do c = read(); while (c < 33 || c > 126);
            for (; c >= 33 && c <= 126; c = read()) sb.Append((char)c);
            return sb.ToString();
        }
        public static void Scan(Action<int> f)
        {
            int c;
            StringBuilder sb = new StringBuilder();
            do c = read(); while (c < 33 || c > 126);
            for (; c >= 33 && c <= 126; c = read()) f(c);
        }

        private static uint gu(int first)
        {
            int c;
            uint u = (uint)first - '0';
            for (c = read(); c != -1 && char.IsDigit((char)c); c = read())
            {
                u = 10 * u + (uint)c - '0';
            }

            error = c;
            return u;
        }

        private static int read() { return reader.ReadByte(); }

        public static bool NewLine()
        {
            int c = error;
            if (c != 10) do c = reader.ReadByte(); while (c != -1 && c != 10);
            return c != -1;
        }
    }
}

using System;

class Program {

    static int[] qaq;
    static int[] qa;
    static int[] q;
    static string reduced;

    static void Main(string[] args) {
        qaq = new int[111];
        qa = new int[111];
        q = new int[111];
        for (int i = 0; i < 111; i++) {
            qaq[i] = qa[i] = q[i] = -1;
        }
        string s = Console.ReadLine();
        reduced = "";
        for (int i = 0; i < s.Length; i++) {
            if (s[i] == 'A' || s[i] == 'Q') {
                reduced += s[i];
            }
        }
        Console.WriteLine(Qaq(reduced.Length));
    }

    static int Qaq(int n) {
        if (n < 3)
            return 0;
        if (qaq[n] != -1)
            return qaq[n];
        if (reduced[n-1] == 'Q') {
            return qaq[n] = Qa(n - 1) + Qaq(n - 1);
        }
        else {
            return qaq[n] = Qaq(n - 1);
        }
    }

    static int Qa(int n) {
        if (n < 2)
            return 0;
        if (qa[n] != -1)
            return qa[n];
        if (reduced[n - 1] == 'A') {
            return qa[n] = Q(n - 1) + Qa(n - 1);
        }
        else {
            return qa[n] = Qa(n - 1);
        }
    }

    static int Q(int n) {
        if (n == 0)
            return 0;
        if (q[n] != -1)
            return q[n];
        if (reduced[n - 1] == 'Q') {
            return q[n] = 1 + Q(n - 1);
        }
        else {
            return q[n] = Q(n - 1);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _900
{
    class Program
    {
        static void Main(string[] args)
        {
            string qaqstring = Console.ReadLine();

            int qaqsum = 0;
            for(int i = 0; i < qaqstring.Length; ++i)
            {
                    for(int j = i; j < qaqstring.Length; ++j)
                    {
                            for(int k = j; k < qaqstring.Length; ++k)
                            {
                                if (qaqstring[i] == 'Q' && qaqstring[j] == 'A' && qaqstring[k] == 'Q')
                                    qaqsum++;
                            }
                    }
            }
            Console.WriteLine(qaqsum);

        }

    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp88
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = Console.ReadLine();
            int sum = 0;
            if (s.Length >= 1 & s.Length <= 100)
            {

                for (int i = 0; i < s.Length; i++)
                {

                        if (s[i] == 'Q')
                        {
                            for (int p = i + 1; p < s.Length; p++)
                            {
                                if (s[p] == 'A')
                                {
                                    for (int z = p + 1; z < s.Length; z++)
                                    {
                                        if (s[z] == 'Q')
                                        {
                                            sum++;
                                        }
                                    }
                                }
                            }
                        }

                }
            }
            Console.WriteLine(sum);

        }
    }
}

using System;
using System.Linq;

public class Program
{
    public static void Main()
    {
        string s = Console.ReadLine();
        int count = 0;
        for (var q1 = 0; q1 < s.Length; ++q1)
        for (var a = q1 + 1; a < s.Length; ++a)
        for (var q2 = a + 1; q2 < s.Length; ++q2)
        {
            if (s[q1] != 'Q' || s[a] != 'A' || s[q2] != 'Q')
                continue;
            ++count;
        }

        Console.WriteLine(count);
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {

            int number_of_subsequences=0;
            string sequence = System.Console.ReadLine();

            for (int i=0;i<sequence.Length;i++)
            {
               if (sequence[i]=='Q')
                   for(int j=i+1;j<sequence.Length;j++)
                   {
                       if(sequence[j]=='A')
                       {
                            for(int k=j+1;k<sequence.Length;k++)
                            {
                                if(sequence[k]=='Q')
                                {
                                    number_of_subsequences++;
                                }
                            }
                       }
                   }

            }

            System.Console.WriteLine(number_of_subsequences);

        }
    }
}

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Reverse_Array
{
    class Program
    {
        static void Main(string[] args)
        {
            string line = Console.ReadLine();
            long count = 0;

            ArrayList store = new ArrayList();
            char[] array = new char[line.Length];
            for (int i = 0; i < line.Length; i++)
            {
                array[i] = line[i];
            }

            for (int x = 0; x < line.Length; x++)
            {
                if (array[x] == 'Q')
                {
                    store.Add(array[x]);
                    for (int j = x+1; j < line.Length; j++)
                    {
                        if (array[j] == 'A')
                        {
                            store.Add(array[j]);
                            for (int k = j+1; k < line.Length; k++)
                            {
                                if (array[k] == 'Q')
                                {
                                    count++;
                                    store.Add(array[k]);
                                }
                            }
                        }

                    }

                }
            }

            Console.WriteLine(count);
        }
    }
}

using System;

namespace contest894a
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            string capitals = Console.ReadLine();

            int[] numOfQs = new int[capitals.Length];

            int Qs = 0;

            for (int i = 0; i < capitals.Length; i++)
            {
                if (capitals[i].ToString() == "Q")
                {
                    Qs++;
                    numOfQs[i] = Qs;
                }
                else{
                    numOfQs[i] = Qs;
                }
            }

            int totalMatches = 0;

			for (int c = 0; c < capitals.Length - 1; c++)
			{
                if (capitals[c].ToString() == "A" && c != 0 )
				{
                    totalMatches += numOfQs[c - 1] * (Math.Abs(numOfQs[capitals.Length - 1] - numOfQs[c - 1]));
				}
			}

            Console.WriteLine(totalMatches);
        }
    }
}

using System;

namespace _894
{
    class Program
    {
        static void Main(string[] args)
        {
            string qaq = Console.ReadLine();
            int count = 0;

            for(int ii = 0; ii < qaq.Length; ++ii)
            {
                for(int jj = ii; jj < qaq.Length; ++jj)
                {
                    for(int kk = jj; kk < qaq.Length; ++kk)
                    {
                        if(qaq[ii] == 'Q' && qaq[jj] == 'A' && qaq[kk] == 'Q')
                        {
                            ++count;
                        }
                    }
                }
            }
            Console.WriteLine(count);
        }
    }
}

using System;
namespace ConsoleApp17{

    class Program
    {

        static void Main()
        {
            var line = Console.ReadLine();
            var count = 0;
            for (int i = 0; i < line.Length; i++)
            {
                if (line[i].Equals('Q'))
                {
                    for (int j = i; j < line.Length; j++)
                    {
                        if (line[j].Equals('A'))
                        {
                            for (int k = j; k <line.Length; k++)
                            {
                                if (line[k].Equals('Q'))
                                {
                                    count++;
                                }
                            }
                        }
                    }
                }
            }
            Console.WriteLine(count);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;

namespace Codeforces
{
    class Program : IDisposable
    {
        private static readonly TextReader textReader = new StreamReader(Console.OpenStandardInput());
        private static readonly TextWriter textWriter = new StreamWriter(Console.OpenStandardOutput());

        private void Solve()
        {
            var s = Console.ReadLine();
            int q = 0;
            int qa = 0;
            int ans = 0;
            foreach (var ch in s)
            {
                if (ch == 'A')
                {
                    qa += q;
                }
                else if (ch == 'Q')
                {
                    q++;
                    ans += qa;
                }
            }
            Console.WriteLine(ans);
        }

        int BinarySearch(int[] arr, int q)
        {
            var l = 0;
            var r = arr.Length - 1;
            //int med = -1;
            while (l <= r)
            {
                var med = l + (r - l) / 2;
                if (arr[med] == q)
                {
                    return med;
                }
                if (q < arr[med])
                {
                    r = med - 1;
                }
                else
                {
                    l = med + 1;
                }
            }
            return -1;
        }

        static void Main(string[] args)
        {
            var p = new Program();
            p.Solve();
            //Console.ReadLine();
            //p.Dispose();
            //////////////////////////////
        }

        #region Helpers

        private static int ReadInt()
        {
            return int.Parse(textReader.ReadLine());
        }

        private static long ReadLong()
        {
            return long.Parse(textReader.ReadLine());
        }

        private static int[] ReadIntArray()
        {
            return textReader.ReadLine().Split(' ').Select(int.Parse).ToArray();
        }
        #endregion

        public void Dispose()
        {
            textReader.Close();
            textWriter.Close();
        }
    }
}

using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

class Program
{
	static int BinSearch(List<int> a, int el)
	{
		int l = -1, r = a.Count;
		while (r - l > 1)
		{
			int m = (l + r) / 2;
			if (a[m] < el)
				l = m;
			else
				r = m;
		}
		return r;
	}
	static void Main()
	{
		string s = Console.ReadLine();
		List<int> Q = new List<int>(), A = new List<int>();
		for (int i = 0; i < s.Length; i++)
			if (s[i] == 'Q')
				Q.Add(i);
			else if (s[i] == 'A')
				A.Add(i);
		long ans = 0;
		for (int i = 0; i < Q.Count; i++)
			for (int j = BinSearch(A, Q[i]); j < A.Count; j++)
				ans += Q.Count - BinSearch(Q, A[j]);
		Console.WriteLine(ans);
		Console.ReadLine();
	}
}

using System;

class A894 {
    public static void Main() {
        var s = Console.ReadLine();
        int q0 = 0, q1 = 0, q2 = 0;
        foreach (var i in s)
            if (i == 'Q') { ++q0; q2 += q1; }
            else if (i == 'A') { q1 += q0; }
        Console.WriteLine(q2);
    }
}

using System;
namespace ConsoleApplication
{
    sealed class Program
    {
        static void Main(string[] args)
        {
            var s = Console.ReadLine();
            var result = 0;
            for (int i = 0; i < s.Length; i++)
            {
                for (int j = i + 1; j < s.Length; j++)
                {
                    for (int z = j + 1; z < s.Length; z++)
                    {
                        if (s[i] == 'Q' && s[j] == 'A' && s[z] == 'Q')
                            result++;
                    }
                }
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Solution
{
    class Program
    {
        static void Main(string[] args)
        {
            var qaq = Console.ReadLine().Where( x => (x == 'Q' || x == 'A')).ToArray();
            var total = 0;
            for (var i = 0; i < qaq.Length; i ++)
            {
                if (qaq[i] == 'A')
                {
                    var qLeft = 0; var qRight = 0;
                    for (var k = 0; k < qaq.Length; k++)
                        if (qaq[k]=='Q'){
                            if (k < i) qLeft++;
                            if (k > i) qRight++;
                        }
                    total += Math.Min(qLeft, qRight) + Math.Max(qLeft, qRight) - 1;
                }
            }
            Console.WriteLine(total);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Solution
{
    class Program
    {
        static void Main(string[] args)
        {
            var qaq = Console.ReadLine().Where( x => (x == 'Q' || x == 'A')).ToArray();
            var total = 0;
            for (var i = 0; i < qaq.Length; i ++)
            {
                if (qaq[i] == 'A')
                {
                    var qLeft = 0; var qRight = 0;
                    for (var k = 0; k < qaq.Length; k++)
                        if (qaq[k]=='Q'){
                            if (k < i) qLeft++;
                            if (k > i) qRight++;
                        }
                    if (qLeft > 0 && qRight > 0)
                        total += Math.Min(qLeft, qRight) + Math.Max(qLeft, qRight) - 1;
                }
            }
            Console.WriteLine(total);
        }
    }
}

using System;
class a
{
    static void Main()
    {
        var s = Console.ReadLine();
        string p = "";
        for (int i = 0; i < s.Length; i++) if (s[i] == 'Q' || s[i] == 'A') p += s[i];
        int r = 0;
        Console.WriteLine(p);
        for (int i = 0; i < p.Length; i++)
        {
            for (int j = i+1; j < p.Length; j++)
            {
                for (int k = j+1; k < p.Length; k++)
                {
                    if (p[i].ToString() + p[j].ToString() + p[k].ToString() == "QAQ") r++;
                }
            }
        }
        Console.WriteLine(r);
    }
}

using System;
class a
{
    static void Main()
    {
        var s = Console.ReadLine();
        string p = "";
        for (int i = 0; i < s.Length; i++) if (s[i] == 'Q' || s[i] == 'A') p += s[i];
        int r = 0;
        if(p == "QAQ")
        {
            Console.WriteLine(1);
            return;
        }
        for (int i = 0; i < p.Length; i++)
        {
            string temp = p;
            temp = R(temp, i);
            if (Q(temp)) r++;
        }
        Console.WriteLine(r);

    }
    static bool Q(string x)
    {
        int q1 = x.IndexOf('Q');
        int a = x.IndexOf('A');
        if (a < 0) return false;
        int q2 = a + x.Substring(a, x.Length - a).IndexOf('Q') + 1;
        return (q1 < a && a < q2);
    }
    static string R(string x, int index)
    {
        x = x.Substring(0, index) + "*" + x.Substring(index + 1, x.Length - index - 1);
        return x;
    }
}

using System;
using System.Collections.Generic;
using System.Linq;

namespace Codeforces
{
	static class Program
	{

		static void Main()
		{
			var s = Console.ReadLine();
			var pref = new int[s.Length];
			if (s[0] == 'Q') pref[0] = 1;
			for (int i = 1; i < s.Length; i++)
			{
				if (s[i] == 'Q')
					pref[i] = pref[i - 1] + 1;

				else pref[i] = pref[i - 1];
			}
			int count = 0;
			for (int i = 0; i < s.Length; i++)
			{
				if(s[i] == 'A')
				{
					if (pref[s.Length - 1] - pref[i] == 0) break;
					count += Math.Max(pref[i], pref[s.Length - 1] - pref[i]);
				}
			}
			Console.WriteLine(count);
			Console.ReadLine();
		}

		private static List<long> ReadLongList() => Console.ReadLine().Split().Select(long.Parse).ToList();
		private static List<int> ReadIntList() => Console.ReadLine().Split().Select(int.Parse).ToList();
		private static int[] ReadIntArray() => Console.ReadLine().Split().Select(int.Parse).ToArray();
		private static long[] ReadLongArray() => Console.ReadLine().Split().Select(long.Parse).ToArray();
		private static int ReadInt() => int.Parse(Console.ReadLine());
		private static long ReadLong() => long.Parse(Console.ReadLine());
	}
}

using System;
using System.Collections.Generic;
using System.Linq;

namespace Codeforces
{
	static class Program
	{

		static void Main()
		{
			var s = Console.ReadLine();
			var pref = new int[s.Length];
			if (s[0] == 'Q') pref[0] = 1;
			for (int i = 1; i < s.Length; i++)
			{
				if (s[i] == 'Q')
					pref[i] = pref[i - 1] + 1;

				else pref[i] = pref[i - 1];
			}
			int count = 0;
			for (int i = 0; i < s.Length; i++)
			{
				if(s[i] == 'A')
				{
					count += Math.Min(pref[i], pref[s.Length - 1] - pref[i]);
				}
			}
			Console.WriteLine(count);
			Console.ReadLine();
		}

		private static List<long> ReadLongList() => Console.ReadLine().Split().Select(long.Parse).ToList();
		private static List<int> ReadIntList() => Console.ReadLine().Split().Select(int.Parse).ToList();
		private static int[] ReadIntArray() => Console.ReadLine().Split().Select(int.Parse).ToArray();
		private static long[] ReadLongArray() => Console.ReadLine().Split().Select(long.Parse).ToArray();
		private static int ReadInt() => int.Parse(Console.ReadLine());
		private static long ReadLong() => long.Parse(Console.ReadLine());
	}
}

using System;
using System.Collections.Generic;
using System.Linq;

namespace Codeforces
{
	static class Program
	{

		static void Main()
		{
			var s = Console.ReadLine();
			var pref = new int[s.Length];
			if (s[0] == 'Q') pref[0] = 1;
			for (int i = 1; i < s.Length; i++)
			{
				if (s[i] == 'Q')
					pref[i] = pref[i - 1] + 1;

				else pref[i] = pref[i - 1];
			}
			int count = 0;
			for (int i = 0; i < s.Length; i++)
			{
				if(s[i] == 'A')
				{
					count += Math.Max(pref[i], pref[s.Length - 1] - pref[i]);
				}
			}
			Console.WriteLine(count);
			Console.ReadLine();
		}

		private static List<long> ReadLongList() => Console.ReadLine().Split().Select(long.Parse).ToList();
		private static List<int> ReadIntList() => Console.ReadLine().Split().Select(int.Parse).ToList();
		private static int[] ReadIntArray() => Console.ReadLine().Split().Select(int.Parse).ToArray();
		private static long[] ReadLongArray() => Console.ReadLine().Split().Select(long.Parse).ToArray();
		private static int ReadInt() => int.Parse(Console.ReadLine());
		private static long ReadLong() => long.Parse(Console.ReadLine());
	}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string str = Console.ReadLine();
            int countQ = 0, countA = 0;
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] == 'Q') countQ++;
                if (str[i] == 'A') countA++;
            }
            Console.Write(countA * (countQ - 1));
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string str = Console.ReadLine();
            int countQ = 0, countA = 0, count = 0;
            for (int i = 0; i < str.Length; i++)
            {

                if (str[i] == 'A') countA++;
                if (str[i] == 'Q' && countA > 0) countQ++;
            }
            Console.Write(countA * countQ);
           // Console.ReadKey();

        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string str = Console.ReadLine();
            int countQ = 0, countA = 0, count = 0;
            for (int i = 0; i < str.Length; i++)
            {

                if (str[i] == 'A') countA++;
                if (str[i] == 'Q') countQ++;
                if (countA > 0 && countQ > 1 && str[i] == 'Q') count = countA*(countQ-1);
            }
            Console.Write(count);
            //Console.ReadKey();

        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string str = Console.ReadLine();
            int countQ = 0, countA = 0, count = 0;
            for (int i = 0; i < str.Length; i++)
            {

                if (str[i] == 'A' && countQ >0) countA++;
                if (str[i] == 'Q') countQ++;
                if (countA > 0 && countQ > 1 && str[i] == 'Q') count = countA*(countQ-1);
            }
            Console.Write(count);
            //Console.ReadKey();

        }
    }
}

using System;

namespace _894
{
    class Program
    {
        static void Main(string[] args)
        {
            string qaq = Console.ReadLine();
            int count = 0;

            for(int ii = 0; ii < qaq.Length; ++ii)
            {
                if(qaq[ii] == 'Q')
                {
                    for(int jj = ii; jj < qaq.Length - ii; ++jj)
                    {
                        if(qaq[jj] == 'A')
                        {
                            for(int kk = jj; kk < qaq.Length - jj; ++kk)
                            {
                                if(qaq[kk] == 'Q')
                                {
                                    ++count;
                                }
                            }
                        }
                    }
                }

            }
            Console.WriteLine(count);
        }
    }
}