description: You are given a string s of length n consisting of characters a and/or b.
  
  Let \operatorname{AB}(s) be the number of occurrences of string ab in s as a substring. Analogically, \operatorname{BA}(s) is the number of occurrences of ba in s as a substring.
  
  In one step, you can choose any index i and replace s_i with character a or b.
  
  What is the minimum number of steps you need to make to achieve \operatorname{AB}(s) = \operatorname{BA}(s)?
  
  Reminder:
  
  The number of occurrences of string d in s as substring is the number of indices i (1 ≤ i ≤ |s| - |d| + 1) such that substring s_i s_{i + 1} ... s_{i + |d| - 1} is equal to d. For example, \operatorname{AB}(aabbbabaa) = 2 since there are two indices i: i = 2 where aabbbabaa and i = 6 where aabbbabaa.
  
  Input
  
  Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 1000). Description of the test cases follows.
  
  The first and only line of each test case contains a single string s (1 ≤ |s| ≤ 100, where |s| is the length of the string s), consisting only of characters a and/or b.
  
  Output
  
  For each test case, print the resulting string s with \operatorname{AB}(s) = \operatorname{BA}(s) you'll get making the minimum number of steps.
  
  If there are multiple answers, print any of them.
  
  Example
  
  Input
  
  
  4
  b
  aabbbabaa
  abbb
  abbaab
  
  
  Output
  
  
  b
  aabbbabaa
  bbbb
  abbaaa
  
  Note
  
  In the first test case, both \operatorname{AB}(s) = 0 and \operatorname{BA}(s) = 0 (there are no occurrences of ab (ba) in b), so can leave s untouched.
  
  In the second test case, \operatorname{AB}(s) = 2 and \operatorname{BA}(s) = 2, so you can leave s untouched. 
  
  In the third test case, \operatorname{AB}(s) = 1 and \operatorname{BA}(s) = 0. For example, we can change s_1 to b and make both values zero.
  
  In the fourth test case, \operatorname{AB}(s) = 2 and \operatorname{BA}(s) = 1. For example, we can change s_6 to a and make both values equal to 1.
