Problem:
Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

If T_i = 1: reverse the string S.

If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

If F_i = 1 : Add C_i to the beginning of the string S.

If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

1 \leq Q \leq 2 \times 10^5

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy

Solution in C# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using AtCoder.Algorithms;
using AtCoder.Collections;
using AtCoder.Extensions;
using AtCoder.Mathematics;

namespace AtCoder.ABC158
{
    public class A : Solver
    {
        public override void Solve()
        {
            var S = Console.ReadLine();
            if (S.Distinct().Count() == 1)
            {
                Console.WriteLine("No");
            }
            else
            {
                Console.WriteLine("Yes");
            }
        }
    }
    public class B : Solver
    {
        public override void Solve()
        {
            var array = Scanner.ReadLongArray();
            var N = array[0];
            var A = array[1];
            var B = array[2];

            long count = N / (A + B);
            long blue = A * count;
            long red = B * count;
            long sum = A + B;

            while (sum < N)
            {
                if ((N - sum) > A)
                {
                    blue += A;
                    sum += A;
                }
                else
                {
                    var delta = (N - sum);
                    blue += delta;
                    sum += delta;
                    break;
                }

                if ((N - sum) > B)
                {
                    red += B;
                    sum += B;
                }
                else
                {
                    var delta = (N - sum);
                    red += delta;
                    sum += delta;
                    break;
                }
            }

            Console.WriteLine(blue);
        }
    }
    public class C : Solver
    {
        public override void Solve()
        {
            var array = Scanner.ReadIntArray();
            var A = array[0];
            var B = array[1];

            var otA = (int)Math.Ceiling(A / 0.08m);
            var otB = (int)Math.Ceiling(B / 0.10m);
            var otB2 = (int)Math.Ceiling((B + 1) / 0.10m);

            var bSet = new MultiSet<int>();
            for (int i = otB; i < otB2; i++)
            {
                bSet.Insert(i);
            }
            var bInd = bSet.LowerBound(otA);
            if (otA == bSet[bInd])
            {
                Console.WriteLine(otA);
            }
            else
            {
                Console.WriteLine(-1);
            }
        }
    }
    public class D : Solver
    {
        public override void Solve()
        {
            var S = Console.ReadLine();
            var Q = Scanner.ReadInt();

            var deq = new Deque<char>(S.Length);
            for (int i = 0; i < S.Length; i++)
            {
                deq.PushBack(S[i]);
            }

            for (int i = 0; i < Q; i++)
            {
                var q = Console.ReadLine();
                if (q.StartsWith("1"))
                {
                    var temp = new Deque<char>(deq.Count);
                    var str = new string(deq.Items);

                    foreach (var item in str.Reverse())
                    {
                        temp.PushBack(item);
                    }

                    deq = temp;
                }
                else
                {
                    var qArray = q.Split();
                    var F = qArray[1];
                    var C =qArray[2][0];

                    if (F == "1")
                    {
                        deq.PushFront(C);
                    }
                    else
                    {
                        deq.PushBack(C);
                    }
                }
            }

            Console.WriteLine(new string(deq.Items));
        }
    }
    public class E : Solver
    {
        public override void Solve()
        {
        }
    }
    public class F : Solver
    {
        public override void Solve()
        {
        }
    }
}

namespace AtCoder
{
    public abstract class Solver
    {
        public abstract void Solve();
    }

    class Program
    {
        static void Main(string[] args)
        {
            //var solver = new ABC158.A();
            //var solver = new ABC158.B();
            //var solver = new ABC158.C();
            var solver = new ABC158.D();
            //var solver = new ABC158.E();
            //var solver = new ABC158.F();
            solver.Solve();
        }
    }
}

namespace AtCoder.Algorithms
{
}

namespace AtCoder.Collections
{
    public class Deque<T>
    {
        T[] buf;
        int offset, count, capacity;
        public int Count { get { return count; } }
        public Deque(int cap) { buf = new T[capacity = cap]; }
        public Deque() { buf = new T[capacity = 16]; }

        // for debug
        public T[] Items
        {
            get
            {
                var a = new T[count];
                for (int i = 0; i < count; i++)
                {
                    a[i] = this[i];
                }
                return a;
            }
        }

        public void Init()
        {
            count = 0;
        }

