// import java.awt.event.MouseAdapter;
import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        int t=input.nextInt();
        while (t-->0){
            String s=input.next();
            int max=0;

            for (int i = 0; i <10 ; i++) {
                for (int j = 0; j <10 ; j++) {
                    max=Math.max(max,alternate(i,j,s));

                }
            }
            System.out.println(s.length()-max);

        }
    }

    private static int alternate(int i, int j, String s) {
        int prev=-1;
        int length=0;
        for (int k = 0; k <s.length() ; k++) {
            if (prev==-1 && s.charAt(k)-'0'==i ){
                prev=s.charAt(k)-'0';
                length++;
                continue;
            }
            if (prev==i && s.charAt(k)-'0'==j){
                prev=j;
                length++;
            } else   if (prev==j && s.charAt(k)-'0'==i){
                prev=i;
                length++;
            }

        }
        if (length%2!=0){
            length--;
        }
        return length;
    }
}


// package com.company.codeforces;

// import java.awt.event.MouseAdapter;
import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        int t=input.nextInt();
        while (t-->0){
            String s=input.next();
            int fre[]=new int[11];

            for (int i = 0; i <s.length() ; i++) {
                fre[Integer.parseInt(s.charAt(i)+"")]++;

            }
            int max=1;
            for (int i = 0; i <=10 ; i++) {
                max= Math.max(max,fre[i]);
            }

            for (int i = 1; i <10 ; i++) {
                for (int j = i+1; j <=10 ; j++) {
                    max=Math.max(max,alternate(i,j,s));
                    max=Math.max(max,alternate(j,i,s));

                }
            }
            System.out.println(s.length()-max);

        }
    }

    private static int alternate(int i, int j, String s) {
        int prev=-1;
        int length=0;
        for (int k = 0; k <s.length() ; k++) {
            if (prev==-1 && (Integer.parseInt(s.charAt(k)+"")==i )){
                prev=Integer.parseInt(s.charAt(k)+"");
                length++;
                continue;
            }
            if (prev==i && Integer.parseInt(s.charAt(k)+"")==j){
                prev=j;
                length++;
            } else   if (prev==j && Integer.parseInt(s.charAt(k)+"")==i){
                prev=i;
                length++;
            }

        }
        if (length%2!=0){
            length--;
        }
        return length;
    }
}


import java.io.*;
import java.util.*;