solution: import java.util.*;
  import java.lang.*;
  import java.io.*;
  
  public class Main
  {
      static long mod = (int)1e9+7;
      public static void main (String[] args) throws java.lang.Exception
      {
          FastReader sc =new FastReader();
  
          int t=sc.nextInt();
     
      //   int t=1;
     
          while(t-->0)
          {
              char a[]=sc.next().toCharArray();
              int ac=0,bc=0;
              for(int i=0;i<a.length-1;i++)
              {
                  if(a[i]=='a'&&a[i+1]=='b')
                  {
                      ac++;
                  }
                  else if(a[i]=='b'&&a[i+1]=='a')
                  {
                      bc++;
                  }
              }
              if(ac==bc)
              {
                  for(int i=0;i<a.length;i++)
                  {
                      System.out.print(a[i]);
                  }
                  System.out.println();
                  continue;
              }
              else if(ac>bc)
              {
                  int m=ac-bc;
                  for(int i=0;i<m;i++)
                  {
                      for(int j=0;j<a.length-1;j++)
                      {
                          if(a[j]=='a'&&a[j+1]=='b'||a[j]=='a'&&a[j+1]=='a')
                          {
                              a[j]='b';
                              break;
                          }
                      }
                  }
              }
              else
              {
                  int m=bc-ac;
                  for(int i=0;i<m;i++)
                  {
                      for(int j=0;j<a.length-1;j++)
                      {
                          if(a[j]=='b'&&a[j+1]=='a'||a[j]=='b'&&a[j+1]=='b')
                          {
                              a[j]='a';
                              break;
                          }
                      }
                  }
              }
              for(int i=0;i<a.length;i++)
              {
                  System.out.print(a[i]);
              }
              System.out.println();
          }
      }
  
  static int findfrequencies(int a[],int n)
  {
      int count=0;
      for(int i=0;i<a.length;i++)
      {
          if(a[i]==n)
          {
              count++;
          }
      }
      return count;
  }
  
  static class FastReader {
          BufferedReader br;
          StringTokenizer st;
   
          public FastReader()
          {
              br = new BufferedReader(
                  new InputStreamReader(System.in));
          }
   
          String next()
          {
              while (st == null || !st.hasMoreElements()) {
                  try {
                      st = new StringTokenizer(br.readLine());
                  }
                  catch (IOException e) {
                      e.printStackTrace();
                  }
              }
              return st.nextToken();
          }
   
          int nextInt() { return Integer.parseInt(next()); }
   
          long nextLong() { return Long.parseLong(next()); }
   
          double nextDouble()
          {
              return Double.parseDouble(next());
          }
         
          float nextFloat()
          {
              return Float.parseFloat(next());
          }
   
          String nextLine()
          {
              String str = "";
              try {
                  str = br.readLine();
              }
              catch (IOException e) {
                  e.printStackTrace();
              }
              return str;
          }
         
          int[] readArray(int n) {
  int[] a=new int[n];
  for (int i=0; i<n; i++) a[i]=nextInt();
  return a;
  }
  
  long[] readArrayLong(int n) {
  long[] a=new long[n];
  for (int i=0; i<n; i++) a[i]=nextLong();
  return a;
  }
  
      }
     
      public static int[] radixSort2(int[] a)
  {
  int n = a.length;
  int[] c0 = new int[0x101];
  int[] c1 = new int[0x101];
  int[] c2 = new int[0x101];
  int[] c3 = new int[0x101];
  for(int v : a) {
  c0[(v&0xff)+1]++;
  c1[(v>>>8&0xff)+1]++;
  c2[(v>>>16&0xff)+1]++;
  c3[(v>>>24^0x80)+1]++;
  }
  for(int i = 0;i < 0xff;i++) {
  c0[i+1] += c0[i];
  c1[i+1] += c1[i];
  c2[i+1] += c2[i];
  c3[i+1] += c3[i];
  }
  int[] t = new int[n];
  for(int v : a)t[c0[v&0xff]++] = v;
  for(int v : t)a[c1[v>>>8&0xff]++] = v;
  for(int v : a)t[c2[v>>>16&0xff]++] = v;
  for(int v : t)a[c3[v>>>24^0x80]++] = v;
  return a;
  }
  
  static int[] EvenOddArragement(int a[])
  {
      ArrayList<Integer> list=new ArrayList<>();
      for(int i=0;i<a.length;i++)
      {
          if(a[i]%2==0)
          {
              list.add(a[i]);
          }
      }
      for(int i=0;i<a.length;i++)
      {
          if(a[i]%2!=0)
          {
              list.add(a[i]);
          }
      }
      for(int i=0;i<a.length;i++)
      {
          a[i]=list.get(i);
      }
      return a;
  }
  static int gcd(int a, int b) {
      while (b != 0) {
          int t = a;
          a = b;
          b = t % b;
      }
      return a;
  }
     
      public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm)
      {
          // Create a list from elements of HashMap
          List<Map.Entry<Integer, Integer> > list =
                 new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet());
   
          // Sort the list
          Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() {
              public int compare(Map.Entry<Integer, Integer> o1,
                                 Map.Entry<Integer, Integer> o2)
              {
                  return (o1.getValue()).compareTo(o2.getValue());
              }
          });
           
          // put data from sorted list to hashmap
          HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>();
          for (Map.Entry<Integer, Integer> aa : list) {
              temp.put(aa.getKey(), aa.getValue());
          }
          return temp;
      }
      static int DigitSum(int n)
      {
          int r=0,sum=0;
          while(n>=0)
          {
              r=n%10;
              sum=sum+r;
              n=n/10;
          }
          return sum;
      }
  }