        public T this[int index]
        {
            get { return buf[getIndex(index)]; }
            set { buf[getIndex(index)] = value; }
        }
        private int getIndex(int index)
        {
            if (index >= capacity)
                throw new IndexOutOfRangeException("out of range");
            var ret = index + offset;
            if (ret >= capacity)
                ret -= capacity;
            return ret;
        }
        public void PushFront(T item)
        {
            if (count == capacity) Extend();
            if (--offset < 0) offset += buf.Length;
            buf[offset] = item;
            ++count;
        }
        public T PopFront()
        {
            if (count == 0)
                throw new InvalidOperationException("collection is empty");
            --count;
            var ret = buf[offset++];
            if (offset >= capacity) offset -= capacity;
            return ret;
        }
        public void PushBack(T item)
        {
            if (count == capacity) Extend();
            var id = count++ + offset;
            if (id >= capacity) id -= capacity;
            buf[id] = item;
        }
        public T PopBack()
        {
            if (count == 0)
                throw new InvalidOperationException("collection is empty");
            return buf[getIndex(--count)];
        }
        public void Insert(int index, T item)
        {
            if (index > count) throw new IndexOutOfRangeException();
            this.PushFront(item);
            for (int i = 0; i < index; i++)
                this[i] = this[i + 1];
            this[index] = item;
        }
        public T RemoveAt(int index)
        {
            if (index < 0 || index >= count) throw new IndexOutOfRangeException();
            var ret = this[index];
            for (int i = index; i > 0; i--)
                this[i] = this[i - 1];
            this.PopFront();
            return ret;
        }
        private void Extend()
        {
            T[] newBuffer = new T[capacity << 1];
            if (offset > capacity - count)
            {
                var len = buf.Length - offset;
                Array.Copy(buf, offset, newBuffer, 0, len);
                Array.Copy(buf, 0, newBuffer, len, count - len);
            }
            else Array.Copy(buf, offset, newBuffer, 0, count);
            buf = newBuffer;
            offset = 0;
            capacity <<= 1;
        }
    }
    public class PriorityQueue<T> : IEnumerable<T>
    {
        private readonly List<T> _data = new List<T>();
        private readonly IComparer<T> _comparer;
        private readonly bool _isDescending;

        public PriorityQueue(IComparer<T> comparer, bool isDescending = true)
        {
            _comparer = comparer;
            _isDescending = isDescending;
        }

        public PriorityQueue(Comparison<T> comparison, bool isDescending = true)
            : this(Comparer<T>.Create(comparison), isDescending)
        {
        }

        public PriorityQueue(bool isDescending = true)
            : this(Comparer<T>.Default, isDescending)
        {
        }

        public void Enqueue(T item)
        {
            _data.Add(item);
            var childIndex = _data.Count - 1;
            while (childIndex > 0)
            {
                var parentIndex = (childIndex - 1) / 2;
                if (Compare(_data[childIndex], _data[parentIndex]) >= 0)
                    break;
                Swap(childIndex, parentIndex);
                childIndex = parentIndex;
            }
        }

        public T Dequeue()
        {
            var lastIndex = _data.Count - 1;
            var firstItem = _data[0];
            _data[0] = _data[lastIndex];
            _data.RemoveAt(lastIndex--);
            var parentIndex = 0;
            while (true)
            {
                var childIndex = parentIndex * 2 + 1;
                if (childIndex > lastIndex)
                    break;
                var rightChild = childIndex + 1;
                if (rightChild <= lastIndex && Compare(_data[rightChild], _data[childIndex]) < 0)
                    childIndex = rightChild;
                if (Compare(_data[parentIndex], _data[childIndex]) <= 0)
                    break;
                Swap(parentIndex, childIndex);
                parentIndex = childIndex;
            }
            return firstItem;
        }

        public T Peek()
        {
            return _data[0];
        }

        private void Swap(int a, int b)
        {
            var tmp = _data[a];
            _data[a] = _data[b];
            _data[b] = tmp;
        }

        private int Compare(T a, T b)
        {
            return _isDescending ? _comparer.Compare(b, a) : _comparer.Compare(a, b);
        }

        public int Count => _data.Count;

        public IEnumerator<T> GetEnumerator()
        {
            return _data.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
    }
    public class PriorityQueue<TKey, TValue> : IEnumerable<TValue>
    {
        private readonly List<KeyValuePair<TKey, TValue>> _data = new List<KeyValuePair<TKey, TValue>>();
        private readonly bool _isDescending;
        private readonly Func<TValue, TKey> _keySelector;
        private readonly IComparer<TKey> _keyComparer;

