#include <stdio.h>

  int train[150];
  int sets;
  int swapcnt;
  int count;


void swap(int *en, int *to) {
  int tmp;
  tmp = *en;
  *en = *to;
  *to = tmp;
}

void swappings() {
  int i, j;
  for (i = 0; i < count; ++i)
    for (j = i; j < count; ++j)
      if (train[i] > train[j]) {
    	swap(&train[i], &train[j]);
    	swapcnt++;
      }
}



int main(){
  int i,j,k,l,m;
  scanf("%d", &sets);
  for (i = 0; i < sets; ++i) {
    scanf("%d", &count);
    if (count > 0) {
      swapcnt = 0;
      for (j = 0; j < count; ++j) {
        scanf("%d", &train[j]);
      }
      swappings();
    printf("Optimal train swapping takes %i swaps.\n", swapcnt);
    } else
          printf("Optimal train swapping takes 0 swaps.\n", swapcnt);
  }
  return 0;
}