public class Solution {
    static int dp[][];

    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        int t=input.nextInt();
        while (t-->0){
            int n=input.nextInt();
            int m=input.nextInt();
            int k=input.nextInt();
            dp=new int[n][k+1];
            int a[]=new int[n];
            for (int i = 0; i <n ; i++) {
                a[i]=input.nextInt();
            }

            System.out.println(getresult(0,m,k,a));

        }

        }

    private static int getresult(int pos, int totmov, int lefmov, int[] a) {
        if (totmov<0){
            return 0;
        }
        if (dp[pos][lefmov]!=0){
            return dp[pos][lefmov];
        }
        int sum=a[pos];
        if (pos==0){
            sum+=getresult(pos+1, totmov-1, lefmov, a);

        }else if (pos==a.length-1){
            if (lefmov>0){
                sum+=getresult(pos-1, totmov-1, lefmov-1, a);
            }
        }else {
            int leftsum=0;
            int rightsum=0;
            if (lefmov>0){
                leftsum=getresult(pos-1, totmov-1, lefmov-1, a);
            }
            rightsum=getresult(pos+1, totmov-1, lefmov, a);
            sum+=Math.max(leftsum,rightsum);
        }
        return dp[pos][lefmov]=sum;
}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UncheckedIOException;
import java.lang.reflect.Array;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class Main {
    public void exec(Stdin stdin, Stdout stdout) {
        int t = stdin.nextInt();
        for (int i = 0; i < t; i++) {
            String s = stdin.nextString();
            stdout.println(exec(s));
        }
    }

    private int exec(String s) {
        Map<Character, Integer> d = new HashMap<>();
        for (char ch : s.toCharArray()) {
            d.put(ch, d.getOrDefault(ch, 0)+1);
        }
        int ans = d.values().stream().max(Comparator.naturalOrder()).get();

        for (char x = '0'; x <= '9'; x++) {
            for (char y = '0'; y <= '9'; y++) {
                if (x == y) {
                    continue;
                }

                char current = x;
                int c = 0;
                for (char ch : s.toCharArray()) {
                    if (current == ch) {
                        c++;
                        if (current == x) {
                            current = y;
                        } else {
                            current = x;
                        }
                    }
                }

                if (c % 2 == 0) {
                    ans = Math.max(ans, c);
                } else {
                    ans = Math.max(ans, c - 1);
                }
            }
        }
        return s.length() - ans;
    }

    private int count(String s, char x, char y) {
        int n = s.length();
        int[][] dp = new int[n+1][2];
        for (int i = 0; i < n; i++) {
            if (s.charAt(i) == x) {
                dp[i+1][1] = Math.max(dp[i+1][1], dp[i][0]+1);
            }
            if (s.charAt(i) == y) {
                dp[i+1][0] = Math.max(dp[i+1][0], dp[i][1]+1);
            }
            dp[i+1][0] = Math.max(dp[i+1][0], dp[i][0]);
            dp[i+1][1] = Math.max(dp[i+1][1], dp[i][1]);
        }
        return Math.max(dp[n][0], dp[n][1]);
    }

    public static void main(String[] args) {
        Stdin stdin = new Stdin();
        Stdout stdout = new Stdout();
        new Main().exec(stdin, stdout);
        stdout.flush();
    }

    public static class Stdin {
        private BufferedReader stdin;
        private Deque<String> tokens;
        private Pattern delim;

        public Stdin() {
            stdin = new BufferedReader(new InputStreamReader(System.in));
            tokens = new ArrayDeque<>();
            delim = Pattern.compile(" ");
        }

        public String nextString() {
            try {
                if (tokens.isEmpty()) {
                    String line = stdin.readLine();
                    delim.splitAsStream(line).forEach(tokens::addLast);
                }
                return tokens.pollFirst();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        public int nextInt() {
            return Integer.parseInt(nextString());
        }

        public double nextDouble() {
            return Double.parseDouble(nextString());
        }

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

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

    public static class Stdout {
        private PrintWriter stdout;

        public Stdout() {
            stdout =  new PrintWriter(System.out, false);
        }

        public void printf(String format, Object ... args) {
            stdout.printf(format, args);
        }

        public void println(Object ... objs) {
            String line = Arrays.stream(objs).map(this::deepToString).collect(Collectors.joining(" "));
            stdout.println(line);
        }

        private String deepToString(Object o) {
            if (o == null || !o.getClass().isArray()) {
                return Objects.toString(o);
            }

            int len = Array.getLength(o);
            String[] tokens = new String[len];
            for (int i = 0; i < len; i++) {
                tokens[i] = deepToString(Array.get(o, i));
            }
            return "{" + String.join(",", tokens) + "}";
        }

        public void flush() {
            stdout.flush();
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UncheckedIOException;
import java.lang.reflect.Array;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class Main {
    public void exec(Stdin stdin, Stdout stdout) {
        int t = stdin.nextInt();
        for (int i = 0; i < t; i++) {
            String s = stdin.nextString();
            stdout.println(exec(s));
        }
    }

    private int exec(String s) {
        Map<Character, Integer> d = new HashMap<>();
        for (char ch : s.toCharArray()) {
            d.put(ch, d.getOrDefault(ch, 0));
        }
        int ans = d.values().stream().max(Comparator.naturalOrder()).get();

        for (char x = '0'; x <= '9'; x++) {
            for (char y = '0'; y <= '9'; y++) {
                if (x == y) {
                    continue;
                }

                int c = count(s, x, y);
                if (c % 2 == 0) {
                    ans = Math.max(ans, c);
                } else {
                    ans = Math.max(ans, c - 1);
                }
            }
        }
        return s.length() - ans;
    }

    private int count(String s, char x, char y) {
        int n = s.length();
        int[][] dp = new int[n+1][2];
        for (int i = 0; i < n; i++) {
            if (s.charAt(i) == x) {
                dp[i+1][1] = Math.max(dp[i+1][1], dp[i][0]+1);
            }
            if (s.charAt(i) == y) {
                dp[i+1][0] = Math.max(dp[i+1][0], dp[i][1]+1);
            }
            dp[i+1][0] = Math.max(dp[i+1][0], dp[i][0]);
            dp[i+1][1] = Math.max(dp[i+1][1], dp[i][1]);
        }
        return Math.max(dp[n][0], dp[n][1]);
    }

    public static void main(String[] args) {
        Stdin stdin = new Stdin();
        Stdout stdout = new Stdout();
        new Main().exec(stdin, stdout);
        stdout.flush();
    }

    public static class Stdin {
        private BufferedReader stdin;
        private Deque<String> tokens;
        private Pattern delim;

        public Stdin() {
            stdin = new BufferedReader(new InputStreamReader(System.in));
            tokens = new ArrayDeque<>();
            delim = Pattern.compile(" ");
        }

        public String nextString() {
            try {
                if (tokens.isEmpty()) {
                    String line = stdin.readLine();
                    delim.splitAsStream(line).forEach(tokens::addLast);
                }
                return tokens.pollFirst();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        public int nextInt() {
            return Integer.parseInt(nextString());
        }

        public double nextDouble() {
            return Double.parseDouble(nextString());
        }

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

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

    public static class Stdout {
        private PrintWriter stdout;

        public Stdout() {
            stdout =  new PrintWriter(System.out, false);
        }

        public void printf(String format, Object ... args) {
            stdout.printf(format, args);
        }

        public void println(Object ... objs) {
            String line = Arrays.stream(objs).map(this::deepToString).collect(Collectors.joining(" "));
            stdout.println(line);
        }

        private String deepToString(Object o) {
            if (o == null || !o.getClass().isArray()) {
                return Objects.toString(o);
            }

            int len = Array.getLength(o);
            String[] tokens = new String[len];
            for (int i = 0; i < len; i++) {
                tokens[i] = deepToString(Array.get(o, i));
            }
            return "{" + String.join(",", tokens) + "}";
        }

        public void flush() {
            stdout.flush();
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UncheckedIOException;
import java.lang.reflect.Array;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class Main {
    public void exec(Stdin stdin, Stdout stdout) {
        int t = stdin.nextInt();
        for (int i = 0; i < t; i++) {
            String s = stdin.nextString();
            stdout.println(exec(s));
        }
    }

    private int exec(String s) {
        Map<Character, Integer> d = new HashMap<>();
        for (char ch : s.toCharArray()) {
            d.put(ch, d.getOrDefault(ch, 0));
        }
        int ans = d.values().stream().max(Comparator.naturalOrder()).get();

        for (char x = '0'; x <= '9'; x++) {
            for (char y = '0'; y <= '9'; y++) {
                if (x == y) {
                    continue;
                }

                char current = x;
                int c = 0;
                for (char ch : s.toCharArray()) {
                    if (current == ch) {
                        c++;
                        if (current == x) {
                            current = y;
                        } else {
                            current = x;
                        }
                    }
                }

                if (c % 2 == 0) {
                    ans = Math.max(ans, c);
                } else {
                    ans = Math.max(ans, c - 1);
                }
            }
        }
        return s.length() - ans;
    }

    public static void main(String[] args) {
        Stdin stdin = new Stdin();
        Stdout stdout = new Stdout();
        new Main().exec(stdin, stdout);
        stdout.flush();
    }

    public static class Stdin {
        private BufferedReader stdin;
        private Deque<String> tokens;
        private Pattern delim;

        public Stdin() {
            stdin = new BufferedReader(new InputStreamReader(System.in));
            tokens = new ArrayDeque<>();
            delim = Pattern.compile(" ");
        }

        public String nextString() {
            try {
                if (tokens.isEmpty()) {
                    String line = stdin.readLine();
                    delim.splitAsStream(line).forEach(tokens::addLast);
                }
                return tokens.pollFirst();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        public int nextInt() {
            return Integer.parseInt(nextString());
        }

        public double nextDouble() {
            return Double.parseDouble(nextString());
        }

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

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

    public static class Stdout {
        private PrintWriter stdout;

        public Stdout() {
            stdout =  new PrintWriter(System.out, false);
        }

        public void printf(String format, Object ... args) {
            stdout.printf(format, args);
        }

        public void println(Object ... objs) {
            String line = Arrays.stream(objs).map(this::deepToString).collect(Collectors.joining(" "));
            stdout.println(line);
        }

        private String deepToString(Object o) {
            if (o == null || !o.getClass().isArray()) {
                return Objects.toString(o);
            }

            int len = Array.getLength(o);
            String[] tokens = new String[len];
            for (int i = 0; i < len; i++) {
                tokens[i] = deepToString(Array.get(o, i));
            }
            return "{" + String.join(",", tokens) + "}";
        }

        public void flush() {
            stdout.flush();
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define fi first
#define se second
const int N=3e5+5,M=1.5e7+5;
int cc=0;
const int mod = 1e9+7;

/*
  author: mohit kumar meena

  TALENT IS OVERRATED.

  flight till your last breath
  don't be rude to yourself

  try to workout with pen and paper you will definitely get something.

*/

/*
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

ll modexp(ll x, ll n)
{
    if (n == 0) {
        return 1;
    }
    else if (n % 2 == 0) {
        return modexp((x * x) % m, n / 2);
    }
    else {
        return (x * modexp((x * x) % m, (n - 1) / 2) % m);
    }
}
ll getFractionModulo(ll a, ll b)
{
    ll c = gcd(a, b);
    a = a / c;
    b = b / c;
    ll d = modexp(b, m - 2);
    ll ans = ((a % m) * (d % m)) % m;

    return ans;
}
*/

bool isPowerOfTwo (ll x)
{
    return x && (!(x&(x-1)));
}

void  solve(){

    string s;
    cin>>s;
    int n=s.length();
   int ans=0;

    for(int i=0;i<=9;i++){
      for(int j=0;j<=9;j++){
          int cnt=0;
          for(int k=0;k<n;k++){
            if(k%2){
              if(s[k]==i+'0') cnt++;
            }
            else{
              if(s[k]==j+'0') cnt++;
            }
          }

         ans=max(ans,cnt-(i==j?0:cnt%2));

      }
    }
  cout<<n-ans<<endl;

  return;

}

int main(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
        ll t;
        cin>>t;
       // t=1;
        while(t--){
            solve();
        }
       return 0;
   }


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

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        string s;
        cin >> s;
        int longest = 0;
        for (int d1 = 0; d1 < 10; d1++) {
            for (int d2 = 0; d2 < 10; d2++) {
                bool first = true;
                int len = 0;
                for (int i = 0; i < s.size(); i++) {
                    if (first && s[i] - '0' == d1) {
                        len++;
                        first = false;
                    } else if (!first && s[i] - '0' == d2) {
                        len++;
                        first = true;
                    }
                }
                longest = max(longest, len - (d1 == d2 ? 0 : len % 2));
            }
        }
        cout << s.size() - longest << '\n';
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define fi first
#define se second
const int N=3e5+5,M=1.5e7+5;
int cc=0;
const int mod = 1e9+7;

/*
  author: mohit kumar meena

  TALENT IS OVERRATED.

  flight till your last breath
  don't be rude to yourself

  try to workout with pen and paper you will definitely get something.

*/

/*
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

ll modexp(ll x, ll n)
{
    if (n == 0) {
        return 1;
    }
    else if (n % 2 == 0) {
        return modexp((x * x) % m, n / 2);
    }
    else {
        return (x * modexp((x * x) % m, (n - 1) / 2) % m);
    }
}
ll getFractionModulo(ll a, ll b)
{
    ll c = gcd(a, b);
    a = a / c;
    b = b / c;
    ll d = modexp(b, m - 2);
    ll ans = ((a % m) * (d % m)) % m;

    return ans;
}
*/

bool isPowerOfTwo (ll x)
{
    return x && (!(x&(x-1)));
}

void  solve(){

    string s;
    cin>>s;
    int n=s.length();
   int ans=INT_MAX;

    for(int i=0;i<=9;i++){
      for(int j=0;j<=9;j++){
          int cnt=0;
          for(int k=0;k<n;k++){
            if(k%2){
              if(s[k]!=i+'0') cnt++;
            }
            else{
              if(s[k]!=j+'0') cnt++;
            }
          }
          ans=min(ans,cnt);

      }
    }
  cout<<ans<<endl;

  return;

}

int main(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
        ll t;
        cin>>t;
       // t=1;
        while(t--){
            solve();
        }
       return 0;
   }


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

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        string s;
        cin >> s;
        int longest = 0;
        for (int d1 = 0; d1 < 10; d1++) {
            for (int d2 = 0; d2 < 10; d2++) {
                // bool first = true;
                int len = 0;
                for (int i = 0; i < s.size(); i++) {
                    if (i%2==0 && s[i] - '0' == d1) {
                        len++;
                        // first = false;
                    } else if (i%2==1 && s[i] - '0' == d2) {
                        len++;
                        // first = true;
                    }
                }
                longest = max(longest, len - (d1 == d2 ? 0 : len % 2));
            }
        }
        cout << s.size() - longest << '\n';
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define fi first
#define se second
const int N=3e5+5,M=1.5e7+5;
int cc=0;
const int mod = 1e9+7;

/*
  author: mohit kumar meena

  TALENT IS OVERRATED.

  flight till your last breath
  don't be rude to yourself

  try to workout with pen and paper you will definitely get something.

*/

/*
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

ll modexp(ll x, ll n)
{
    if (n == 0) {
        return 1;
    }
    else if (n % 2 == 0) {
        return modexp((x * x) % m, n / 2);
    }
    else {
        return (x * modexp((x * x) % m, (n - 1) / 2) % m);
    }
}
ll getFractionModulo(ll a, ll b)
{
    ll c = gcd(a, b);
    a = a / c;
    b = b / c;
    ll d = modexp(b, m - 2);
    ll ans = ((a % m) * (d % m)) % m;

    return ans;
}
*/

bool isPowerOfTwo (ll x)
{
    return x && (!(x&(x-1)));
}

void  solve(){

    string s;
    cin>>s;
    int n=s.length();
   int ans=INT_MAX;

    for(int i=0;i<=9;i++){
      for(int j=0;j<=9;j++){
          int cnt=0;
          for(int k=0;k<n;k++){
            if(k%2){
              if(s[k]!=i+'0') cnt++;
            }
            else{
              if(s[k]!=j+'0') cnt++;
            }
          }
          ans=min(ans,cnt);

          cnt=0;

          for(int k=0;k<n;k++){
            if(k%2){
              if(s[k]!=j+'0') cnt++;
            }
            else{
              if(s[k]!=i+'0') cnt++;
            }
          }
          ans=min(ans,cnt);

      }
    }
  cout<<ans<<endl;

  return;

}

int main(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
        ll t;
        cin>>t;
       // t=1;
        while(t--){
            solve();
        }
       return 0;
   }


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define fi first
#define se second
const int N=3e5+5,M=1.5e7+5;
int cc=0;
const int mod = 1e9+7;

/*
  author: mohit kumar meena

  TALENT IS OVERRATED.

  flight till your last breath
  don't be rude to yourself

  try to workout with pen and paper you will definitely get something.

*/

/*
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

ll modexp(ll x, ll n)
{
    if (n == 0) {
        return 1;
    }
    else if (n % 2 == 0) {
        return modexp((x * x) % m, n / 2);
    }
    else {
        return (x * modexp((x * x) % m, (n - 1) / 2) % m);
    }
}
ll getFractionModulo(ll a, ll b)
{
    ll c = gcd(a, b);
    a = a / c;
    b = b / c;
    ll d = modexp(b, m - 2);
    ll ans = ((a % m) * (d % m)) % m;

    return ans;
}
*/

bool isPowerOfTwo (ll x)
{
    return x && (!(x&(x-1)));
}

void  solve(){

    string s;
    cin>>s;
    int n=s.length();
   int ans=INT_MAX;

   vector<int> freq(10,0);

   for(int i=0;i<n;i++){
    freq[s[i]-'0']++;
   }

   ans=n-(*max_element(freq.begin(),freq.end()));

    for(int i=0;i<=9;i++){
      for(int j=0;j<=9;j++){
          int cnt=0;
          for(int k=0;k<n;k++){
            if(k%2){
              if(s[k]!=s[1]) cnt++;
            }
            else{
              if(s[k]!=s[0]) cnt++;
            }
          }
          ans=min(ans,cnt);

      }
    }

  cout<<ans<<endl;

  return;

}

int main(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
        ll t;
        cin>>t;
       // t=1;
        while(t--){
            solve();
        }
       return 0;
   }


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define fi first
#define se second
const int N=3e5+5,M=1.5e7+5;
int cc=0;
const int mod = 1e9+7;

/*
  author: mohit kumar meena

  TALENT IS OVERRATED.

  flight till your last breath
  don't be rude to yourself

  try to workout with pen and paper you will definitely get something.

*/

/*
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

ll modexp(ll x, ll n)
{
    if (n == 0) {
        return 1;
    }
    else if (n % 2 == 0) {
        return modexp((x * x) % m, n / 2);
    }
    else {
        return (x * modexp((x * x) % m, (n - 1) / 2) % m);
    }
}
ll getFractionModulo(ll a, ll b)
{
    ll c = gcd(a, b);
    a = a / c;
    b = b / c;
    ll d = modexp(b, m - 2);
    ll ans = ((a % m) * (d % m)) % m;

    return ans;
}
*/

bool isPowerOfTwo (ll x)
{
    return x && (!(x&(x-1)));
}

void  solve(){

    string s;
    cin>>s;
    int n=s.length();
   int ans=INT_MAX;

    for(int i=0;i<=9;i++){
      for(int j=0;j<=9;j++){
          int cnt=0;

          if(s[0]!=i+'0') cnt++;
          if(s[1]!=j+'0') cnt++;
          string tmp=s;
          tmp[0]=i+'0';
          tmp[1]=j+'0';

          for(int k=2;k<n;k++){
            if(k%2){
              if(tmp[k]!=tmp[1]) cnt++;
            }
            else{
              if(tmp[k]!=tmp[0]) cnt++;
            }
          }
          ans=min(ans,cnt);

      }
    }
  cout<<ans<<endl;

  return;

}

int main(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
        ll t;
        cin>>t;
       // t=1;
        while(t--){
            solve();
        }
       return 0;
   }


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

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        string s;
        cin >> s;
        int longest = 0;
        for (int d1 = 0; d1 < 10; d1++) {
            for (int d2 = 0; d2 < 10; d2++) {
                // bool first = true;
                int len = 0;
                for (int i = 0; i < s.size(); i++) {
                    if (i%2 && s[i] - '0' == d1) {
                        len++;
                        // first = false;
                    } else if (i%2==0 && s[i] - '0' == d2) {
                        len++;
                        // first = true;
                    }
                }
                longest = max(longest, len - (d1 == d2 ? 0 : len % 2));
            }
        }
        cout << s.size() - longest << '\n';
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define fi first
#define se second
const int N=3e5+5,M=1.5e7+5;
int cc=0;
const int mod = 1e9+7;

/*
  author: mohit kumar meena

  TALENT IS OVERRATED.

  flight till your last breath
  don't be rude to yourself

  try to workout with pen and paper you will definitely get something.

*/

/*
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

ll modexp(ll x, ll n)
{
    if (n == 0) {
        return 1;
    }
    else if (n % 2 == 0) {
        return modexp((x * x) % m, n / 2);
    }
    else {
        return (x * modexp((x * x) % m, (n - 1) / 2) % m);
    }
}
ll getFractionModulo(ll a, ll b)
{
    ll c = gcd(a, b);
    a = a / c;
    b = b / c;
    ll d = modexp(b, m - 2);
    ll ans = ((a % m) * (d % m)) % m;

    return ans;
}
*/

bool isPowerOfTwo (ll x)
{
    return x && (!(x&(x-1)));
}

void  solve(){

    string s;
    cin>>s;
    int n=s.length();
   int ans=INT_MAX;

   vector<int> freq(10,0);

   for(int i=0;i<n;i++){
    freq[s[i]-'0']++;
   }

   ans=n-(*max_element(freq.begin(),freq.end()));

    for(int i=0;i<=9;i++){
      for(int j=0;j<=9;j++){
          int cnt=0;
          for(int k=0;k<n;k++){
            if(k%2){
              if(s[k]!=i+'0') cnt++;
            }
            else{
              if(s[k]!=j+'0') cnt++;
            }
          }
          ans=min(ans,cnt);

      }
    }

  cout<<ans<<endl;

  return;

}

int main(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
        ll t;
        cin>>t;
       // t=1;
        while(t--){
            solve();
        }
       return 0;
   }


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;

int main()
{
	ll n, t, ans;
	string s;
	cin >> t;
	while (t--) {
		ans = 1e10;
		cin >> s;
		n = s.size();
		string s1 = "";
		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < 10; j++) {
				s1 = "";
				for (int qq = 0; qq < n; qq++) {
					if ((int)(s[qq] - '0') == i || (int)(s[qq] - '0') == j) {
						s1 += s[qq];
					}
				}
				ll f = 0;
				ll cos = 0;
				for (int qqq = 2; qqq < s1.size(); qqq++) {
					if (!f && s1[qqq] - '0' == i || f && s1[qqq] - '0' == j) {
						f++;
						cos++;
					}
					f = (f + 1) % 2;
				}
				if ((s1.size() - cos) % 2 == 1 && i != j) {
					cos++;
				}
				if (ans > n - s1.size() + cos) {
					ans = n - s1.size() + cos;
				}
			}
		}
		cout << ans << "\n";
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;

int main()
{
	ll n, t, ans;
	string s;
	cin >> t;
	while (t--) {
		ans = 1e10;
		cin >> s;
		n = s.size();
		string s1 = "";
		for (int i = 0; i < 10; i++) {
			for (int j = i; j < 10; j++) {
				s1 = "";
				for (int qq = 0; qq < n; qq++) {
					if ((int)(s[qq] - '0') == i || (int)(s[qq] - '0') == j) {
						s1 += s[qq];
					}
				}
				ll f = 0, cos = 0;
				for (int i = 0; i < s1.size(); i++) {
					if (s1[i] != s1[(i + f) % 2]) {
						f = (f + 1) % 2;
						cos++;
					}
				}
				if ((s1.size() - cos) % 2 == 1 && i != j) {
					cos++;
				}
				if (ans > n - s1.size() + cos) {
					ans = n - s1.size() + cos;
				}
			}
		}
		cout << ans << "\n";
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;

int main()
{
	ll n, t, ans;
	string s;
	cin >> t;
	while (t--) {
		ans = 1e17;
		cin >> s;
		n = s.length();
		string s1 = "";
		for (int i = 0; i < 10; i++) {
			for (int j = i; j < 10; j++) {
				s1 = "";
				for (int qq = 0; qq < n; qq++) {
					if ((int)(s[qq] - '0') == i || (int)(s[qq] - '0') == j) {
						s1 += s[qq];
					}
				}
				ll f = 0, cos = 0;
				for (int i = 2; i < s1.length(); i++) {
					if (s1[i] != s1[(i + f) % 2]) {
						f++;
						cos++;
					}
				}
				if ((s1.length() - cos) % 2 == 1 && (s1.length() - cos) > 1 && i != j) {
					cos++;
				}
				if (ans > n - s1.length() + cos) {
					ans = n - s1.length() + cos;
				}
			}
		}
		cout << ans << "\n";
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;

int main()
{
	ll n, t, ans;
	string s;
	cin >> t;
	while (t--) {
		ans = 1e17;
		cin >> s;
		n = s.size();
		string s1 = "";
		for (int i = 0; i < 10; i++) {
			for (int j = i; j < 10; j++) {
				s1 = "";
				for (int qq = 0; qq < n; qq++) {
					if ((int)(s[qq] - '0') == i || (int)(s[qq] - '0') == j) {
						s1 += s[qq];
					}
				}
				ll f = 0, cos = 0;
				for (int i = 2; i < s1.size(); i++) {
					if (s1[i] != s1[(i + f) % 2]) {
						f++;
						cos++;
					}
				}
				if ((s1.size() - cos) % 2 == 1 && i != j) {
					cos++;
				}
				if (ans > n - s1.size() + cos) {
					ans = n - s1.size() + cos;
				}
			}
		}
		cout << ans << "\n";
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;

int main()
{
	ll n, t, ans;
	string s;
	cin >> t;
	while (t--) {
		ans = 1e10;
		cin >> s;
		n = s.size();
		string s1 = "";
		for (int i = 0; i < 10; i++) {
			for (int j = i; j < 10; j++) {
				s1 = "";
				for (int qq = 0; qq < n; qq++) {
					if ((int)(s[qq] - '0') == i || (int)(s[qq] - '0') == j) {
						s1 += s[qq];
					}
				}
				ll f = 0, cos = 0;
				for (int i = 2; i < s1.size(); i++) {
					if (s1[i] != s1[(i + f) % 2]) {
						f = (f+1) % 2;
						cos++;
					}
				}
				if ((s1.size() - cos) % 2 == 1 && i != j) {
					cos++;
				}
				if (ans > n - s1.size() + cos) {
					ans = n - s1.size() + cos;
				}
			}
		}
		cout << ans << "\n";
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;

int main()
{
	ll n, t, ans;
	string s;
	cin >> t;
	while (t--) {
		ans = 1e17;
		cin >> s;
		n = s.length();
		string s1 = "";
		for (int i = 0; i < 10; i++) {
			for (int j = i; j < 10; j++) {
				s1 = "";
				for (int qq = 0; qq < n; qq++) {
					if (s[qq] - '0' == i || s[qq] - '0' == j) {
						s1 += s[qq];
					}
				}
				ll f = 0, cos = 0;
				for (int i = 2; i < s1.length(); i++) {
					if (s1[i] != s1[(i + f) % 2]) {
						f++;
						cos++;
					}
				}
				if ((s1.length() - cos) % 2 == 1 && (s1.length() - cos) > 3 && i != j) {
					cos++;
				}
				if (ans > n - s1.length() + cos) {
					ans = n - s1.length() + cos;
				}
			}
		}
		cout << ans << "\n";
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;

int main()
{
	ll n, t, ans;
	string s;
	cin >> t;
	while (t--) {
		ans = 1e10;
		cin >> s;
		n = s.size();
		string s1 = "";
		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < 10; j++) {
				s1 = "";
				for (int qq = 0; qq < n; qq++) {
					if ((int)(s[qq] - '0') == i || (int)(s[qq] - '0') == j) {
						s1 += s[qq];
					}
				}
				ll f = 0;
				ll cos = 0;
				for (int qqq = 0; qqq < s1.size(); qqq++) {
					if (!f && s1[qqq] - '0' == i || f && s1[qqq] - '0' == j) {
						cos++;
						f = (f + 1) % 2;
					}
				}
				if (cos % 2 == 1 && i != j) {
					cos--;
				}
				if (ans > n - cos) {
					ans = n - cos;
				}
			}
		}
		cout << ans << "\n";
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;

int main()
{
	ll n, t, ans;
	string s;
	cin >> t;
	while (t--) {
		ans = 1e10;
		cin >> s;
		n = s.size();
		string s1 = "";
		for (int i = 0; i < 10; i++) {
			for (int j = i; j < 10; j++) {
				s1 = "";
				for (int qq = 0; qq < n; qq++) {
					if ((int)(s[qq] - '0') == i || (int)(s[qq] - '0') == j) {
						s1 += s[qq];
					}
				}
				ll f = 0, cos = 0;
				for (int qqq = 2; qqq < s1.size(); qqq++) {
					if (s1[qqq] != s1[(qqq + f) % 2]) {
						f++;
						cos++;
					}
				}
				if ((s1.size() - cos) % 2 == 1 && i != j) {
					cos++;
				}
				if (ans > n - s1.size() + cos) {
					ans = n - s1.size() + cos;
				}
			}
		}
		cout << ans << "\n";
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;

int main()
{
	ll n, t, ans;
	string s;
	cin >> t;
	while (t--) {
		ans = 1e17;
		cin >> s;
		n = s.length();
		string s1 = "";
		for (int i = 0; i < 10; i++) {
			for (int j = i; j < 10; j++) {
				s1 = "";
				for (int qq = 0; qq < n; qq++) {
					if (s[qq] - '0' == i || s[qq] - '0' == j) {
						s1 += s[qq];
					}
				}
				ll f = 0, cos = 0;
				for (int i = 2; i < s1.length(); i++) {
					if (s1[i] != s1[(i + f) % 2]) {
						f++;
						cos++;
					}
				}
				if ((s1.length() - cos) % 2 == 1 && s1.length() != 1) {
					cos++;
				}
				if (ans > n - s1.length() + cos) {
					ans = n - s1.length() + cos;
				}
			}
		}
		cout << ans << "\n";
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;

int main()
{
	ll n, t, ans;
	string s;
	cin >> t;
	while (t--) {
		ans = 1e17;
		cin >> s;
		n = s.length();
		string s1 = "";
		for (int i = 0; i < 10; i++) {
			for (int j = i; j < 10; j++) {
				s1 = "";
				for (int qq = 0; qq < n; qq++) {
					if (s[qq] - '0' == i || s[qq] - '0' == j) {
						s1 += s[qq];
					}
				}
				ll f = 0, cos = 0;
				for (int i = 2; i < s1.length(); i++) {
					if (s1[i] != s1[(i + f) % 2]) {
						f++;
						cos++;
					}
				}
				if ((s1.length() - cos) % 2 == 1 && s1.length() != 1 && i != j) {
					cos++;
				}
				if (ans > n - s1.length() + cos) {
					ans = n - s1.length() + cos;
				}
			}
		}
		cout << ans << "\n";
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;

int main()
{
	ll n, t, ans;
	string s;
	cin >> t;
	while (t--) {
		ans = 1e17;
		cin >> s;
		n = s.length();
		string s1 = "";
		for (int i = 0; i < 10; i++) {
			for (int j = i; j < 10; j++) {
				s1 = "";
				for (int qq = 0; qq < n; qq++) {
					if ((int)(s[qq] - '0') == i || (int)(s[qq] - '0') == j) {
						s1 += s[qq];
					}
				}
				ll f = 0, cos = 0;
				for (int i = 2; i < s1.length(); i++) {
					if (s1[i] != s1[(i + f) % 2]) {
						f++;
						cos++;
					}
				}
				if ((s1.length() - cos) % 2 == 1 && (s1.length() - cos) > 3 && i != j) {
					cos++;
				}
				if (ans > n - s1.length() + cos) {
					ans = n - s1.length() + cos;
				}
			}
		}
		cout << ans << "\n";
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;

int main()
{
	ll n, t, ans;
	string s;
	cin >> t;
	while (t--) {
		ans = 1e17;
		cin >> s;
		n = s.length();
		string s1 = "";
		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < 10; j++) {
				s1 = "";
				for (int qq = 0; qq < n; qq++) {
					if (s[qq] - '0' == i || s[qq] - '0' == j) {
						s1 += s[qq];
					}
				}
				ll f = 0, cos = 0;
				for (int i = 2; i < s1.length(); i++) {
					if (s1[i] != s1[(i + f) % 2]) {
						f++;
						cos++;
					}
				}
				if ((s1.length() - cos) % 2 == 1 && (s1.length() - cos) > 3 && i != j) {
					cos++;
				}
				if (ans > n - s1.length() + cos) {
					ans = n - s1.length() + cos;
				}
			}
		}
		cout << ans << "\n";
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;

int main()
{
	ll n, t, ans;
	string s;
	cin >> t;
	while (t--) {
		ans = 1e10;
		cin >> s;
		n = s.size();
		string s1 = "";
		for (int i = 0; i < 10; i++) {
			for (int j = i; j < 10; j++) {
				s1 = "";
				for (int qq = 0; qq < n; qq++) {
					if ((int)(s[qq] - '0') == i || (int)(s[qq] - '0') == j) {
						s1 += s[qq];
					}
				}
				ll f = 0, cos = 0;
				for (int qqq = 2; qqq < s1.size(); qqq++) {
					if (s1[qqq] != s1[(qqq + f) % 2]) {
						f = (f + 1) % 2;
						cos++;
					}
				}
				if ((s1.size() - cos) % 2 == 1 && i != j) {
					cos++;
				}
				if (ans > n - s1.size() + cos) {
					ans = n - s1.size() + cos;
				}
			}
		}
		cout << ans << "\n";
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;

int main()
{
	ll a, b, c, n, m, t, ans;
	string s;
	vector <pair<ll,ll>> v;
	cin >> t;
	while (t--) {
		ans = 1e17;
		cin >> s;
		n = s.length();
		/*v.clear();
		v.resize(10);
		for (int i = 0; i < 10; i++) {
			v[i] = { 0,i };
		}
		for (int i = 0; i < n; i++) {
			v[s[i] - '0'].first++;
		}
		sort(v.begin(), v.end());
		if (v[9].first - v[8].first >= v[8].first) {
			cout << n - v[9].first << "\n";
		}
		else {*/
			string s1 = "";
			for (int i = 0; i < 9; i++) {
				for (int j = i; j < 10; j++) {
					s1 = "";
					for (int qq = 0; qq < n; qq++) {
						if (s[qq] - '0' == i || s[qq] - '0' == j) {
							s1 += s[qq];
						}
					}
					ll f = 0, cos = 0;
					for (int i = 2; i < s1.length(); i++) {
						if (s1[i] != s1[(i + f) % 2]) {
							f++;
							cos++;
						}
					}
					if ((s1.length() - cos) % 2 == 1 && s1.length() != 1) {
						cos++;
					}
					if (ans > n - s1.length() + cos) {
						ans = n - s1.length() + cos;
					}
				}
			}
			cout << ans << "\n";
		//}

	}

}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;

int main()
{
	ll n, t, ans;
	string s;
	cin >> t;
	while (t--) {
		ans = 1e10;
		cin >> s;
		n = s.size();
		string s1 = "";
		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < 10; j++) {
				s1 = "";
				for (int qq = 0; qq < n; qq++) {
					if ((int)(s[qq] - '0') == i || (int)(s[qq] - '0') == j) {
						s1 += s[qq];
					}
				}
				ll f = 0, ff = 0, cos = 0;
				for (int qqq = 2; qqq < s1.size(); qqq++) {
					if (!f && s1[qqq] - '0' == i || f && s1[qqq] == j) {
						f++;
						cos++;
					}
					f = (f + 1) % 2;
				}
				if ((s1.size() - cos) % 2 == 1 && i != j) {
					cos++;
				}
				if (ans > n - s1.size() + cos) {
					ans = n - s1.size() + cos;
				}
			}
		}
		cout << ans << "\n";
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;

int main()
{
	ll n, t, ans;
	string s;
	cin >> t;
	while (t--) {
		ans = 1e10;
		cin >> s;
		n = s.size();
		string s1 = "";
		for (int i = 0; i < 10; i++) {
			for (int j = i; j < 10; j++) {
				s1 = "";
				for (int qq = 0; qq < n; qq++) {
					if ((int)(s[qq] - '0') == i || (int)(s[qq] - '0') == j) {
						s1 += s[qq];
					}
				}
				ll f = 0, cos = 0;
				for (int i = 2; i < s1.size(); i++) {
					if (s1[i] != s1[(i + f) % 2]) {
						f = (f + 1) % 2;
						cos++;
					}
				}
				if ((s1.size() - cos) % 2 == 1 && i != j) {
					cos++;
				}
				if (ans > n - s1.size() + cos) {
					ans = n - s1.size() + cos;
				}
			}
		}
		cout << ans << "\n";
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;

int main()
{
	ll n, t, ans;
	string s;
	cin >> t;
	while (t--) {
		ans = 1e10;
		cin >> s;
		n = s.size();
		string s1 = "";
		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < 10; j++) {
				s1 = "";
				for (int qq = 0; qq < n; qq++) {
					if ((int)(s[qq] - '0') == i || (int)(s[qq] - '0') == j) {
						s1 += s[qq];
					}
				}
				ll f = 0, ff = 0, cos = 0;
				for (int qqq = 2; qqq < s1.size(); qqq++) {
					if (!f && s1[qqq] - '0' == i || f && s1[qqq] == j) {
						f++;
						cos++;
					}
					f++;
				}
				if ((s1.size() - cos) % 2 == 1 && i != j) {
					cos++;
				}
				if (ans > n - s1.size() + cos) {
					ans = n - s1.size() + cos;
				}
			}
		}
		cout << ans << "\n";
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;

int main()
{
	ll n, t, ans;
	string s;
	cin >> t;
	while (t--) {
		ans = 1e10;
		cin >> s;
		n = s.size();
		string s1 = "";
		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < 10; j++) {
				s1 = "";
				for (int qq = 0; qq < n; qq++) {
					if ((int)(s[qq] - '0') == i || (int)(s[qq] - '0') == j) {
						s1 += s[qq];
					}
				}
				ll f = 0, cos = 0;
				for (int qqq = 2; qqq < s1.size(); qqq++) {
					if (s1[qqq] != s1[(qqq + f) % 2]) {
						f++;
						cos++;
					}
				}
				if ((s1.size() - cos) % 2 == 1 && i != j) {
					cos++;
				}
				if (ans > n - s1.size() + cos) {
					ans = n - s1.size() + cos;
				}
			}
		}
		cout << ans << "\n";
	}
}


#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define fi first
#define se second
#define all(container) container.begin(),container.end()
#define fast ios::sync_with_stdio(0); cin.tie(0)
#ifndef ONLINE_JUDGE
#include "debug.cpp"
#endif
#define int long long
const int mod = 1e9+7;
int power(int a,int b) { if(b==0) return 1; int res = power(a,b/2); if(b&1) return res * res * a; else return res * res; }

int32_t main(){

  fast;
  int tc; cin >> tc;
  while(tc--){
    string s; cin >> s;

    int freq[10] = {0};

    for(int i=0;i<s.size();i++){
      freq[s[i]-'0']++;
    }

    int ans = INT_MAX, N = s.size();

    for(int i=0;i<10;i++){
      for(int j=0;j<10;j++){
        if(i==j){
          ans = min(ans,N-freq[i]);
        }else{
          if(freq[i]==0||freq[j]==0) ans = min(ans,N-(freq[i]+freq[j]));
          else ans = min(ans,N-2*min(freq[i],freq[j]));
        }
      }
    }

    cout << ans << '\n';

  }

  return 0;
}


#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define fi first
#define se second
#define all(container) container.begin(),container.end()
#define fast ios::sync_with_stdio(0); cin.tie(0)
#ifndef ONLINE_JUDGE
#include "debug.cpp"
#endif
#define int long long
const int mod = 1e9+7;
int power(int a,int b) { if(b==0) return 1; int res = power(a,b/2); if(b&1) return res * res * a; else return res * res; }

int32_t main(){

  fast;
  int tc; cin >> tc;
  while(tc--){
    string s; cin >> s;

    int freq1[10] = {0};
    int freq2[10] = {0};

    for(int i=0;i<s.size();i++){
      if(i%2==0){
        freq1[s[i]-'0']++;
      }else{
        freq2[s[i]-'0']++;
      }
    }

    int N = s.size();
    int ans = N-2;

    for(int i=0;i<10;i++){
      if(freq1[i]!=0){
        for(int j=0;j<10;j++){
          if(freq2[j]!=0){
            int minn = min(freq1[i],freq2[j]);
            ans = min(ans,N-2*minn);
          }
        }
      }
    }

    cout << ans << '\n';

  }

  return 0;
}


#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define fi first
#define se second
#define all(container) container.begin(),container.end()
#define fast ios::sync_with_stdio(0); cin.tie(0)
#ifndef ONLINE_JUDGE
#include "debug.cpp"
#endif
#define int long long
const int mod = 1e9+7;
int power(int a,int b) { if(b==0) return 1; int res = power(a,b/2); if(b&1) return res * res * a; else return res * res; }

int32_t main(){

  fast;
  int tc; cin >> tc;
  while(tc--){
    string s; cin >> s;

    int freq1[10] = {0};
    int freq2[10] = {0};

    for(int i=0;i<s.size();i++){
      if(i%2==0){
        freq1[s[i]-'0']++;
      }else{
        freq2[s[i]-'0']++;
      }
    }

    int N = s.size();
    int ans = N-2;

    for(int i=0;i<10;i++){
      for(int j=0;j<10;j++){
        if(i!=j){
          int minn = min(freq1[i],freq2[j]);
          if(freq1[i]>0&&freq2[j]>0){
            int minn = min(freq1[i],freq2[j]);
            ans = min(ans,N-2*minn);
          }else if(freq1[i]>0){
            ans = min(ans,N - freq1[i]);
          }else if(freq2[j]>0){
            ans = min(ans,N - freq2[j]);
          }
        }else{
          ans = min(ans,N-(freq1[i]+freq2[j]));
        }
      }
    }

    cout << ans << '\n';

  }

  return 0;
}


#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define fi first
#define se second
#define all(container) container.begin(),container.end()
#define fast ios::sync_with_stdio(0); cin.tie(0)
#ifndef ONLINE_JUDGE
#include "debug.cpp"
#endif
#define int long long
const int mod = 1e9+7;
int power(int a,int b) { if(b==0) return 1; int res = power(a,b/2); if(b&1) return res * res * a; else return res * res; }

int32_t main(){

  fast;
  int tc; cin >> tc;
  while(tc--){
    string s; cin >> s;
    map<int,vector<int>> mp;

    for(int i=0;i<s.size();i++){
      mp[s[i]-'0'].push_back(i);
    }

    int N = s.size(), ans = INT_MAX;
    int glb = INT_MIN;

    for(int i=0;i<10;i++){
      for(int j=0;j<10;j++){
        if(i==j&&mp[i].size()){
          int sz = mp[i].size();
          ans = min(ans,N-sz);
          // print(ans);
        }else{

            vector<int>& v1 = mp[i];
            vector<int>& v2 = mp[j];

            int p1 = 0, p2 = 0;
            bool turn = true;

            vector<int> v = {-1};

            while(p1<v1.size()&&p2<v2.size()){
              if(turn){
                if(v1[p1]>v.back()) v.pb(v1[p1]);
                else p1++;
              }else{
                if(v2[p2]>v.back()) v.pb(v2[p2]);
                else p2++;
              }
              turn^=1;
            }

            if(turn){
              while(p1<v1.size()&&v1[p1]>v.back()) v.pb(v1[p1++]);
            }else{
              while(p2<v2.size()&&v2[p2]>v.back()) v.pb(v2[p2++]);
            }
            int sz = (v.size()-1);
            if(sz%2==1) sz--;
            ans = min(ans,N-sz);
            // print(i,j,ans,v1,v2,v);
        }
      }
    }

    cout << ans << '\n';

  }

  return 0;
}


#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define fi first
#define se second
#define all(container) container.begin(),container.end()
#define fast ios::sync_with_stdio(0); cin.tie(0)
#ifndef ONLINE_JUDGE
#include "debug.cpp"
#endif
#define int long long
const int mod = 1e9+7;
int power(int a,int b) { if(b==0) return 1; int res = power(a,b/2); if(b&1) return res * res * a; else return res * res; }

int32_t main(){

  fast;
  int tc; cin >> tc;
  while(tc--){
    string s; cin >> s;

    int freq1[10] = {0};
    int freq2[10] = {0};

    for(int i=0;i<s.size();i++){
      if(i%2==0){
        freq1[s[i]-'0']++;
      }else{
        freq2[s[i]-'0']++;
      }
    }

    int ans = 0 , k1 = 0 , k2 = 0;
    for(int i=0;i<10;i++){
      k1 = max(k1,freq1[i]);
      k2 = max(k2,freq2[i]);
    }

    int minn = min(k1,k2);
    k1 = minn;
    k2 = minn;

    ans = s.size() - 2*minn;

    cout << ans << '\n';

  }

  return 0;
}


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

namespace ConsoleApplication25
{
    class Solution
    {
        static void Main(string[] args)
        {
            var n = int.Parse(Console.ReadLine());
            var init = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();

            int maxSum = init.Sum();

            for (int i = 2; i < n / 2; i++)
            {
                if ( n % i == 0)
                {
                    for (int x = 0; x < i; x++)
                    {
                        int cursum = 0;
                        for (int y = x; y < n; y += i)
                        {
                            cursum += init[y];
                        }
                        maxSum = Math.Max(cursum,maxSum);
                    }
                }
            }

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


using System;
using System.Linq;

class Program {
  static void Main() {
    var n = int.Parse(Console.ReadLine());
    var t = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
    var res = 0;

    foreach (var d in Enumerable.Range(1, n).Where(d => n % d == 0 && n / d >= 3)) {
      var subs = new int[d];
      for (var i = 0 ; i < n ; i++) subs[i%d] += t[i];
      res = Math.Max(res, subs.Max());
    }
    Console.WriteLine(res);
  }
}


using System;
using System.Linq;

class Program {
  static void Main() {
    var n = int.Parse(Console.ReadLine());
    var t = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
    var res = int.MinValue;

    foreach (var d in Enumerable.Range(1, n).Where(d => n % d == 0 && n / d >= 3)) {
      var subs = new int[d];
      for (var i = 0 ; i < n ; i++) subs[i%d] += t[i];
      res = Math.Max(res, subs.Max());
    }
    Console.WriteLine(res);
  }
}


using System;
using System.Linq;

class Program {
  static void Main() {
    var n = int.Parse(Console.ReadLine());
    var t = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
    var res = Enumerable.Range(1, n)
                  .Where(d => n % d == 0 && n / d >= 3)
                  .SelectMany(d => Enumerable.Range(0, d), (d,r) => new {d, r})
                  .Select(x => Enumerable.Range(0,n).Where(i => i%x.d==x.r)
                                   .Select(i => t[i]).Sum())
                  .Max();
    Console.WriteLine(res);
  }
}


using System;
using System.Collections.Generic;

namespace Taxi
{
    class Directory
    {
        public string Name { get; set; }
        public Directory Parent;
        public Dictionary<string, Directory> Leaves = new Dictionary<string, Directory>();
        public string FullPath { get; set; }

        public Directory(string name, string path, Directory parent)
        {
            Name = name;
            FullPath = path;
            Parent = parent;
        }
        public void pwd()
        {
            Console.WriteLine(FullPath);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var n = Convert.ToInt32(Console.ReadLine());
            var str = Console.ReadLine().Split(' ');
            int[] t = new int[n];
            for (var i = 0; i < n; i++)
            {
                t[i] = Convert.ToInt32(str[i]);
            }
            var listDerivitives = new List<int>();
            for (var j = 3; j <= n; j++)
            {
                if (n%j == 0)
                {
                    listDerivitives.Add(j);
                }
            }
            var sum = 0;
            var maxSum = 0;
            for (var k = 0; k < listDerivitives.Count; k++)
            {
                var div = n/listDerivitives[k];
                for (var st = 0; st < div; st++)
                {
                    var edge = st;
                    sum = 0;
                    while (edge < n)
                    {
                        sum += t[edge];
                        edge += div;
                    }
                    if (k == 0 && st == 0)
                    {
                        maxSum = sum;
                    }
                    if (sum > maxSum)
                    {
                        maxSum = sum;
                    }
                }
            }
            Console.WriteLine(maxSum);

        }

    }
}


using System;
using System.Collections.Generic;

namespace Taxi
{
    class Directory
    {
        public string Name { get; set; }
        public Directory Parent;
        public Dictionary<string, Directory> Leaves = new Dictionary<string, Directory>();
        public string FullPath { get; set; }

        public Directory(string name, string path, Directory parent)
        {
            Name = name;
            FullPath = path;
            Parent = parent;
        }
        public void pwd()
        {
            Console.WriteLine(FullPath);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var n = Convert.ToInt32(Console.ReadLine());
            var str = Console.ReadLine().Split(' ');
            int[] t = new int[n];
            for (var i = 0; i < n; i++)
            {
                t[i] = Convert.ToInt32(str[i]);
            }
            var listDerivitives = new List<int>();
            for (var j = 3; j <= n; j++)
            {
                if (n%j == 0)
                {
                    listDerivitives.Add(j);
                }
            }
            var sum = 0;
            var maxSum = 0;
            for (var k = 0; k < listDerivitives.Count; k++)
            {
                var div = n/listDerivitives[k];
                for (var st = 0; st < div; st++)
                {
                    var edge = st;
                    sum = 0;
                    while (edge < n)
                    {
                        sum += t[edge];
                        edge += div;
                    }
                    if (sum > maxSum)
                    {
                        maxSum = sum;
                    }
                }
            }
            Console.WriteLine(maxSum);

        }

    }
}


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

namespace ConsoleApplication10
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = Convert.ToInt32(Console.ReadLine());
            string s = Console.ReadLine();
            string[] mas = s.Split(' ');
            int max = 0;
            int sum = 0;
            for (int i = 0; i < n; i++)
            {
                max = max + Convert.ToInt32(mas[i]);
            }
            for (int i = 2; i < (n/2 + 1) ; i++)
            {
                if (n % i == 0)
                {
                    for (int j = 0; j < i; j = j + 1)
                    {
                        for (int l = j; l < n; l = l + i)
                        {
                            sum = sum + Convert.ToInt32(mas[l]);
                        }
                        if (sum >= max)
                            max = sum;
                        sum = 0;
                    }
                }
            }
            Console.WriteLine(max);
        }
    }
}


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

namespace ConsoleApplication10
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = Convert.ToInt32(Console.ReadLine());
            string s = Console.ReadLine();
            string[] mas = s.Split(' ');
            int max = 0;
            int sum = 0;
            for (int i = 0; i < n; i++)
            {
                max = max + Convert.ToInt32(mas[i]);
            }
            for (int i = 2; i <= (n/3) ; i++)
            {
                if (n % i == 0)
                {
                    for (int j = 0; j < i; j = j + 1)
                    {
                        for (int l = j; l < n; l = l + i)
                        {
                            sum = sum + Convert.ToInt32(mas[l]);
                        }
                        if (sum >= max)
                            max = sum;
                        sum = 0;
                    }
                }
            }
            Console.WriteLine(max);
        }
    }
}


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

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

            //D
            string line = Console.ReadLine();
            var str = line.Split(' ');
            var arr = str.Select(el => Int32.Parse(el)).ToArray();
            int N = arr[0];
            line = Console.ReadLine();
            str = line.Split(' ');
            arr = str.Select(el => Int32.Parse(el)).ToArray();
            int res = arr.Sum();
            for (int divisor = 2; divisor < N / 2; divisor++)
            {
                if (N % divisor == 0)
                {
                    for (int startNum = 0; startNum < divisor; startNum++)
                    {
                        int sum = 0;
                        for (int i = startNum; i < N; i += divisor)
                        {
                            sum += arr[i];
                        }
                        if (sum > res) res = sum;
                    }
                }
            }
            Console.WriteLine(res);
            //Console.ReadLine();
        }
    }
}


/*
rahil_1407
*/
#include<bits/stdc++.h>
#define SPEED ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define endl '\n'
#define f first
#define s second
#define pii pair<int,int>
#define vi vector<int>
#define mp make_pair
#define pb push_back
#define fill(a) memset(a,0,sizeof(a))
#define For(i,a,b) for(int i=a;i<b;i++)
#define ford(i,a,b) for(int i=a;i>=b;i--)
#define pi 3.1415926536
#define MAX 1000005
#define int long long

const int N=10e8+7;
using namespace std;

vi v[MAX];
int32_t main()
{
    SPEED;
    int n;
    cin>>n;
    int arr[n-2][3];
    for(int i=0;i<n-2;i++)
    {
        for(int j=0;j<3;j++)
        {
            cin>>arr[i][j];
            v[arr[i][j]].pb(i);
        }
    }
    int ans[n+1];
    for(int i=1;i<MAX;i++)
    {
        if(v[i].size()==1) {
            ans[0]=i;
            break;
        }
    }
    int idx=v[ans[0]][0];
    for(int i=0;i<3;i++)
    {
        if(v[arr[idx][i]].size()==2)
        {
            ans[1]=arr[idx][i];
        }
        if(v[arr[idx][i]].size()>2)
            ans[2]=arr[idx][i];
    }
    for(int i=3;i<n;i++)
    {
       int x,y,p1=-1,p2=-1;
       x=ans[i-2];
       y=ans[i-1];
       map<int,int>m;
       for(auto j=0;j<v[x].size();j++) m[j]++;
       for(auto j=0;j<v[y].size();j++) m[j]++;
       for(auto it=m.begin();it!=m.end();it++)
       {
           if(it->s==2)
           {
               if(p1==-1) p1=it->f;
               else if(p2==-1) p2=it->f;
           }
       }
       int k1,k2;
       for(int j=0;j<3;j++)
       {
           if(arr[p1][j]!=x && arr[p1][j]!=y)
            k1=arr[p1][j];
       }
       for(int j=0;j<3;j++)
       {
           if(arr[p2][j]!=x && arr[p2][j]!=y)
            k2=arr[p1][j];
       }
       if(ans[i-3]==k1)
         ans[i]=k2;
       else if(ans[i-3]==k2)
        ans[i]=k1;
    }
    for(auto i:ans)
        cout<<i<<" ";
    cout<<endl;

}


/*
rahil_1407
*/
#include<bits/stdc++.h>
#define SPEED ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define endl '\n'
#define f first
#define s second
#define pii pair<int,int>
#define vi vector<int>
#define mp make_pair
#define pb push_back
#define fill(a) memset(a,0,sizeof(a))
#define For(i,a,b) for(int i=a;i<b;i++)
#define ford(i,a,b) for(int i=a;i>=b;i--)
#define pi 3.1415926536
#define MAX 1000005
#define int long long

const int N=10e8+7;
using namespace std;

vi v[MAX];
int32_t main()
{
    SPEED;
    int n;
    cin>>n;
    int arr[n-2][3];
    for(int i=0;i<n-2;i++)
    {
        for(int j=0;j<3;j++)
        {
            cin>>arr[i][j];
            v[arr[i][j]].pb(i);
        }
    }
    int ans[n+1];
    for(int i=1;i<MAX;i++)
    {
        if(v[i].size()==1) {
            ans[0]=i;
            break;
        }
    }
    int idx=v[ans[0]][0];
    for(int i=0;i<3;i++)
    {
        if(v[arr[idx][i]].size()==2)
        {
            ans[1]=arr[idx][i];
        }
        if(v[arr[idx][i]].size()>2)
            ans[2]=arr[idx][i];
    }
    for(int i=3;i<n;i++)
    {
       int x,y,p1=-1,p2=-1;
       x=ans[i-2];
       y=ans[i-1];
       map<int,int>m;
       for(auto j:v[x]) m[j]++;
       for(auto j:v[y]) m[j]++;
       for(auto it=m.begin();it!=m.end();it++)
       {
           if(it->s==2)
           {
               if(p1==-1) p1=it->f;
               if(p2==-1) p2=it->f;
           }
       }
       int k1,k2;
       for(int j=0;j<3;j++)
       {
           if(arr[p1][j]!=x && arr[p1][j]!=y)
            k1=arr[p1][j];
       }
       for(int j=0;j<3;j++)
       {
           if(arr[p2][j]!=x && arr[p2][j]!=y)
            k2=arr[p1][j];
       }
       if(ans[i-3]==k1)
         ans[i]=k2;
       else if(ans[i-3]==k2)
        ans[i]=k1;
    }
    for(auto i:ans)
        cout<<i<<" ";
    cout<<endl;

}


/*
rahil_1407
Consistency is the key to improve yourself everyday!!
*/
#include<bits/stdc++.h>
#define SPEED ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define endl '\n'
#define f first
#define s second
#define pii pair<int,int>
#define vi vector<int>
#define mp make_pair
#define pb push_back
#define fill(a) memset(a,0,sizeof(a))
#define For(i,a,b) for(int i=a;i<b;i++)
#define ford(i,a,b) for(int i=a;i>=b;i--)
#define pi 3.1415926536
#define MAX 1000005
#define int long long

const int N=10e8+7;
using namespace std;

vi v[MAX];
int arr[MAX][3];
int32_t main()
{
    SPEED;
    int n;
    cin>>n;
    For(i,0,n-2)
    {
        For(j,0,3)
        {
            cin>>arr[i][j];
            v[arr[i][j]].pb(i);
        }
    }
    int ans[n];
    memset(ans,-1,sizeof(ans));
    For(i,0,MAX)
    {
        if(v[i].size()==1)
        {
            ans[0]=i;
            int idx=v[i][0];
            For(j,0,3)
            {
                if(arr[idx][j]!=i && v[arr[idx][j]].size()==2)
                    ans[1]=arr[idx][j];
                else if(arr[idx][j]!=i && v[arr[idx][j]].size()>2)
                    ans[2]=arr[idx][j];
            }
            break;
        }
    }
    for(int i=3;i<n;i++)
    {
        int p1=-1,p2=-1;
        int x=ans[i-2],y=ans[i-1];
        for(auto j:v[x])
        {
            for(auto k:v[y])
            {
                if(j==k)
                {
                    if(p1==-1) p1=j;
                    else if(p2==-1) p2=j;
                }
            }
        }
        int k1,k2;
        For(j,0,3)
        {
            if(arr[p1][j]!=x && arr[p1][j]!=y)
            {
                k1=arr[p1][j];
            }
        }
        For(j,0,3)
        {
            if(arr[p2][j]!=x && arr[p2][j]!=y)
            {
                k2=arr[p2][j];
            }
        }
        if(k1==ans[i-3])
            ans[i]=k2;
        else if(k2==ans[i-3])
            ans[i]=k1;
    }
    for(auto i:ans)
        cout<<i<<" ";
    cout<<endl;
}


/*
rahil_1407
*/
#include<bits/stdc++.h>
#define SPEED ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define endl '\n'
#define f first
#define s second
#define pii pair<int,int>
#define vi vector<int>
#define mp make_pair
#define pb push_back
#define fill(a) memset(a,0,sizeof(a))
#define For(i,a,b) for(int i=a;i<b;i++)
#define ford(i,a,b) for(int i=a;i>=b;i--)
#define pi 3.1415926536
#define MAX 1000005
#define int long long

const int N=10e8+7;
using namespace std;

vi v[MAX];
int32_t main()
{
    SPEED;
    int n;
    cin>>n;
    int arr[n-2][3];
    for(int i=0;i<n-2;i++)
    {
        for(int j=0;j<3;j++)
        {
            cin>>arr[i][j];
            v[arr[i][j]].pb(i);
        }
    }
    int ans[n+1];
    for(int i=1;i<MAX;i++)
    {
        if(v[i].size()==1) {
            ans[0]=i;
            break;
        }
    }
    int idx=v[ans[0]][0];
    for(int i=0;i<3;i++)
    {
        if(v[arr[idx][i]].size()==2)
        {
            ans[1]=arr[idx][i];
        }
        if(v[arr[idx][i]].size()>2)
            ans[2]=arr[idx][i];
    }
    for(int i=3;i<n;i++)
    {
       int x,y,p1=-1,p2=-1;
       x=ans[i-2];
       y=ans[i-1];
       map<int,int>m;
       for(auto j=0;j<v[x].size();j++) m[j]++;
       for(auto j=0;j<v[y].size();j++) m[j]++;
       for(auto it=m.begin();it!=m.end();it++)
       {
           if(it->s==2)
           {
               if(p1==-1) p1=-1;
               else if(p2==-1) p2=-1;
           }
       }
       int k1,k2;
       for(int j=0;j<3;j++)
       {
           if(arr[p1][i]!=x && arr[p1][i]!=y)
            k1=arr[p1][i];
       }
       for(int j=0;j<3;j++)
       {
           if(arr[p2][i]!=x && arr[p2][i]!=y)
            k2=arr[p1][i];
       }
       if(ans[i-3]==k1)
         ans[i]=k2;
       else if(ans[i-3]==k2)
        ans[i]=k1;
    }
    for(auto i:ans)
        cout<<i<<" ";
    cout<<endl;

}


/*
rahil_1407
*/
#include<bits/stdc++.h>
#define SPEED ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define endl '\n'
#define f first
#define s second
#define pii pair<int,int>
#define vi vector<int>
#define mp make_pair
#define pb push_back
#define fill(a) memset(a,0,sizeof(a))
#define For(i,a,b) for(int i=a;i<b;i++)
#define ford(i,a,b) for(int i=a;i>=b;i--)
#define pi 3.1415926536
#define MAX 1000005
#define int long long

const int N=10e8+7;
using namespace std;

vi v[MAX];
int32_t main()
{
    SPEED;
    int n;
    cin>>n;
    int arr[MAX][3];
    for(int i=0;i<n-2;i++)
    {
        for(int j=0;j<3;j++)
        {
            cin>>arr[i][j];
            v[arr[i][j]].pb(i);
        }
    }
    int ans[MAX];
    for(int i=1;i<MAX;i++)
    {
        if(v[i].size()==1) {
            ans[0]=i;
            break;
        }
    }
    int idx=v[ans[0]][0];
    for(int i=0;i<3;i++)
    {
        if(v[arr[idx][i]].size()==2)
            ans[1]=arr[idx][i];
        if(v[arr[idx][i]].size()>2)
            ans[2]=arr[idx][i];
    }
    /*for(int i=3;i<n;i++)
    {
        int x,y;
        x=ans[i-2];
        y=ans[i-1];
        //map<int,int>mm;
        //for(auto j:v[x]) mm[j]++;
    }*/
    for(auto i:ans)
        cout<<i<<" ";
    cout<<endl;

}


#include <bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
#define mid(l, r) ((l + r) >> 1)
#define assign(x,val) memset(x,val,sizeof(x))
#define all(x) (x).begin(), (x).end()
#define for0(i,n) for(ll i=0;i<n;++i)
#define for1(i,n) for(ll i=1;i<=n;++i)
#define ford(i,n) for(ll i=n;i>=1;--i)
#define forg(i,a,b) for(ll i=a;i<b;++i)
using namespace std;
typedef long long ll;
long long mod = 1000000007;
inline ll nextInt(){ ll x; scanf("%d",&x); return x; }
inline ll nextLongLong(){ ll x; scanf("%lld",&x); return x; }
inline void printInt(ll x){printf("%d\n",x);}
inline void printLongLong(ll x){printf("%lld\n",x);}
const long double PI = (long double)(3.1415926535897932384626433832795);
template<class T> bool win(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool wax(T &a, T b) { return a < b ? (a = b, true) : false; }
const ll N = (int)1e5 + 1;

void solve(){
	int n;cin>>n;
	set<int>queries[n-2], numberindexes[n+1];
	unordered_map<int, int> cnt, done, visitquery;
	std::vector<int> ans;

	for0(i,n-2){
		int a, b, c;cin>>a>>b>>c;
		queries[i].insert(a);
		queries[i].insert(b);
		queries[i].insert(c);
		numberindexes[a].insert(i);
		numberindexes[b].insert(i);
		numberindexes[c].insert(i);
		cnt[a]++;cnt[b]++;cnt[c]++;
		// cout<<i<<endl;
	}
	// cout<<123<<endl;
	int st = 0,end=0;
	for(auto x : cnt){
		if(st &&x.ss==1){
			end = x.ff;
			break;
		}
		if(x.ss==1){
			st = x.ff;
		}

	}
	ans.pb(st);
	int temp=0, temp2 = 0, val = 0, val2 = 0;
	int currquery = 0;
	while(temp!=end && temp2 != end){
		//cout<<st<<endl;
		// ans.pb(st);
		done[st]=1;
		int total = 10, tmp = 0;
		for(auto y : numberindexes[st]){
			if(visitquery[y]) continue;
			for(auto x : queries[y]){
					if(done[x]) continue;
					tmp++;
				}
				if(tmp < total){
					currquery = y;
				}
		}
		// for0(i,n-2){
		// 	if(queries[i].find(st)!=queries[i].end() &&!visitquery[i]){
		// 		//cout<<i<<endl;

		// 		//break;
		// 	}
		// }
		visitquery[currquery] = 1;
		temp=0, temp2 = 0, val = 0, val2 = 0;
		for(auto x : queries[currquery]){
			if(done[x]) continue;
			if(temp){
				temp2 = x;
			} else{
				temp = x;
			}
		}
		for(auto x : numberindexes[temp]){
			if(visitquery[x]){
				continue;
			}
			for(auto y : queries[x]){
				if(done[y]) continue;
				val++;
			}

		}
		for(auto x : numberindexes[temp2]){
			if(visitquery[x]){
				continue;
			}
			for(auto y : queries[x]){
				if(done[y]) continue;
				val2++;
			}
			//val2++;
		}
		if(val2<val){
			swap(val, val2);
			swap(temp, temp2);
		}
		if(temp && temp!=end){
			ans.pb(temp);
			done[temp] = 1;
			st = temp;
		}
		if(temp2 && temp2!=end){
			ans.pb(temp2);
			done[temp2] = 1;
			st = temp2;
		}
		//cout<<temp<<" "<<temp2<<endl;
	}
	ans.pb(end);
	for(auto x : ans){
		cout<<x<<" ";
	}
	//cout<<ans.size();
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);

    //freopen("output.txt", "w", stdout);
#endif
    ll t=1;
    //cin>>t;
    for0(i,t) solve();
    //cout<<1<<endl;
#ifndef ONLINE_JUDGE
    cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}


#include <bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
#define mid(l, r) ((l + r) >> 1)
#define assign(x,val) memset(x,val,sizeof(x))
#define all(x) (x).begin(), (x).end()
#define for0(i,n) for(ll i=0;i<n;++i)
#define for1(i,n) for(ll i=1;i<=n;++i)
#define ford(i,n) for(ll i=n;i>=1;--i)
#define forg(i,a,b) for(ll i=a;i<b;++i)
using namespace std;
typedef long long ll;
long long mod = 1000000007;
inline ll nextInt(){ ll x; scanf("%d",&x); return x; }
inline ll nextLongLong(){ ll x; scanf("%lld",&x); return x; }
inline void printInt(ll x){printf("%d\n",x);}
inline void printLongLong(ll x){printf("%lld\n",x);}
const long double PI = (long double)(3.1415926535897932384626433832795);
template<class T> bool win(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool wax(T &a, T b) { return a < b ? (a = b, true) : false; }
const ll N = (int)1e5 + 1;

void solve(){
	int n;cin>>n;
	set<int>queries[n-2], numberindexes[n+1];
	unordered_map<int, int> cnt, done, visitquery;
	std::vector<int> ans;

	for0(i,n-2){
		int a, b, c;cin>>a>>b>>c;
		queries[i].insert(a);
		queries[i].insert(b);
		queries[i].insert(c);
		numberindexes[a].insert(i);
		numberindexes[b].insert(i);
		numberindexes[c].insert(i);
		cnt[a]++;cnt[b]++;cnt[c]++;
		// cout<<i<<endl;
	}
	// cout<<123<<endl;
	int st = 0,end=0;
	for(auto x : cnt){
		if(st &&x.ss==1){
			end = x.ff;
			break;
		}
		if(x.ss==1){
			st = x.ff;
		}

	}
	ans.pb(st);
	int currquery = 0;
	while(ans.size()<n){
		//cout<<st<<endl;
		// ans.pb(st);
		done[st]=1;
		int total = 10, tmp = 0;
		for0(i,n-2){
			if(queries[i].find(st)!=queries[i].end() &&!visitquery[i]){
				//cout<<i<<endl;
				for(auto x : queries[i]){
					if(done[x]) continue;
					tmp++;
				}
				if(tmp < total){
					currquery = i;
				}

				//break;
			}
		}
		visitquery[currquery] = 1;
		int temp=0, temp2 = 0, val = 0, val2 = 0;
		for(auto x : queries[currquery]){
			if(done[x]) continue;
			if(temp){
				temp2 = x;
			} else{
				temp = x;
			}
		}
		for(auto x : numberindexes[temp]){
			if(visitquery[x]){
				continue;
			}
			for(auto y : queries[x]){
				if(done[y]) continue;
				val++;
			}

		}
		for(auto x : numberindexes[temp2]){
			if(visitquery[x]){
				continue;
			}
			for(auto y : queries[x]){
				if(done[y]) continue;
				val2++;
			}
			//val2++;
		}
		if(val2<val){
			swap(val, val2);
			swap(temp, temp2);
		}
		if(temp){
			ans.pb(temp);
			done[temp] = 1;
			st = temp;
		}
		if(temp2){
			ans.pb(temp2);
			done[temp2] = 1;
			st = temp2;
		}
		//cout<<temp<<" "<<temp2<<endl;
	}
	for(auto x : ans){
		cout<<x<<" ";
	}
	//cout<<ans.size();
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);

    //freopen("output.txt", "w", stdout);
#endif
    ll t=1;
    //cin>>t;
    for0(i,t) solve();
    //cout<<1<<endl;
#ifndef ONLINE_JUDGE
    cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}


#include <bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
#define mid(l, r) ((l + r) >> 1)
#define assign(x,val) memset(x,val,sizeof(x))
#define all(x) (x).begin(), (x).end()
#define for0(i,n) for(ll i=0;i<n;++i)
#define for1(i,n) for(ll i=1;i<=n;++i)
#define ford(i,n) for(ll i=n;i>=1;--i)
#define forg(i,a,b) for(ll i=a;i<b;++i)
using namespace std;
typedef long long ll;
long long mod = 1000000007;
inline ll nextInt(){ ll x; scanf("%d",&x); return x; }
inline ll nextLongLong(){ ll x; scanf("%lld",&x); return x; }
inline void printInt(ll x){printf("%d\n",x);}
inline void printLongLong(ll x){printf("%lld\n",x);}
const long double PI = (long double)(3.1415926535897932384626433832795);
template<class T> bool win(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool wax(T &a, T b) { return a < b ? (a = b, true) : false; }
const ll N = (int)1e5 + 1;

void solve(){
	int n;cin>>n;
	set<int>queries[n-2], numberindexes[n+1];
	unordered_map<int, int> cnt, done, visitquery;
	std::vector<int> ans;

	for0(i,n-2){
		int a, b, c;cin>>a>>b>>c;
		queries[i].insert(a);
		queries[i].insert(b);
		queries[i].insert(c);
		numberindexes[a].insert(i);
		numberindexes[b].insert(i);
		numberindexes[c].insert(i);
		cnt[a]++;cnt[b]++;cnt[c]++;
		// cout<<i<<endl;
	}
	// cout<<123<<endl;
	int st = 0,end=0, scend = 0;
	for(auto x : cnt){
		if(st &&x.ss==1){
			end = x.ff;
			break;
		}
		if(x.ss==1){
			st = x.ff;
		}

	}
	for(auto x : numberindexes[end]){
		for(auto y : queries[x]){
			if(cnt[y]==2){
				scend = y;break;
			}
		}
	}
	ans.pb(st);
	int temp=0, temp2 = 0, val = 0, val2 = 0;
	int currquery = 0;
	while(ans.size()!=n-2 ){
		//cout<<st<<endl;
		// ans.pb(st);
		done[st]=1;
		int total = 10, tmp = 0;
		for(auto y : numberindexes[st]){
			if(visitquery[y]) continue;
			for(auto x : queries[y]){
					if(done[x]) continue;
					tmp++;
				}
				if(tmp < total){
					currquery = y;
				}
		}
		// for0(i,n-2){
		// 	if(queries[i].find(st)!=queries[i].end() &&!visitquery[i]){
		// 		//cout<<i<<endl;

		// 		//break;
		// 	}
		// }
		visitquery[currquery] = 1;
		temp=0, temp2 = 0, val = 0, val2 = 0;
		for(auto x : queries[currquery]){
			if(done[x]) continue;
			if(temp){
				temp2 = x;
			} else{
				temp = x;
			}
		}
		for(auto x : numberindexes[temp]){
			if(visitquery[x]){
				continue;
			}
			for(auto y : queries[x]){
				if(done[y]) continue;
				val++;
			}

		}
		for(auto x : numberindexes[temp2]){
			if(visitquery[x]){
				continue;
			}
			for(auto y : queries[x]){
				if(done[y]) continue;
				val2++;
			}
			//val2++;
		}
		if(val2<val){
			swap(val, val2);
			swap(temp, temp2);
		}
		if(temp && temp!=end && temp!=scend){
			ans.pb(temp);
			done[temp] = 1;
			st = temp;
		}
		if(temp2 && temp2!=end && temp2!=scend){
			ans.pb(temp2);
			done[temp2] = 1;
			st = temp2;
		}
		//cout<<temp<<" "<<temp2<<endl;
	}
	ans.pb(scend);
	ans.pb(end);
	for(auto x : ans){
		cout<<x<<" ";
	}
	//cout<<ans.size();
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);

    //freopen("output.txt", "w", stdout);
#endif
    ll t=1;
    //cin>>t;
    for0(i,t) solve();
    //cout<<1<<endl;
#ifndef ONLINE_JUDGE
    cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}


#include <bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
#define mid(l, r) ((l + r) >> 1)
#define assign(x,val) memset(x,val,sizeof(x))
#define all(x) (x).begin(), (x).end()
#define for0(i,n) for(ll i=0;i<n;++i)
#define for1(i,n) for(ll i=1;i<=n;++i)
#define ford(i,n) for(ll i=n;i>=1;--i)
#define forg(i,a,b) for(ll i=a;i<b;++i)
using namespace std;
typedef long long ll;
long long mod = 1000000007;
inline ll nextInt(){ ll x; scanf("%d",&x); return x; }
inline ll nextLongLong(){ ll x; scanf("%lld",&x); return x; }
inline void printInt(ll x){printf("%d\n",x);}
inline void printLongLong(ll x){printf("%lld\n",x);}
const long double PI = (long double)(3.1415926535897932384626433832795);
template<class T> bool win(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool wax(T &a, T b) { return a < b ? (a = b, true) : false; }
const ll N = (int)1e5 + 1;

void solve(){
	int n;cin>>n;
	set<int>queries[n-2], numberindexes[n+1];
	unordered_map<int, int> cnt, done, visitquery;
	std::vector<int> ans;

	for0(i,n-2){
		int a, b, c;cin>>a>>b>>c;
		queries[i].insert(a);
		queries[i].insert(b);
		queries[i].insert(c);
		numberindexes[a].insert(i);
		numberindexes[b].insert(i);
		numberindexes[c].insert(i);
		cnt[a]++;cnt[b]++;cnt[c]++;
		// cout<<i<<endl;
	}
	// cout<<123<<endl;
	int st = 0,end=0;
	for(auto x : cnt){
		if(st &&x.ss==1){
			end = x.ff;
			break;
		}
		if(x.ss==1){
			st = x.ff;
		}

	}
	ans.pb(st);
	int currquery = 0;
	while(ans.size()<n){
		cout<<st<<endl;
		// ans.pb(st);
		done[st]=1;
		int total = 10, tmp = 0;
		for0(i,n-2){
			if(queries[i].find(st)!=queries[i].end() &&!visitquery[i]){
				//cout<<i<<endl;
				for(auto x : queries[i]){
					if(done[x]) continue;
					tmp++;
				}
				if(tmp < total){
					currquery = i;
				}

				//break;
			}
		}
		visitquery[currquery] = 1;
		int temp=0, temp2 = 0, val = 0, val2 = 0;
		for(auto x : queries[currquery]){
			if(done[x]) continue;
			if(temp){
				temp2 = x;
			} else{
				temp = x;
			}
		}
		for(auto x : numberindexes[temp]){
			if(visitquery[x]){
				continue;
			}
			for(auto y : queries[x]){
				if(done[y]) continue;
				val++;
			}

		}
		for(auto x : numberindexes[temp2]){
			if(visitquery[x]){
				continue;
			}
			for(auto y : queries[x]){
				if(done[y]) continue;
				val2++;
			}
			//val2++;
		}
		if(val2<val){
			swap(val, val2);
			swap(temp, temp2);
		}
		if(temp){
			ans.pb(temp);
			done[temp] = 1;
			st = temp;
		}
		if(temp2){
			ans.pb(temp2);
			done[temp2] = 1;
			st = temp2;
		}
		//cout<<temp<<" "<<temp2<<endl;
	}
	for(auto x : ans){
		cout<<x<<" ";
	}
	//cout<<ans.size();
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);

    //freopen("output.txt", "w", stdout);
#endif
    ll t=1;
    //cin>>t;
    for0(i,t) solve();
    //cout<<1<<endl;
#ifndef ONLINE_JUDGE
    cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}


#include <bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
#define mid(l, r) ((l + r) >> 1)
#define assign(x,val) memset(x,val,sizeof(x))
#define all(x) (x).begin(), (x).end()
#define for0(i,n) for(ll i=0;i<n;++i)
#define for1(i,n) for(ll i=1;i<=n;++i)
#define ford(i,n) for(ll i=n;i>=1;--i)
#define forg(i,a,b) for(ll i=a;i<b;++i)
using namespace std;
typedef long long ll;
long long mod = 1000000007;
inline ll nextInt(){ ll x; scanf("%d",&x); return x; }
inline ll nextLongLong(){ ll x; scanf("%lld",&x); return x; }
inline void printInt(ll x){printf("%d\n",x);}
inline void printLongLong(ll x){printf("%lld\n",x);}
const long double PI = (long double)(3.1415926535897932384626433832795);
template<class T> bool win(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool wax(T &a, T b) { return a < b ? (a = b, true) : false; }
const ll N = (int)1e5 + 1;

void solve(){
	int n;cin>>n;
	set<int>queries[n-2], numberindexes[n+1];
	unordered_map<int, int> cnt, done, visitquery;
	std::vector<int> ans;

	for0(i,n-2){
		int a, b, c;cin>>a>>b>>c;
		queries[i].insert(a);
		queries[i].insert(b);
		queries[i].insert(c);
		numberindexes[a].insert(i);
		numberindexes[b].insert(i);
		numberindexes[c].insert(i);
		cnt[a]++;cnt[b]++;cnt[c]++;
		// cout<<i<<endl;
	}
	// cout<<123<<endl;
	int st = 0,end=0;
	for(auto x : cnt){
		if(st &&x.ss==1){
			end = x.ff;
			break;
		}
		if(x.ss==1){
			st = x.ff;
		}

	}
	ans.pb(st);
	int temp=0, temp2 = 0, val = 0, val2 = 0;
	int currquery = 0;
	while(temp!=end && temp2 != end){
		//cout<<st<<endl;
		// ans.pb(st);
		done[st]=1;
		int total = 10, tmp = 0;
		for0(i,n-2){
			if(queries[i].find(st)!=queries[i].end() &&!visitquery[i]){
				//cout<<i<<endl;
				for(auto x : queries[i]){
					if(done[x]) continue;
					tmp++;
				}
				if(tmp < total){
					currquery = i;
				}

				//break;
			}
		}
		visitquery[currquery] = 1;
		temp=0, temp2 = 0, val = 0, val2 = 0;
		for(auto x : queries[currquery]){
			if(done[x]) continue;
			if(temp){
				temp2 = x;
			} else{
				temp = x;
			}
		}
		for(auto x : numberindexes[temp]){
			if(visitquery[x]){
				continue;
			}
			for(auto y : queries[x]){
				if(done[y]) continue;
				val++;
			}

		}
		for(auto x : numberindexes[temp2]){
			if(visitquery[x]){
				continue;
			}
			for(auto y : queries[x]){
				if(done[y]) continue;
				val2++;
			}
			//val2++;
		}
		if(val2<val){
			swap(val, val2);
			swap(temp, temp2);
		}
		if(temp && temp!=end){
			ans.pb(temp);
			done[temp] = 1;
			st = temp;
		}
		if(temp2 && temp2!=end){
			ans.pb(temp2);
			done[temp2] = 1;
			st = temp2;
		}
		//cout<<temp<<" "<<temp2<<endl;
	}
	ans.pb(end);
	for(auto x : ans){
		cout<<x<<" ";
	}
	//cout<<ans.size();
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);

    //freopen("output.txt", "w", stdout);
#endif
    ll t=1;
    //cin>>t;
    for0(i,t) solve();
    //cout<<1<<endl;
#ifndef ONLINE_JUDGE
    cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+5;
int n,t;
vector<int> v[N];
int vis[N],cnt[N];
int main(){
	cin>>n;
	int x,y,z;
	for(int i=1;i<=n-2;i++){
		scanf("%d%d%d",&x,&y,&z);
		v[x].push_back(y),v[x].push_back(z);
		v[y].push_back(x),v[y].push_back(z);
		v[z].push_back(x),v[z].push_back(y);
		cnt[x]++,cnt[y]++,cnt[z]++;
	}
	for(int i=1;i<=n;i++){
		if(cnt[i]==1){
			x=i;
			break;
		}
	}
	if(cnt[v[x][0]]==2)y=v[x][0];
	else y=v[x][1];
	vis[x]=vis[y]=1;
	printf("%d %d ",x,y);
	for(int i=1;i<=n-2;i++){
		int z;
		for(int j=0;j<v[x].size();j++){
			if(!vis[v[x][j]]){z=v[x][j];break;}
		}
		vis[z]=1;
		printf("%d ",z);
		x=y;
		y=z;
	}
	return 0;
}


#include <bits/stdc++.h>
#define mod 1000000007
#define speedgeek ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define f(i,x,y) for(i=x;i<y;i++)
#define pb push_back
#define ll long long
#define mll map<long long,long long>
#define mcl map<char,long long>
#define vll vector<long long>
#define sll set<long long>
using namespace std;
int main(){
    ll a,b,t,ans,i;
    cin>>t;
    sll s;
    while(t--){
        for(i=0;i<3;i++){
            cin>>a;
            s.insert(a);
        }
    }
    for(auto x:s) cout<<x<<" ";
    return 0;
}


#include <bits/stdc++.h>
#define mod 1000000007
#define speedgeek ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define f(i,x,y) for(i=x;i<y;i++)
#define pb push_back
#define ll long long
#define mll map<long long,long long>
#define mcl map<char,long long>
#define vll vector<long long>
#define sll set<long long>
using namespace std;
int main(){
    speedgeek;
    ll n,i,x,y,z,j;
    mll m;ll vis[100001]={0};
    cin>>n;
    vll v[n+1];
    for(i=0;i<n-2;i++){
        cin>>x>>y>>z;
        v[x].pb(y);v[x].pb(z);m[x]++;
        v[y].pb(x);v[y].pb(z);m[y]++;
        v[z].pb(y);v[z].pb(x);m[z]++;
    }
    f(i,1,n+1){
        if(m[i]==1){
            x=i;
            if(m[v[x][0]]!=1) y=v[x][0];
            else y=v[x][1];
            break;
        }
    }vis[x]=1;vis[y]=1;
    cout<<x<<" "<<y<<" ";
    f(i,0,n-2){
        for(j=0;j<v[x].size();j++){
            if(!vis[v[x][j]]){
                z=v[x][j];
                vis[z]=1;
                break;
            }
        }
        cout<<z<<" ";
        x=y;
        y=z;
    }
    return 0;
}


#include <bits/stdc++.h>
#define mod 1000000007
#define speedgeek ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define f(i,x,y) for(i=x;i<y;i++)
#define pb push_back
#define ll long long
#define mll map<long long,long long>
#define mcl map<char,long long>
#define vll vector<long long>
#define sll set<long long>
using namespace std;
int main(){
    speedgeek;
    ll n,i,x,y,z,j;
    mll m;ll vis[100001]={0};
    cin>>n;
    vll v[n+1];
    for(i=0;i<n-2;i++){
        cin>>x>>y>>z;
        v[x].pb(y);v[x].pb(z);m[x]++;
        v[y].pb(x);v[y].pb(z);m[y]++;
        v[z].pb(y);v[z].pb(x);m[z]++;
    }
    f(i,1,n+1){
        if(m[i]==1){
            x=i;
            if(m[v[x][0]]==2) y=v[x][0];
            else y=v[x][1];
            break;
        }
    }vis[x]=1;vis[y]=1;
    cout<<x<<" "<<y<<" ";
    f(i,0,n-2){
        for(j=0;j<v[x].size();j++){
            if(!vis[v[x][j]]){
                z=v[x][j];
                vis[z]=1;
                break;
            }
        }
        cout<<z<<" ";
        x=y;
        y=z;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
bool SortBySec(pii a, pii b) {
    return (a.second < b.second);
}
int occ[100010];
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    for (int i=0; i < n-2; i++) {
        int x,y,z;
        cin >> x >> y >> z;
        occ[x]++;
        occ[y]++;
        occ[z]++;
    }
    vector<pii> ValOccPairs;
    vector<int> ans;
    int last;
    for (int i=1; i <= n; i++) ValOccPairs.push_back(make_pair(i, occ[i]));
    sort(ValOccPairs.begin(), ValOccPairs.end(), SortBySec);
    for (int i=0; i < ValOccPairs.size(); i+=2) {
        int val = ValOccPairs[i].first;
        ans.push_back(val);
    }
    int it = (n%2)?n-2:n-1;
    for (it; it >= 0; it -= 2) {
        int val = ValOccPairs[it].first;
        ans.push_back(val);
    }
    for (int i=0; i < ans.size(); i++) cout << ans[i] << " ";
    cout << "\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
vector<int> adj[N];
bool vis[N];
int cnt[N];
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, startNode;
    cin >> n;
    for (int i=0; i < n-2; i++) {
        int x,y,z;
        cin >> x >> y >> z;
        cnt[x]++;
        cnt[y]++;
        cnt[z]++;
        adj[x].push_back(y);
        adj[x].push_back(z);
        adj[y].push_back(x);
        adj[y].push_back(z);
        adj[z].push_back(x);
        adj[z].push_back(y);
    }
    vector<int> ans(n);
    for (int i=1; i <= n; i++) {
        if (cnt[i]==1) {
            startNode = i;
            break;
        }
    }
    ans[0] = startNode;
    if (cnt[adj[startNode][0]]==2) ans[1] = adj[startNode][0];
    else ans[1] = adj[startNode][1];
    int node = ans[1];
    vis[node] = true;
    vis[startNode] = true;
    for (int i=2; i < n; i++) {
        int child;
        for (int it = 0; it < adj[startNode].size(); it++) {
            if (!vis[adj[startNode][it]]) {
                child = adj[startNode][it];
                break;
            }
        }
        vis[child] = true;
        ans[i] = child;
        startNode = node;
        node = child;
    }
    for (int i=0; i < n; i++) cout << ans[i] << " ";
    cout << "\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MAX(x,y,z) max(x, max(y,z))
int occ[100010];
bool vis[100010];
vector<int> adj[100010];
int max_occ;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,startVal=-1;
    cin >> n;
    vector<int> ans(n);
    for (int i=0; i < n-2; i++) {
        int x,y,z;
        cin >> x >> y >> z;
        adj[x].push_back(y);
        adj[x].push_back(z);
        adj[y].push_back(x);
        adj[y].push_back(z);
        adj[z].push_back(x);
        adj[z].push_back(y);
        occ[x]++;
        occ[y]++;
        occ[z]++;
        max_occ = max(max_occ, MAX(occ[x],occ[y], occ[z]));
        if (occ[x]==1) startVal = x;
        else if (occ[y]==1) startVal = y;
        else if (occ[z]==1) startVal = z;
    }
    int times_to_be_maxed = (n%2)?1:2;
    int it = 0, add = 1, count=0;
    while (it < n) {
        ans[it] = startVal;
        vis[startVal] = true;
        if (occ[startVal]==max_occ) {
            count++;
            if (count==times_to_be_maxed) add = -1;
            else add = 0;
        }
        for (int i=0; i < adj[startVal].size(); i++) {
            int child = adj[startVal][i];
            if (!vis[child] && occ[child]==occ[startVal]+add && child!=startVal) {
                startVal = child;
                break;
            }
        }
        it++;
    }
    for (int i=0; i < n; i++) cout << ans[i] << " ";
    cout << "\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MAX(x,y,z) max(x, max(y,z))
int occ[100010];
bool vis[100010];
vector<int> adj[100010];
int max_occ;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,startVal=-1;
    cin >> n;
    vector<int> ans(n);
    for (int i=0; i < n-2; i++) {
        int x,y,z;
        cin >> x >> y >> z;
        adj[x].push_back(y);
        adj[x].push_back(z);
        adj[y].push_back(x);
        adj[y].push_back(z);
        adj[z].push_back(x);
        adj[z].push_back(y);
        occ[x]++;
        occ[y]++;
        occ[z]++;
        max_occ = max(max_occ, MAX(occ[x],occ[y], occ[z]));
        if (occ[x]==1) startVal = x;
        else if (occ[y]==1) startVal = y;
        else if (occ[z]==1) startVal = z;
    }
    int times_to_be_maxed = (n%2)?1:2;
    int it = 0, add = 1, count=0;
    while (it < n) {
        ans[it] = startVal;
        vis[startVal] = true;
        if (occ[startVal]==max_occ) {
            count++;
            if (count==times_to_be_maxed) add = -1;
            else add = 0;
        }
        for (int i=0; i < adj[startVal].size(); i++) {
            int child = adj[startVal][i];
            if (!vis[child] && occ[child]==occ[startVal]+add) {
                startVal = child;
                break;
            }
        }
        it++;
    }
    for (int i=0; i < n; i++) cout << ans[i] << " ";
    cout << "\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MAX(x,y,z) max(x, max(y,z))
int occ[100010];
bool vis[100010];
vector<int> adj[100010];
int max_occ;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,startVal=-1;
    cin >> n;
    vector<int> ans(n);
    for (int i=0; i < n-2; i++) {
        int x,y,z;
        cin >> x >> y >> z;
        adj[x].push_back(y);
        adj[x].push_back(z);
        adj[y].push_back(x);
        adj[y].push_back(z);
        adj[z].push_back(x);
        adj[z].push_back(y);
        occ[x]++;
        occ[y]++;
        occ[z]++;
        max_occ = max(max_occ, MAX(occ[x],occ[y], occ[z]));
        if (occ[x]==1) startVal = x;
        else if (occ[y]==1) startVal = y;
        else if (occ[z]==1) startVal = z;
    }
    int times_to_be_maxed = 0;
    for (int i=1; i <= n; i++)
        if (occ[i]==max_occ) times_to_be_maxed++;
    int it = 0, add = 1, count=0;
    while (it < n) {
        ans[it] = startVal;
        vis[startVal] = true;
        if (occ[startVal]==max_occ) {
            count++;
            if (count==times_to_be_maxed) add = -1;
            else add = 0;
        }
        for (int i=0; i < adj[startVal].size(); i++) {
            int child = adj[startVal][i];
            if (!vis[child] && occ[child]==occ[startVal]+add) {
                startVal = child;
                break;
            }
        }
        it++;
    }
    for (int i=0; i < n; i++) cout << ans[i] << " ";
    cout << "\n";
    return 0;
}


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

#define pb push_back
#define mp make_pair
#define s second
#define f first
#define MOD 998244353
#define lp(i,n) for(int i = 0;i < n;++i)
#define sz(x) ((int)(x).size())

typedef  unsigned long long ll;
typedef pair<ll , ll> pii;
typedef  double ld;
typedef unsigned long long ull ;
static const int OO = 0x3f3f3f3f , N =2*1e5+10 ;
static const unsigned long long OOL = 0xffffffffffffffff ;

int dx[] = {1 , 1 , 2 , 2 , 3 , 3 };
int dy[] = {2 , 3 , 1 , 3 , 1 , 2 };

int n,x,arr[N][4],firstt=-1, secondd=-1,lastt=-1,before_lastt=-1,visited[N];
vector<int>answer,vec[N];

int main(){
    cin >> n;
    lp(i,n-2){
        lp(j,3)
            cin >> arr[i][j];

            lp(j,3){
                lp(k,3){
                    if(k!=j && find(vec[arr[i][j]].begin() , vec[arr[i][j]].end(),arr[i][k]) == vec[arr[i][j]].end())vec[arr[i][j]].pb(arr[i][k]);
                }
            }
    }
    lp(i,n)visited[i+1]=sz(vec[i+1]);
    lp(i,n){
        if(visited[i+1]==2 && firstt == -1){firstt=i+1;}
        else if(firstt!=-1 && visited[i+1] == 2)lastt = i+1;
        }
    lp(i,2)if(visited[vec[firstt][i]] == 3){secondd= vec[firstt][i];break;}
    lp(i,2)if(visited[vec[lastt][i]] == 3 && vec[lastt][i]!=secondd){before_lastt= vec[lastt][i];break;}
    answer.pb(firstt);
    visited[firstt]=0;visited[secondd]=0;
    queue<int>q;
   q.push(secondd);
    while(!q.empty()){
        int v = q.front();
        q.pop();
        answer.pb(v);
        lp(i,sz(vec[v])){
            if(visited[vec[v][i]]!=0 && vec[v][i]!=firstt&&vec[v][i]!=secondd&&vec[v][i]!=lastt&&vec[v][i]!=before_lastt)q.push(vec[v][i]),visited[vec[v][i]]=0;
        }

    }
answer.pb(before_lastt);
answer.pb(lastt);
    lp(i,sz(answer))cout << answer[i] << " ";

}


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

#define pb push_back
#define mp make_pair
#define s second
#define f first
#define MOD 998244353
#define lp(i,n) for(int i = 0;i < n;++i)
#define sz(x) ((int)(x).size())

typedef  unsigned long long ll;
typedef pair<ll , ll> pii;
typedef  double ld;
typedef unsigned long long ull ;
static const int OO = 0x3f3f3f3f , N =2*1e5+10 ;
static const unsigned long long OOL = 0xffffffffffffffff ;

int dx[] = {1 , 1 , 2 , 2 , 3 , 3 };
int dy[] = {2 , 3 , 1 , 3 , 1 , 2 };

int n,x,arr[N][4],firstt=-1, secondd=-1,lastt=-1,before_lastt=-1,visited[N];
vector<int>answer,vec[N];

int main(){
    cin >> n;
    lp(i,n-2){
        lp(j,3)
            cin >> arr[i][j];

            lp(j,3){
                lp(k,3){
                    if(k!=j && find(vec[arr[i][j]].begin() , vec[arr[i][j]].end(),arr[i][k]) == vec[arr[i][j]].end())vec[arr[i][j]].pb(arr[i][k]);
                }
            }
    }
    lp(i,n)visited[i+1]=sz(vec[i+1]);
    lp(i,n){
        if(visited[i+1]==2 && firstt == -1){firstt=i+1;}
        else if(firstt!=-1 && visited[i+1] == 2)lastt = i+1;
        }
    lp(i,2)if(visited[vec[firstt][i]] == 3){secondd= vec[firstt][i];break;}
    lp(i,2)if(visited[vec[lastt][i]] == 3 && vec[lastt][i]!=secondd){before_lastt= vec[lastt][i];break;}
    visited[firstt]=0;visited[secondd]=0;
    visited[lastt]=0;visited[before_lastt]=0;
    queue<int>q;
   q.push(firstt);
   q.push(secondd);
    while(!q.empty()){
        int v = q.front();
        q.pop();
        answer.pb(v);
        lp(i,sz(vec[v])){
            if(visited[vec[v][i]]!=0)q.push(vec[v][i]),visited[vec[v][i]]=0;
        }

    }
answer.pb(before_lastt);
answer.pb(lastt);
    lp(i,sz(answer))cout << answer[i] << " ";

}