        public PriorityQueue(Func<TValue, TKey> keySelector, bool isDescending = true)
            : this(keySelector, Comparer<TKey>.Default, isDescending)
        {
        }

        public PriorityQueue(Func<TValue, TKey> keySelector, IComparer<TKey> keyComparer, bool isDescending = true)
        {
            _keySelector = keySelector;
            _keyComparer = keyComparer;
            _isDescending = isDescending;
        }

        public void Enqueue(TValue item)
        {
            _data.Add(new KeyValuePair<TKey, TValue>(_keySelector(item), item));
            var childIndex = _data.Count - 1;
            while (childIndex > 0)
            {
                var parentIndex = (childIndex - 1) / 2;
                if (Compare(_data[childIndex].Key, _data[parentIndex].Key) >= 0)
                    break;
                Swap(childIndex, parentIndex);
                childIndex = parentIndex;
            }
        }

        public TValue Dequeue()
        {
            var lastIndex = _data.Count - 1;
            var firstItem = _data[0];
            _data[0] = _data[lastIndex];
            _data.RemoveAt(lastIndex--);
            var parentIndex = 0;
            while (true)
            {
                var childIndex = parentIndex * 2 + 1;
                if (childIndex > lastIndex)
                    break;
                var rightChild = childIndex + 1;
                if (rightChild <= lastIndex && Compare(_data[rightChild].Key, _data[childIndex].Key) < 0)
                    childIndex = rightChild;
                if (Compare(_data[parentIndex].Key, _data[childIndex].Key) <= 0)
                    break;
                Swap(parentIndex, childIndex);
                parentIndex = childIndex;
            }
            return firstItem.Value;
        }

        public TValue Peek()
        {
            return _data[0].Value;
        }

        private void Swap(int a, int b)
        {
            var tmp = _data[a];
            _data[a] = _data[b];
            _data[b] = tmp;
        }

        private int Compare(TKey a, TKey b)
        {
            return _isDescending ? _keyComparer.Compare(b, a) : _keyComparer.Compare(a, b);
        }

        public int Count => _data.Count;

        public IEnumerator<TValue> GetEnumerator()
        {
            return _data.Select(r => r.Value).GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
    }
    /// <summary>
    /// Self-Balancing Binary Search Tree
    /// (using Randamized BST)
    /// </summary>
    public class SB_BinarySearchTree<T> where T : IComparable
    {
        public class Node
        {
            public T Value;
            public Node LChild;
            public Node RChild;
            public int Count;

            public Node(T v)
            {
                Value = v;
                Count = 1;
            }
        }
        static readonly Random _rnd = new Random();
        public static int Count(Node t)
        {
            return t == null ? 0 : t.Count;
        }
        static Node Update(Node t)
        {
            t.Count = Count(t.LChild) + Count(t.RChild) + 1;
            return t;
        }
        public static Node Merge(Node l, Node r)
        {
            if (l == null || r == null) return l == null ? r : l;

            if (Count(l) / (double)(Count(l) + Count(r)) > _rnd.NextDouble())
            {
                l.RChild = Merge(l.RChild, r);
                return Update(l);
            }
            else
            {
                r.LChild = Merge(l, r.LChild);
                return Update(r);
            }
        }
        /// <summary>
        /// split as [0, k), [k, n)
        /// </summary>
        public static Tuple<Node, Node> Split(Node t, int k)
        {
            if (t == null) return new Tuple<Node, Node>(null, null);
            if (k <= Count(t.LChild))
            {
                var s = Split(t.LChild, k);
                t.LChild = s.Item2;
                return new Tuple<Node, Node>(s.Item1, Update(t));
            }
            else
            {
                var s = Split(t.RChild, k - Count(t.LChild) - 1);
                t.RChild = s.Item1;
                return new Tuple<Node, Node>(Update(t), s.Item2);
            }
        }
        public static Node Remove(Node t, T v)
        {
            if (Find(t, v) == null) return t;
            return RemoveAt(t, LowerBound(t, v));
        }
        public static Node RemoveAt(Node t, int k)
        {
            var s = Split(t, k);
            var s2 = Split(s.Item2, 1);
            return Merge(s.Item1, s2.Item2);
        }
        public static bool Contains(Node t, T v)
        {
            return Find(t, v) != null;
        }
        public static Node Find(Node t, T v)
        {
            while (t != null)
            {
                var cmp = t.Value.CompareTo(v);
                if (cmp > 0) t = t.LChild;
                else if (cmp < 0) t = t.RChild;
                else break;
            }
            return t;
        }
        public static Node FindByIndex(Node t, int idx)
        {
            if (t == null) return null;

            var currentIdx = Count(t) - Count(t.RChild) - 1;
            while (t != null)
            {
                if (currentIdx == idx) return t;
                if (currentIdx > idx)
                {
                    t = t.LChild;
                    currentIdx -= (Count(t == null ? null : t.RChild) + 1);
                }
                else
                {
                    t = t.RChild;
                    currentIdx += (Count(t == null ? null : t.LChild) + 1);
                }
            }

            return null;
        }
        public static int UpperBound(Node t, T v)
        {
            var torg = t;
            if (t == null)
                return -1;

            var ret = int.MaxValue;
            var idx = Count(t) - Count(t.RChild) - 1;
            while (t != null)
            {
                var cmp = t.Value.CompareTo(v);

                if (cmp > 0)
                {
                    ret = Math.Min(ret, idx);
                    t = t.LChild;
                    idx -= (Count(t == null ? null : t.RChild) + 1);
                }
                else if (cmp <= 0)
                {
                    t = t.RChild;
                    idx += (Count(t == null ? null : t.LChild) + 1);
                }
            }
            return ret == int.MaxValue ? Count(torg) : ret;
        }
        public static int LowerBound(Node t, T v)
        {
            var torg = t;
            if (t == null) return -1;

            var idx = Count(t) - Count(t.RChild) - 1;
            var ret = int.MaxValue;
            while (t != null)
            {
                var cmp = t.Value.CompareTo(v);
                if (cmp >= 0)
                {
                    if (cmp == 0) ret = Math.Min(ret, idx);
                    t = t.LChild;
                    if (t == null) ret = Math.Min(ret, idx);
                    idx -= t == null ? 0 : (Count(t.RChild) + 1);
                }
                else if (cmp < 0)
                {
                    t = t.RChild;
                    idx += (Count(t == null ? null : t.LChild) + 1);
                    if (t == null) return idx;
                }
            }
            return ret == int.MaxValue ? Count(torg) : ret;
        }
        public static Node Insert(Node t, T v)
        {
            var ub = LowerBound(t, v);
            return InsertByIdx(t, ub, v);
        }
        static Node InsertByIdx(Node t, int k, T v)
        {
            var s = Split(t, k);
            return Merge(Merge(s.Item1, new Node(v)), s.Item2);
        }
        public static IEnumerable<T> Enumerate(Node t)
        {
            var ret = new List<T>();
            Enumerate(t, ret);
            return ret;
        }
        static void Enumerate(Node t, List<T> ret)
        {
            if (t == null) return;
            Enumerate(t.LChild, ret);
            ret.Add(t.Value);
            Enumerate(t.RChild, ret);
        }
    }
    public class Set<T> where T : IComparable
    {
        protected SB_BinarySearchTree<T>.Node _root;

        public T this[int idx] { get { return ElementAt(idx); } }

        public int Count()
        {
            return SB_BinarySearchTree<T>.Count(_root);
        }

        public virtual void Insert(T v)
        {
            if (_root == null) _root = new SB_BinarySearchTree<T>.Node(v);
            else
            {
                if (SB_BinarySearchTree<T>.Find(_root, v) != null) return;
                _root = SB_BinarySearchTree<T>.Insert(_root, v);
            }
        }

        public void Clear()
        {
            _root = null;
        }

        public void Remove(T v)
        {
            _root = SB_BinarySearchTree<T>.Remove(_root, v);
        }

        public bool Contains(T v)
        {
            return SB_BinarySearchTree<T>.Contains(_root, v);
        }

        public T ElementAt(int k)
        {
            var node = SB_BinarySearchTree<T>.FindByIndex(_root, k);
            if (node == null) throw new IndexOutOfRangeException();
            return node.Value;
        }

        public int Count(T v)
        {
            return SB_BinarySearchTree<T>.UpperBound(_root, v) - SB_BinarySearchTree<T>.LowerBound(_root, v);
        }

        public int LowerBound(T v)
        {
            return SB_BinarySearchTree<T>.LowerBound(_root, v);
        }

        public int UpperBound(T v)
        {
            return SB_BinarySearchTree<T>.UpperBound(_root, v);
        }

        public Tuple<int, int> EqualRange(T v)
        {
            if (!Contains(v)) return new Tuple<int, int>(-1, -1);
            return new Tuple<int, int>(SB_BinarySearchTree<T>.LowerBound(_root, v), SB_BinarySearchTree<T>.UpperBound(_root, v) - 1);
        }

        public List<T> ToList()
        {
            return new List<T>(SB_BinarySearchTree<T>.Enumerate(_root));
        }
    }
    public class MultiSet<T> : Set<T> where T : IComparable
    {
        public override void Insert(T v)
        {
            if (_root == null) _root = new SB_BinarySearchTree<T>.Node(v);
            else _root = SB_BinarySearchTree<T>.Insert(_root, v);
        }
    }
    public class UnionFind<T>
    {
        private Dictionary<T, T> _parent = new Dictionary<T, T>();
        private Dictionary<T, int> _size = new Dictionary<T, int>();

        public void Unite(T x, T y)
        {
            x = Root(x);
            y = Root(y);

            if (IsEqual(x, y)) return;

            var sx = Size(x);
            var sy = Size(y);
            if (sx < sy)
            {
                var t = x;
                x = y;
                y = t;
            }
            _size[x] += _size[y];
            _size.Remove(y);
            _parent[y] = x;
        }
        public int Size(T x)
        {
            var p = Root(x);

            int s;
            if (_size.TryGetValue(p, out s))
            {
                return s;
            }

            return _size[x] = 1;
        }
        public bool IsSameGroup(T x, T y)
        {
            return IsEqual(Root(x), Root(y));
        }
        public bool IsRoot(T x)
        {
            return IsEqual(Root(x), x);
        }
        private T Root(T x)
        {
            T r;
            if (_parent.TryGetValue(x, out r))
            {
                return _parent[x] = Root(r);
            }

            return x;
        }
        private bool IsEqual(T x, T y)
        {
            return EqualityComparer<T>.Default.Equals(x, y);
        }
    }
}

namespace AtCoder.Mathematics
{
    public class MathEx
    {
        /// <summary>
        /// 最小公倍数
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        static long Lcm(long a, long b)
        {
            return a / Gcd(a, b) * b;
        }
        /// <summary>
        /// 最大公約数
        /// </summary>
        /// <param name="m"></param>
        /// <param name="n"></param>
        /// <returns></returns>
        static long Gcd(long m, long n)
        {
            if (n == 0) return m;
            return Gcd(n, m % n);
        }
    }
    public struct ModInt
    {
        public const long Mod = (int)1e9 + 7;
        public long num;
        public ModInt(long n) { num = n; }
        public override string ToString() { return num.ToString(); }
        public static ModInt operator +(ModInt l, ModInt r) { l.num += r.num; if (l.num >= Mod) l.num -= Mod; return l; }
        public static ModInt operator -(ModInt l, ModInt r) { l.num -= r.num; if (l.num < 0) l.num += Mod; return l; }
        public static ModInt operator *(ModInt l, ModInt r) { return new ModInt(l.num * r.num % Mod); }
        public static ModInt operator /(ModInt l, ModInt r) { return l * Pow(r, Mod - 2); }
        public static implicit operator ModInt(long n) { n %= Mod; if (n < 0) n += Mod; return new ModInt(n); }
        public static ModInt Pow(ModInt v, long k) { return Pow(v.num, k); }
        public static ModInt Pow(long v, long k)
        {
            long ret = 1;
            for (k %= Mod - 1; k > 0; k >>= 1, v = v * v % Mod)
                if ((k & 1) == 1) ret = ret * v % Mod;
            return new ModInt(ret);
        }
        public static ModInt Combination(long n, long r)
        {
            ModInt ans = 1;
            for (int i = 0; i < r; i++)
            {
                ans *= n - i;
            }
            for (int i = 1; i <= r; i++)
            {
                ans /= i;
            }
            return ans;
        }
        public static ModInt Inverse(ModInt v) { return Pow(v, Mod - 2); }
    }
}

namespace AtCoder.Extensions
{
    public static class Extension
    {
        static int BinarySearch<T>(IList<T> source, T value, IComparer<T> comparer, bool isLowerBound)
        {
            return BinarySearch(source, 0, source.Count - 1, value, comparer, isLowerBound);
        }
        static int BinarySearch<T>(IList<T> source, int start, int end, T value, IComparer<T> comparer, bool isLowerBound)
        {
            while (start <= end)
            {
                var mid = (start + end) / 2;
                var result = comparer.Compare(source[mid], value);
                if (result < 0 || (result == 0 && !isLowerBound)) start = mid + 1;
                else end = mid - 1;
            }
            return start;
        }
        public static int LowerBound<T>(this IList<T> source, T value, Comparison<T> f) => BinarySearch(source, value, Comparer<T>.Create(f), true);
        public static int LowerBound<T>(this IList<T> source, int start, T value, Comparison<T> f) => BinarySearch(source, start, source.Count - 1, value, Comparer<T>.Create(f), true);
        public static int LowerBound<T>(this IList<T> source, int start, int end, T value, Comparison<T> f) => BinarySearch(source, start, end, value, Comparer<T>.Create(f), true);
        public static int LowerBound<T>(this IList<T> source, T value) => BinarySearch(source, value, Comparer<T>.Default, true);
        public static int LowerBound<T>(this IList<T> source, int start, T value) => BinarySearch(source, start, source.Count - 1, value, Comparer<T>.Default, true);
        public static int LowerBound<T>(this IList<T> source, int start, int end, T value) => BinarySearch(source, start, end, value, Comparer<T>.Default, true);
        public static int UpperBound<T>(this IList<T> source, T value, Comparison<T> cmp) => BinarySearch(source, value, Comparer<T>.Create(cmp), false);
        public static int UpperBound<T>(this IList<T> source, int start, T value, Comparison<T> cmp) => BinarySearch(source, start, source.Count - 1, value, Comparer<T>.Create(cmp), false);
        public static int UpperBound<T>(this IList<T> source, int start, int end, T value, Comparison<T> cmp) => BinarySearch(source, start, end, value, Comparer<T>.Create(cmp), false);
        public static int UpperBound<T>(this IList<T> source, T value) => BinarySearch(source, value, Comparer<T>.Default, false);
        public static int UpperBound<T>(this IList<T> source, int start, T value) => BinarySearch(source, start, source.Count - 1, value, Comparer<T>.Default, false);
        public static int UpperBound<T>(this IList<T> source, int start, int end, T value) => BinarySearch(source, start, end, value, Comparer<T>.Default, false);
        public static PriorityQueue<T> AsPriorityQueue<T>(this IEnumerable<T> source, bool isDescending = true)
        {
            var queue = new PriorityQueue<T>(isDescending);
            foreach (var item in source)
            {
                queue.Enqueue(item);
            }

            return queue;
        }
        public static PriorityQueue<TKey, TSource> AsPriorityQueue<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, bool isDescending = true)
        {
            var queue = new PriorityQueue<TKey, TSource>(keySelector, isDescending);
            foreach (var item in source)
            {
                queue.Enqueue(item);
            }

            return queue;
        }
        public static bool ChangeMin<T>(this IList<T> source, int index, T value)
            where T : IComparable
        {
            var a = source[index];
            if (a.CompareTo(value) > 0)
            {
                source[index] = value;
                return true;
            }
            return false;
        }
        public static bool ChangeMax<T>(this IList<T> source, int index, T value)
            where T : IComparable
        {
            var a = source[index];
            if (a.CompareTo(value) < 0)
            {
                source[index] = value;
                return true;
            }
            return false;
        }
    }
    public static class Scanner
    {
        public static string ReadString() => Console.ReadLine();
        public static int ReadInt() => int.Parse(Console.ReadLine());
        public static long ReadLong() => long.Parse(Console.ReadLine());
        public static double ReadDouble() => double.Parse(Console.ReadLine());
        public static decimal ReadDecimal() => decimal.Parse(Console.ReadLine());
        public static string[] ReadStringArray() => Console.ReadLine().Split().ToArray();
        public static string[] ReadStringArray(int n) => Enumerable.Range(0, n).Select(_ => Console.ReadLine()).ToArray();
        public static int[] ReadIntArray() => Console.ReadLine().Split().Select(int.Parse).ToArray();
        public static int[] ReadIntArray(int n) => Enumerable.Range(0, n).Select(_ => ReadInt()).ToArray();
        public static long[] ReadLongArray() => Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
        public static long[] ReadLongArray(int n) => Enumerable.Range(0, n).Select(_ => ReadLong()).ToArray();
        public static double[] ReadDoubleArray() => Console.ReadLine().Split().Select(double.Parse).ToArray();
        public static double[] ReadDoubleArray(int n) => Enumerable.Range(0, n).Select(_ => ReadDouble()).ToArray();
        public static decimal[] ReadDecimalArray() => Console.ReadLine().Split().Select(decimal.Parse).ToArray();
        public static decimal[] ReadDecimalArray(int n) => Enumerable.Range(0, n).Select(_ => ReadDecimal()).ToArray();
    }
}