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

public class ProblemF {
	static int queryCount = 0;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		FastScanner input = new FastScanner();
		int tests = input.nextInt();
		label: for (int t = 0; t < tests; t++) {
			queryCount = 0;
			boolean done = false;
			int height = input.nextInt();
			if(height == 0){
				System.exit(0);
			}
			HashSet<Integer> explored = new HashSet<Integer>();
			Node[] nodes = new Node[1 << height];
			for (int a = 0; a < 1 << height; a++) {
				nodes[a] = new Node(a);
			}
			int startPoint = (int) (Math.random() * (1 << height - 1)) + 1;
			nodes[startPoint].neighbors = executeQuery(startPoint, input);
			explored.add(startPoint);

			if (nodes[startPoint].neighbors.size() == 2) {
				completeProblem(startPoint);
				done |= true;
				if (done == true) {
					continue;
				}
			} else if (nodes[startPoint].neighbors.size() == 3) {
				// somewhere in middle of tree
				done |= explore1(nodes[startPoint].neighbors.get(0), nodes,
						explored, input);
				if (done == true) {
					continue;
				}
				done |= explore1(nodes[startPoint].neighbors.get(1), nodes,
						explored, input);
				if (done == true) {
					continue;
				}
			} else {
				done |= explore1(nodes[startPoint].neighbors.get(0), nodes,
						explored, input);
				if (done == true) {
					continue;
				}
			}
			if (done == true) {
				continue;
			}
			// process is get highest spot on tree, explore1, repeat until
			// height of tree gets within 3
			populateHeights(nodes);
			int maxHeightSoFar = getMaxHeight(nodes);
			int tallestNode = getTallestNodeId(nodes);
			while (maxHeightSoFar < height - 3) {
				done |= explore1Wrap(tallestNode, nodes, explored, input);
				if (done == true) {
					continue label;
				}
				maxHeightSoFar = getMaxHeight(nodes);
				tallestNode = getTallestNodeId(nodes);
//				System.out.println(maxHeightSoFar);
//				System.out.println(tallestNode);
//				for (int a = 0; a < 1 << height; a++) {
//					System.out.println(nodes[a].neighbors + " " + nodes[a].me + " "
//							+ nodes[a].height);
//				}
			}
			boolean asdf = dfsThreeLayers(0, tallestNode, nodes, explored, input);

		}
	}

	private static boolean dfsThreeLayers(int layer, int tallestNode, Node[] nodes, HashSet<Integer> explored, FastScanner input) {
		if(layer == 4){
			return false;
		}
		else{
			if(nodes[tallestNode].neighbors == null){
				nodes[tallestNode].neighbors = executeQuery(tallestNode, input);

				if(nodes[tallestNode].neighbors.size() == 2){
					completeProblem(tallestNode);
					return true;
				}
				for(Integer i:nodes[tallestNode].neighbors){
					boolean done = dfsThreeLayers(layer+1, i, nodes, explored, input);
					if(done == true){
						return true;
					}
				}
			}

			return false;
		}

	}

	private static int getMaxHeight(Node[] nodes) {
		int tallest = -1;
		int h = -1;
		for (int a = 0; a < nodes.length; a++) {
			if (nodes[a].height > h) {
				tallest = a;
				h = nodes[a].height;
			}
		}
		return h;
	}

	private static int getTallestNodeId(Node[] nodes) {
		int tallest = -1;
		int h = -1;
		for (int a = 0; a < nodes.length; a++) {
			if (nodes[a].height > h) {
				tallest = a;
				h = nodes[a].height;
			}
		}
		return tallest;
	}

	private static void populateHeights(Node[] nodes) {
		// TODO Auto-generated method stub
		for (int a = 0; a < nodes.length; a++) {
			nodes[a].height = -1;
		}
		for (int a = 0; a < nodes.length; a++) {
			if (nodes[a].neighbors != null && nodes[a].neighbors.size() == 1) {
				nodes[a].height = 1;
			}
		}
		for (int heights = 1; heights <= 10; heights++) {
			for (int a = 0; a < nodes.length; a++) {
				if (nodes[a].neighbors != null
						&& nodes[a].neighbors.size() != 1) {
					int neighbor = -1;
					for (Integer i : nodes[a].neighbors) {
						if (nodes[i].height == heights) {
							neighbor = nodes[i].height;
						}
					}
					if (neighbor != -1 && nodes[a].height == -1) {
						nodes[a].height = neighbor + 1;
					}
				}
			}
		}
	}
    private static boolean explore1Wrap(int integer, Node[] nodes,
			HashSet<Integer> explored, FastScanner input){
    	boolean done = false;
    	if (explored.contains(nodes[integer].neighbors.get(0))) {
			if (explored.contains(nodes[integer].neighbors.get(1))) {
				done |= explore1(nodes[integer].neighbors.get(2), nodes, explored,
						input);
			} else {
				done |= explore1(nodes[integer].neighbors.get(1), nodes, explored,
						input);
			}
		} else {
			done |= explore1(nodes[integer].neighbors.get(0), nodes, explored,
					input);
		}
    	return done;
    }
	private static boolean explore1(int integer, Node[] nodes,
			HashSet<Integer> explored, FastScanner input) {
		if (explored.contains(integer) == false) {
			nodes[integer].neighbors = executeQuery(integer, input);
			explored.add(integer);
			if (nodes[integer].neighbors.size() == 2) {
				completeProblem(integer);
				return true;
			}
			if (nodes[integer].neighbors.size() == 1) {
				return false;
			}
			if (explored.contains(nodes[integer].neighbors.get(0))) {
				if (explored.contains(nodes[integer].neighbors.get(1))) {
					explore1(nodes[integer].neighbors.get(2), nodes, explored,
							input);
				} else {
					explore1(nodes[integer].neighbors.get(1), nodes, explored,
							input);
				}
			} else {
				explore1(nodes[integer].neighbors.get(0), nodes, explored,
						input);
			}
		}
		return false;
	}

	public static void completeProblem(int answer) {
		System.out.println("! " + answer);
	}

	public static ArrayList<Integer> executeQuery(int query, FastScanner in) {
		queryCount++;
		ArrayList<Integer> ret = new ArrayList<Integer>();
		System.out.println("? " + query);
		int children = in.nextInt();
		if(children == 0){
			System.exit(0);
		}
		for (int a = 0; a < children; a++) {
			ret.add(in.nextInt());
		}
		return ret;
	}

	public static class Node {
		ArrayList<Integer> neighbors;
		int me;
		int height;

		public Node(int _me) {
			me = _me;
			height = -1;
		}
	}

	public static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		public FastScanner(Reader in) {
			br = new BufferedReader(in);
		}

		public FastScanner() {
			this(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String readNextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}

		int[] readIntArray(int n) {
			int[] a = new int[n];
			for (int idx = 0; idx < n; idx++) {
				a[idx] = nextInt();
			}
			return a;
		}

		long[] readLongArray(int n) {
			long[] a = new long[n];
			for (int idx = 0; idx < n; idx++) {
				a[idx] = nextLong();
			}
			return a;
		}
	}
}


import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.StringTokenizer;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;

public class Main{
  public static class InputReader {
    BufferedReader bReader;
    StringTokenizer tokenizer;

    public InputReader (InputStream stream){
      bReader = new BufferedReader(new InputStreamReader(stream));
    }

    public String next(){
      while(tokenizer == null || !tokenizer.hasMoreTokens()){
        try{
          tokenizer = new StringTokenizer(bReader.readLine());
        } catch(IOException ioe){
          throw new RuntimeException(ioe);
        }
      }
      return tokenizer.nextToken();
    }

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

    public long nextLong(){
      return Long.parseLong(next());
    }

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

    public String nextLine(){
      String line = "";
      try{
        while(line.equals("")){
          line = bReader.readLine();
        }
      } catch(IOException ioe){
        throw new RuntimeException(ioe);
      }
      return line;
    }
  }

  static HashMap<Integer, ArrayList<Integer> > cache;
  static int[] depth;
  static int[] visited;
  static PrintWriter pw;
  static InputReader reader;
  static ArrayList<Integer> getNeighbours(int id){
    ArrayList<Integer> n = cache.get(id);
    if(n == null){
      pw.printf("? %d\n", id);
      pw.flush();
      int k = reader.nextInt();
      n = new ArrayList<Integer>(k);
      for(int i=0;i<k;++i){
        n.add(reader.nextInt());
      }
      cache.put(id,n);
    }
    return n;
  }

  public static int doubleDfs(int u, int v, int h){
    visited[u] = visited[v] = 1;
    ArrayList<Integer> nu = getNeighbours(u);

    if(nu.size() == 1){
      depth[u] = h;
      return 0;
    }

    if(nu.size() == 2){
      depth[u] = 1;
      return u;
    }

    ArrayList<Integer> nv = getNeighbours(v);

    if(nv.size() == 1){
      depth[v] = h;
      return 0;
    }

    if(nv.size() == 2){
      depth[v] = 1;
      return v;
    }

    int candU = 0;
    int candV = 0;
//     for(int i=1;i<1<<h;++i){
//       pw.printf("%d %d\n", i,visited[i]);
//     }
//     pw.println(nu);
//     pw.println(nv);
    for(int i=0;i<3;++i){
      if(candU == 0 && visited[nu.get(i)] == 0){
        candU = nu.get(i);
      }
      if(candV == 0 && visited[nv.get(i)] == 0){
        candV = nv.get(i);
      }
    }

    int ret = doubleDfs(candU, candV, h);
    if(ret != 0){
      return ret;
    } else {
      if(depth[candU]!=0){
        depth[u] = depth[candU]-1;
      }
      if(depth[candV]!=0){
        depth[v] = depth[candV]-1;
      }
    }
    return 0;
  }

  public static void solve(){
    cache = new HashMap<Integer, ArrayList<Integer> >();
    int h = reader.nextInt();
    depth = new int[1<<h];
    visited = new int[1<<h];
    int curr = 1;
    ArrayList<Integer> ne = getNeighbours(curr);
    if(ne.size() == 2){
      pw.printf("! %d\n", curr);
      return;
    }
    if(ne.size() == 1){
      depth[curr] = h;
      curr = ne.get(0);
      depth[curr] = h-1;
    } else {
      visited[curr] = 1;
      int ret;
      if((ret = doubleDfs(ne.get(0), ne.get(1), h))!= 0){
        pw.printf("! %d\n", ret);
        pw.flush();
        return;
      }
      if(depth[ne.get(0)] != 0){
        depth[curr] = depth[ne.get(0)]-1;
      }
      if(depth[ne.get(1)] != 0){
        depth[curr] = depth[ne.get(1)]-1;
      }

    }
    while(depth[curr] >  3){
      int one = 0;
      int other = 0;
      ne = getNeighbours(curr);
      for(int i=0;i<ne.size();++i){
        if(depth[ne.get(i)] == 0){
          one = ne.get(i);
          break;
        }
      }
      for(int i=ne.size()-1;i>=0;i--){
        if(depth[ne.get(i)] == 0){
          other = ne.get(i);
          break;
        }
      }
      visited = new int[1<<h];
      int ret;
      visited[curr] = 1;
      if((ret = doubleDfs(one, other, h))!= 0){
        pw.printf("! %d\n", ret);
        pw.flush();
        return;
      }
      for(int i=0;i<ne.size();++i){
        if(depth[ne.get(i)] == 0){
          depth[ne.get(i)] = depth[curr]-1;
          curr = ne.get(i);
          break;
        }
      }
    }
    LinkedList<Integer> q = new LinkedList<Integer> ();
    q.add(curr);
    visited = depth;
    int moves = 6;
    while(moves-->0){
      int c = q.poll();
      visited[c] = 1;
      ne = getNeighbours(c);
      if(ne.size() == 2){
        pw.printf("! %d\n", c);
        pw.flush();
        return;
      }
      for(Integer n : ne){
        if(visited[n] == 0){
          q.add(n);
        }
      }
    }
    pw.printf("! %d\n", q.poll());
    pw.flush();
    return;
  }

  public static void main(String[] args){
    reader = new InputReader(System.in);
    pw = new PrintWriter(System.out);
    int t = reader.nextInt();
    while(t-->0){
      solve();
    }
    pw.flush();
    pw.close();
  }

}


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.lang.*;

public class P750F{
	static long mod=1000000007;
	static InputReader in = new InputReader(System.in);
	static ArrayList<Integer>ar1=new ArrayList<Integer>();
	static ArrayList<Integer>ar2=new ArrayList<Integer>();
	static ArrayList<Integer>ar[]=new ArrayList[200];
	static HashSet<Integer>hs=new HashSet<Integer>();
	static int ans=-1;
	static int count=0;
	static void swap()
	{
			ArrayList<Integer>t1=new ArrayList<Integer>();
			ArrayList<Integer>t2=new ArrayList<Integer>();
			for(int cc:ar1)
			{
				t1.add(cc);
			}
			for(int cc:ar2)
			{
				t2.add(cc);
			}
			ar1=new ArrayList<Integer>();
			ar2=new ArrayList<Integer>();
			for(int cc:t1)
			{
				ar2.add(cc);
			}
			for(int cc:t2)
			{
				ar1.add(cc);
			}
	}
	static void l1()
	{
		l2();
	}
	static void l2()
	{
	 int y=in.readInt();
   	 System.out.println("? "+y);
   	 hs.add(y);
   	 int z=in.readInt();
   	if(z==2)
   	{
   		int bb1=in.readInt();
   		int bb2=in.readInt();
   		System.out.println("! "+y);
   	}
   	else
   	{

   		int x1=in.readInt();
   		int x2=in.readInt();
   		int x3=in.readInt();
   		ar[y].add(x1);
   		ar[y].add(x2);
   		ar[y].add(x3);
   		int f=0;
   		if(!hs.contains(x1))
   		{
   			f=1;
   			dfs1(x1);

   		}
   		if(!hs.contains(x2)&&ans==-1)
   		{
   			if(f==0)
   				dfs1(x2);
   			else
   				dfs2(x2);
   		}
   		if(!hs.contains(x3)&&ans==-1)
   		{
   			dfs2(x3);
   		}
   		if(ans!=-1)
   		{
   			System.out.println("! "+ans);
   		}
   		else
   		{
   			int s1=ar1.size();
   			int s2=ar2.size();
   			if(s1<s2)
   			{
   				swap();
   			}
   			s1=ar1.size();
   			s2=ar2.size();
   				int d=s1-s2;
   				d/=2;
   				int l=s2+d;
   				l++;
   				if(l>=4)
   				{
   					if(l==4)
   					{
   						l4(0);
   					}
   					else if(l==5)
   					{
   						l5(0);
   					}
   					else
   					{
   						l6(0);
   					}
   				}
   				else
   				{
   					l3(0);
   				}

   		}

   	}
	}
	static void l3(int u)
	{
		int x1,x2,x3,z,f;
		int ele=ar1.get((0+u));
		ar1=new ArrayList<Integer>();
		ar2=new ArrayList<Integer>();
		int cur=-1;
		for(int j1=0;j1<ar[ele].size();j1++)
		{
			int p=ar[ele].get(j1);
			if(!hs.contains(p))
			{
				cur=p;
			}
		}
		hs.add(cur);
		System.out.println("? "+cur);
    	 z=in.readInt();
    	if(z==2)
    	{
    		int bb1=in.readInt();
       		int bb2=in.readInt();
    		System.out.println("! "+cur);
    	}
    	else
    	{
    		x1=in.readInt();
    		x2=in.readInt();
    		x3=in.readInt();
    		ar[cur].add(x1);
    		ar[cur].add(x2);
    		ar[cur].add(x3);

		f=0;
		if(!hs.contains(x1))
		{
			f=x1;
			dfs1(x1);
		}
		if(!hs.contains(x2))
		{
			if(f==0)
				{
					f=x2;
					dfs1(x2);
				}
		}
		if(ans!=-1)
		{
			System.out.println("! "+ans);
		}
		else
		{
		if(ar1.size()==3)
		{
			int curr=-1;
			for(int j1=0;j1<ar[cur].size();j1++)
			{
				int p=ar[cur].get(j1);
				if(!hs.contains(p))
				{
					curr=p;
				}
			}
			hs.add(curr);
			System.out.println("? "+curr);
       	 z=in.readInt();
       	if(z==2)
       	{
       		int bb1=in.readInt();
       		int bb2=in.readInt();
       		System.out.println("! "+curr);
       	}
       	else
       	{
       		x1=in.readInt();
    		x2=in.readInt();
    		x3=in.readInt();
       		ar[curr].add(x1);
    		ar[curr].add(x2);
    		ar[curr].add(x3);
    		f=0;
    		if(!hs.contains(x1))
    		{
    			f=1;
    			System.out.println("? "+x1);
    			hs.add(x1);
    			 z=in.readInt();
   		        	if(z==2)
   		        	{
   		        		int bb1=in.readInt();
   		        		int bb2=in.readInt();

   		        		ans=x1;
   		        		System.out.println("! "+x1);
   		        	}
   		        	else
   		        	{
   		        		int xx1=in.readInt();
		        		int xx2=in.readInt();
		        		int xx3=in.readInt();
		        		if(!hs.contains(xx1))
		        		{
		        			System.out.println("? "+xx1);
		        			hs.add(xx1);
		        			 z=in.readInt();
	           		        	if(z==2)
	           		        	{
	           		        		int bb1=in.readInt();
	           		        		int bb2=in.readInt();
	           		        		ans=xx1;
	           		        		System.out.println("! "+xx1);
	           		        	}
	           		        	else if(z==1)
	           		        	{
	           		        		int bb1=in.readInt();
	           		        	}
	           		        	else
	           		        	{
	           		        		//int bb1=in.readInt();
	           		        		//int bb2=in.readInt();
	           		        		//int bb3=in.readInt();
	           		        	}
		        		}
		        		if(!hs.contains(xx2)&&ans==-1)
		        		{

		        			System.out.println("? "+xx2);
		        			hs.add(xx2);
		        			 z=in.readInt();
	           		        	if(z==2)
	           		        	{
	           		        		int bb1=in.readInt();
	           		        		int bb2=in.readInt();
	           		        		ans=xx2;
	           		        		System.out.println("! "+xx2);
	           		        	}
	           		        	else if(z==1)
	           		        	{
	           		        		int bb1=in.readInt();
	           		        	}
	           		        	else
	           		        	{
	           		        		//int bb1=in.readInt();
	           		        		//int bb2=in.readInt();
	           		        		//int bb3=in.readInt();
	           		        	}
		        		}
		        		if(!hs.contains(xx3)&&ans==-1)
		        		{

		        			System.out.println("? "+xx3);
		        			hs.add(xx3);
		        			 z=in.readInt();
	           		        	if(z==2)
	           		        	{
	           		        		int bb1=in.readInt();
	           		        		int bb2=in.readInt();
	           		        		ans=xx3;
	           		        		System.out.println("! "+xx3);
	           		        	}
	           		        	else if(z==1)
	           		        	{
	           		        		int bb1=in.readInt();
	           		        	}
	           		        	else
	           		        	{
	           		        		//int bb1=in.readInt();
	           		        		//int bb2=in.readInt();
	           		        		//int bb3=in.readInt();
	           		        	}
		        		}
   		        	}
    		}
    		if(!hs.contains(x2)&&ans==-1)
    		{
    			if(f==0)
    			{
    				f=1;
        			System.out.println("? "+x2);
        			hs.add(x2);
        			 z=in.readInt();
       		        	if(z==2)
       		        	{
       		        		int bb1=in.readInt();
       		        		int bb2=in.readInt();
       		        		ans=x2;
       		        		System.out.println("! "+x2);
       		        	}
       		        	else
       		        	{
       		        		int xx1=in.readInt();
    		        		int xx2=in.readInt();
    		        		int xx3=in.readInt();
    		        		if(!hs.contains(xx1))
    		        		{
    		        			System.out.println("? "+xx1);
    		        			hs.add(xx1);
    		        			 z=in.readInt();
    	           		        	if(z==2)
    	           		        	{
    	           		        		int bb1=in.readInt();
    	           		        		int bb2=in.readInt();
    	           		        		ans=xx1;
    	           		        		System.out.println("! "+xx1);
    	           		        	}
    	           		        	else if(z==1)
    	           		        	{
    	           		        		int bb1=in.readInt();
    	           		        	}
    	           		        	else
    	           		        	{
    	           		        		//int bb1=in.readInt();
    	           		        		//int bb2=in.readInt();
    	           		        		//int bb3=in.readInt();
    	           		        	}
    		        		}
    		        		if(!hs.contains(xx2)&&ans==-1)
    		        		{

    		        			System.out.println("? "+xx2);
    		        			hs.add(xx2);
    		        			 z=in.readInt();
    	           		        	if(z==2)
    	           		        	{
    	           		        		int bb1=in.readInt();
    	           		        		int bb2=in.readInt();
    	           		        		ans=xx2;
    	           		        		System.out.println("! "+xx2);
    	           		        	}
    	           		        	else if(z==1)
    	           		        	{
    	           		        		int bb1=in.readInt();
    	           		        	}
    	           		        	else
    	           		        	{
    	           		        		//int bb1=in.readInt();
    	           		        		//int bb2=in.readInt();
    	           		        		//int bb3=in.readInt();
    	           		        	}
    		        		}
    		        		if(!hs.contains(xx3)&&ans==-1)
    		        		{

    		        			System.out.println("? "+xx3);
    		        			hs.add(xx3);
    		        			 z=in.readInt();
    	           		        	if(z==2)
    	           		        	{
    	           		        		int bb1=in.readInt();
    	           		        		int bb2=in.readInt();
    	           		        		ans=xx3;
    	           		        		System.out.println("! "+xx3);
    	           		        	}
    	           		        	else if(z==1)
    	           		        	{
    	           		        		int bb1=in.readInt();
    	           		        	}
    	           		        	else
    	           		        	{
    	           		        		//int bb1=in.readInt();
    	           		        		//int bb2=in.readInt();
    	           		        		//int bb3=in.readInt();
    	           		        	}
    		        		}
       		        	}
    			}
    			else
    			{
    				f=1;
        			System.out.println("? "+x2);
        			hs.add(x2);
        			 z=in.readInt();
       		        	if(z==2)
       		        	{
       		        		int bb1=in.readInt();
       		        		int bb2=in.readInt();
       		        		ans=x2;
       		        		System.out.println("! "+x2);
       		        	}
       		        	else
       		        	{
       		        		int xx1=in.readInt();
    		        		int xx2=in.readInt();
    		        		int xx3=in.readInt();
    		        		int g=0;
    		        		if(!hs.contains(xx1))
    		        		{
    		        			g=1;
    		        			System.out.println("? "+xx1);
    		        			hs.add(xx1);
    		        			 z=in.readInt();
    	           		        	if(z==2)
    	           		        	{
    	           		        		int bb1=in.readInt();
    	           		        		int bb2=in.readInt();
    	           		        		ans=xx1;
    	           		        		System.out.println("! "+xx1);
    	           		        	}
    	           		        	else if(z==1)
    	           		        	{
    	           		        		int bb1=in.readInt();
    	           		        	}
    	           		        	else
    	           		        	{
    	           		        		//int bb1=in.readInt();
    	           		        		//int bb2=in.readInt();
    	           		        		//int bb3=in.readInt();
    	           		        	}
    		        		}
    		        		if(!hs.contains(xx2)&&ans==-1)
    		        		{
    		        			if(g==0)
    		        			{
    		        				g=1;
    		        			System.out.println("? "+xx2);
    		        			hs.add(xx2);
    		        			 z=in.readInt();
    	           		        	if(z==2)
    	           		        	{
    	           		        		int bb1=in.readInt();
    	           		        		int bb2=in.readInt();
    	           		        		ans=xx2;
    	           		        		System.out.println("! "+xx2);
    	           		        	}
    	           		        	else if(z==1)
    	           		        	{
    	           		        		int bb1=in.readInt();
    	           		        	}
    	           		        	else
    	           		        	{
    	           		        		//int bb1=in.readInt();
    	           		        		//int bb2=in.readInt();
    	           		        		//int bb3=in.readInt();
    	           		        	}
    		        			}
    		        			else
    		        			{
    		        				ans=xx2;
    		        				System.out.println("! "+xx2);
    		        			}
    		        		}
    		        		if(!hs.contains(xx3)&&ans==-1)
    		        		{

    	           		        		ans=xx3;
    	           		        		System.out.println("! "+xx3);
    		        		}
       		        	}
    			}
    		}
    		if(!hs.contains(x3)&&ans==-1)
    		{
    			if(f==0)
    			{
    				f=1;
        			System.out.println("? "+x3);
        			hs.add(x3);
        			 z=in.readInt();
       		        	if(z==2)
       		        	{
       		        		int bb1=in.readInt();
       		        		int bb2=in.readInt();
       		        		ans=x3;
       		        		System.out.println("! "+x3);
       		        	}
       		        	else
       		        	{
       		        		int xx1=in.readInt();
    		        		int xx2=in.readInt();
    		        		int xx3=in.readInt();
    		        		if(!hs.contains(xx1))
    		        		{
    		        			System.out.println("? "+xx1);
    		        			hs.add(xx1);
    		        			 z=in.readInt();
    	           		        	if(z==2)
    	           		        	{
    	           		        		int bb1=in.readInt();
    	           		        		int bb2=in.readInt();
    	           		        		ans=xx1;
    	           		        		System.out.println("! "+xx1);
    	           		        	}
    	           		        	else if(z==1)
    	           		        	{
    	           		        		int bb1=in.readInt();
    	           		        	}
    	           		        	else
    	           		        	{
    	           		        		//int bb1=in.readInt();
    	           		        		//int bb2=in.readInt();
    	           		        		///int bb3=in.readInt();
    	           		        	}
    		        		}
    		        		if(!hs.contains(xx2)&&ans==-1)
    		        		{

    		        			System.out.println("? "+xx2);
    		        			hs.add(xx2);
    		        			 z=in.readInt();
    	           		        	if(z==2)
    	           		        	{
    	           		        		int bb1=in.readInt();
    	           		        		int bb2=in.readInt();
    	           		        		ans=xx2;
    	           		        		System.out.println("! "+xx2);
    	           		        	}
    	           		        	else if(z==1)
    	           		        	{
    	           		        		int bb1=in.readInt();
    	           		        	}
    	           		        	else
    	           		        	{
    	           		        		//int bb1=in.readInt();
    	           		        		//int bb2=in.readInt();
    	           		        		//int bb3=in.readInt();
    	           		        	}
    		        		}
    		        		if(!hs.contains(xx3)&&ans==-1)
    		        		{

    		        			System.out.println("? "+xx3);
    		        			hs.add(xx3);
    		        			 z=in.readInt();
    	           		        	if(z==2)
    	           		        	{
    	           		        		int bb1=in.readInt();
    	           		        		int bb2=in.readInt();
    	           		        		ans=xx3;
    	           		        		System.out.println("! "+xx3);
    	           		        	}
    	           		        	else if(z==1)
    	           		        	{
    	           		        		int bb1=in.readInt();
    	           		        	}
    	           		        	else
    	           		        	{
    	           		        		//int bb1=in.readInt();
    	           		        		//int bb2=in.readInt();
    	           		        		//int bb3=in.readInt();
    	           		        	}
    		        		}
       		        	}
    			}
    			else
    			{
    				f=1;
        			System.out.println("? "+x3);
        			hs.add(x3);
        			 z=in.readInt();
       		        	if(z==2)
       		        	{
       		        		int bb1=in.readInt();
       		        		int bb2=in.readInt();
       		        		ans=x3;
       		        		System.out.println("! "+x3);
       		        	}
       		        	else
       		        	{
       		        		int xx1=in.readInt();
    		        		int xx2=in.readInt();
    		        		int xx3=in.readInt();
    		        		int g=0;
    		        		if(!hs.contains(xx1))
    		        		{
    		        			g=1;
    		        			System.out.println("? "+xx1);
    		        			hs.add(xx1);
    		        			 z=in.readInt();
    	           		        	if(z==2)
    	           		        	{
    	           		        		int bb1=in.readInt();
    	           		        		int bb2=in.readInt();
    	           		        		ans=xx1;
    	           		        		System.out.println("! "+xx1);
    	           		        	}
    	           		        	else if(z==1)
    	           		        	{
    	           		        		int bb1=in.readInt();
    	           		        	}
    	           		        	else
    	           		        	{
    	           		        		//int bb1=in.readInt();
    	           		        		//int bb2=in.readInt();
    	           		        		//int bb3=in.readInt();
    	           		        	}
    		        		}
    		        		if(!hs.contains(xx2)&&ans==-1)
    		        		{
    		        			if(g==0)
    		        			{
    		        				g=1;
    		        			System.out.println("? "+xx2);
    		        			hs.add(xx2);
    		        			 z=in.readInt();
    	           		        	if(z==2)
    	           		        	{
    	           		        		int bb1=in.readInt();
    	           		        		int bb2=in.readInt();
    	           		        		ans=xx2;
    	           		        		System.out.println("! "+xx2);
    	           		        	}
    	           		        	else if(z==1)
    	           		        	{
    	           		        		int bb1=in.readInt();
    	           		        	}
    	           		        	else
    	           		        	{
    	           		        		//int bb1=in.readInt();
    	           		        		//int bb2=in.readInt();
    	           		        		//int bb3=in.readInt();
    	           		        	}
    		        			}
    		        			else
    		        			{
    		        				ans=xx2;
    		        				System.out.println("! "+xx2);
    		        			}
    		        		}
    		        		if(!hs.contains(xx3)&&ans==-1)
    		        		{

    	           		        		ans=xx3;
    	           		        		System.out.println("! "+xx3);
    		        		}
       		        	}
    			}
    		}
    		f=0;

       	}
		}
		else
		{
			if(ar1.size()==5)
			{
				ele=ar1.get(0);
				hs.add(ele);
				cur=-1;
				for(int zz:ar[ele])
				{
					if(!hs.contains(zz))
					{
						cur=zz;
						break;
					}
				}
				System.out.println("? "+cur);
				hs.add(cur);
				z=in.readInt();
				if(z==2)
				{
					int bb1=in.readInt();
			   		int bb2=in.readInt();
					System.out.println("! "+cur);
				}

				else
				{
					x1=in.readInt();
					x2=in.readInt();
					x3=in.readInt();
					if(!hs.contains(x1)&&ans==-1)
					{
						System.out.println("? "+x1);
        				hs.add(x1);
        				z=in.readInt();
        				if(z==2)
        				{
        					int bb1=in.readInt();
        			   		int bb2=in.readInt();
        					ans=x1;
        					System.out.println("! "+x1);
        				}
        				else if(z==1)
       		        	{
       		        		int bb1=in.readInt();
       		        	}
        				else
       		        	{
       		        		//int bb1=in.readInt();
       		        		//int bb2=in.readInt();
       		        		//int bb3=in.readInt();
       		        	}
					}
					if(!hs.contains(x2)&&ans==-1)
					{
						System.out.println("? "+x2);
        				hs.add(x2);
        				z=in.readInt();
        				if(z==2)
        				{
        					int bb1=in.readInt();
        			   		int bb2=in.readInt();
        					ans=x2;
        					System.out.println("! "+x2);
        				}
        				else if(z==1)
       		        	{
       		        		int bb1=in.readInt();
       		        	}
        				else
       		        	{
       		        		//int bb1=in.readInt();
       		        		//int bb2=in.readInt();
       		        		//int bb3=in.readInt();
       		        	}
					}
					if(!hs.contains(x3)&&ans==-1)
					{
						System.out.println("? "+x3);
        				hs.add(x3);
        				z=in.readInt();
        				if(z==2)
        				{
        					int bb1=in.readInt();
        			   		int bb2=in.readInt();
        					ans=x3;
        					System.out.println("! "+x3);
        				}
        				else if(z==1)
       		        	{
       		        		int bb1=in.readInt();
       		        	}
        				else
       		        	{
       		        		//int bb1=in.readInt();
       		        		//int bb2=in.readInt();
       		        		//int bb3=in.readInt();
       		        	}
					}
				}
			}
			else
			{
				ele=ar1.get(1);
				hs.add(ele);
				for(int zz:ar[ele])
				{
					if(!hs.contains(zz))
					{
						ans=zz;
						System.out.println("! "+zz);
						break;
					}
				}
			}
		}
		}
	}
	}
	static void l4(int u)
	{
		int x1,x2,x3,z,f;
		int ele=ar1.get((1+u));
		hs.add(ele);
		int cur=-1;
		for(int j1=0;j1<ar[ele].size();j1++)
		{
			int p=ar[ele].get(j1);
			if(!hs.contains(p))
			{
				cur=p;
			}
		}
		hs.add(cur);
		System.out.println("? "+cur);
    	 z=in.readInt();
    	if(z==2)
    	{
    		int bb1=in.readInt();
       		int bb2=in.readInt();
    		System.out.println("! "+cur);
    	}
    	else
    	{
    		x1=in.readInt();
			x2=in.readInt();
			x3=in.readInt();
			ar[cur].add(x1);
			ar[cur].add(x2);
			ar[cur].add(x3);
			ar1=new ArrayList<Integer>();
			ar2=new ArrayList<Integer>();
			f=0;
			if(ans==-1&&!hs.contains(x1))
			{
				f=1;
				dfs1(x1);
			}
			if(ans==-1&&!hs.contains(x2)&&f==0)
			{
				f=1;
				dfs1(x2);
			}
			if(ans==-1&&!hs.contains(x3)&&f==0)
			{
				f=1;
				dfs1(x3);
			}
			if(ans!=-1)
			{
				System.out.println("! "+ans);

			}
			else
			{
				int s=ar1.size();
				int curr=-1;
				if(ar1.size()==4)
				{
					for(int j=0;j<ar[cur].size();j++)
					{
						int zz=ar[cur].get(j);
						if(!hs.contains(zz))
						{
							curr=zz;
						}
					}

				hs.add(curr);
				System.out.println("? "+curr);
				z=in.readInt();
				if(z==2)
				{
					int bb1=in.readInt();
			   		int bb2=in.readInt();
					System.out.println("! "+curr);
				}
				else
				{
				x1=in.readInt();
				x2=in.readInt();
				x3=in.readInt();
				ar[curr].add(x1);
				ar[curr].add(x2);
				ar[curr].add(x3);
				if(ans==-1&&!hs.contains(x1))
				{
					System.out.println("? "+x1);
					z=in.readInt();
					if(z==2)
					{
						int bb1=in.readInt();
				   		int bb2=in.readInt();
						ans=x1;
						System.out.println("! "+x1);
					}
					else if(z==1)
   		        	{
   		        		int bb1=in.readInt();
   		        	}
					else
   		        	{
   		        		//int bb1=in.readInt();
   		        		//int bb2=in.readInt();
   		        		//int bb3=in.readInt();
   		        	}
				}
				if(ans==-1&&!hs.contains(x2))
				{
					System.out.println("? "+x2);
					z=in.readInt();
					if(z==2)
					{
						int bb1=in.readInt();
				   		int bb2=in.readInt();
						ans=x2;
						System.out.println("! "+x2);
					}
					else if(z==1)
   		        	{
   		        		int bb1=in.readInt();
   		        	}
					else
   		        	{
   		        		//int bb1=in.readInt();
   		        		//int bb2=in.readInt();
   		        		//int bb3=in.readInt();
   		        	}
				}
				if(ans==-1&&!hs.contains(x3))
				{
					System.out.println("? "+x3);
					z=in.readInt();
					if(z==2)
					{
						int bb1=in.readInt();
				   		int bb2=in.readInt();
						ans=x3;
						System.out.println("! "+x3);
					}
					else if(z==1)
   		        	{
   		        		int bb1=in.readInt();
   		        	}
					else
   		        	{
   		        		//int bb1=in.readInt();
   		        		//int bb2=in.readInt();
   		        		//int bb3=in.readInt();
   		        	}
				}
				}
				}
				else
				{
					ele=ar1.get(0);
					hs.add(ele);
					for(int xx:ar[ele])
					{
						if(!hs.contains(xx))
						{
							ans=xx;
							System.out.println("! "+xx);
							break;
						}
					}
				}
			}
    	}
	}
	static void l5(int u)
	{
		int x1,x2,x3,z,f;
		int ele=ar1.get((2+u));
		hs.add(ele);
		int cur=-1;
		for(int zz:ar[ele])
		{
			if(!hs.contains(zz))
			{
				cur=zz;
				break;
			}
		}
		System.out.println("? "+cur);
		hs.add(cur);
		z=in.readInt();
		if(z==2)
		{
			int bb1=in.readInt();
	   		int bb2=in.readInt();
			System.out.println("! "+cur);
		}
		else
		{
			x1=in.readInt();
			x2=in.readInt();
			x3=in.readInt();
			if(!hs.contains(x1)&&ans==-1)
			{
				System.out.println("? "+x1);
				hs.add(x1);
				z=in.readInt();
				if(z==2)
				{
					int bb1=in.readInt();
			   		int bb2=in.readInt();
					ans=x1;
					System.out.println("! "+x1);
				}
				else if(z==1)
		        	{
		        		int bb1=in.readInt();
		        	}
				else
		        	{
		        		int bb1=in.readInt();
		        		int bb2=in.readInt();
		        		int bb3=in.readInt();
		        	}
			}
			if(!hs.contains(x2)&&ans==-1)
			{
				System.out.println("? "+x2);
				hs.add(x2);
				z=in.readInt();
				if(z==2)
				{
					int bb1=in.readInt();
			   		int bb2=in.readInt();
					ans=x2;
					System.out.println("! "+x2);
				}
				else if(z==1)
		        	{
		        		int bb1=in.readInt();
		        	}
				else
		        	{
		        		int bb1=in.readInt();
		        		int bb2=in.readInt();
		        		int bb3=in.readInt();
		        	}
			}
			if(!hs.contains(x3)&&ans==-1)
			{
				System.out.println("? "+x3);
				hs.add(x3);
				z=in.readInt();
				if(z==2)
				{
					int bb1=in.readInt();
			   		int bb2=in.readInt();
					ans=x3;
					System.out.println("! "+x3);
				}
				else if(z==1)
		        	{
		        		int bb1=in.readInt();
		        	}
				else
		        	{
		        		int bb1=in.readInt();
		        		int bb2=in.readInt();
		        		int bb3=in.readInt();
		        	}
			}
		}
	}
	static void l6(int u)
	{
		int x1,x2,x3,z,f;
		int ele=ar1.get((3+u));
		hs.add(ele);
		for(int zz:ar[ele])
		{
			if(!hs.contains(zz))
			{
				System.out.println("! "+zz);
			}
		}
	}
	static boolean dfs1(int i)
	{
		 hs.add(i);
		 ar1.add(i);
		 System.out.println("? "+i);
		 boolean f=false;
		 int k=in.readInt();
		 if(k==1)
		 {
			 int bb1=in.readInt();
			 return true;
		 }
		 else if(k==2)
		 {
			 ans=i;
			 int bb1=in.readInt();
		   	 int bb2=in.readInt();
			 return true;
		 }
		 else
		 {
			 int x1=in.readInt();
     		 int x2=in.readInt();
     		 int x3=in.readInt();
     		 ar[i].add(x1);
     		 ar[i].add(x2);
     		 ar[i].add(x3);
     		 if(!hs.contains(x1))
     		 {
     			 dfs1(x1);
     		 }
     		 else if(!hs.contains(x2))
     		 {
     			 dfs1(x2);
     		 }
     		 else
     		 {
     			 dfs1(x3);
     		 }

		 }
		 return f;
	}
	static boolean dfs2(int i)
	{
		 hs.add(i);
		 ar2.add(i);
		 System.out.println("? "+i);
		 boolean f=false;
		 int k=in.readInt();
		 if(k==1)
		 {
			 int bb1=in.readInt();
			 return true;
		 }
		 else if(k==2)
		 {
			 int bb1=in.readInt();
		   	 int bb2=in.readInt();
			 ans=i;
			 return true;
		 }
		 else
		 {
			 int x1=in.readInt();
     		 int x2=in.readInt();
     		 int x3=in.readInt();
     		 ar[i].add(x1);
     		 ar[i].add(x2);
     		 ar[i].add(x3);
     		 if(!hs.contains(x1))
     		 {
     			 dfs2(x1);
     		 }
     		 else if(!hs.contains(x2))
     		 {
     			 dfs2(x2);
     		 }
     		 else
     		 {
     			 dfs2(x3);
     		 }

		 }
		 return f;
	}
    public static void main(String[] args) throws Exception{

    	PrintWriter pw=new PrintWriter(System.out);

    	int t=in.readInt();
        while(t-->0)
        {
        count=0;
    	ans=-1;
        int h=in.readInt();
        for(int i=0;i<200;i++)
        	ar[i]=new ArrayList<Integer>();
        hs=new HashSet<Integer>();
        ar1=new ArrayList<Integer>();
        ar2=new ArrayList<Integer>();
        System.out.println("? 1");
        count++;
        int k=in.readInt();
        hs.add(1);
        if(k==2)
        {
        	int bb1=in.readInt();
       		int bb2=in.readInt();
        	System.out.println("! 1");
        }
        else if(k==1)
        {
        	l1();
        }
        else
        {
        	int y=1;
        	int x1=in.readInt();
        	int x2=in.readInt();
        	int x3=in.readInt();
        	ar[1].add(x1);
        	ar[1].add(x2);
        	ar[1].add(x3);
        	hs.add(1);
        	dfs1(x1);
        	dfs2(x2);
        	int s1=ar1.size();
        	int s2=ar2.size();
        	if(ans!=-1)
        	{
        		System.out.println("! "+ans);
        	}
        	else
        	{
        		if(s1==s2)
        		{
        			ar1=new ArrayList<Integer>();
        			ar2=new ArrayList<Integer>();
        			int l=s1+1;
        			if(l==2)
        			{
        				int cur=x3;
        				for(int xx:ar[y])
        				{
        					if(!hs.contains(xx))
        					{
        						cur=xx;
        						break;
        					}
        				}
        				dfs1(cur);
        				if(ans!=-1)
        				{
        					System.out.println("! "+ans);
        				}
        				else
        				{
        					if(ar1.size()==3)
        					{
        						l3(1);
        					}
        					else if(ar1.size()==5)
        					{
        						l4(1);
        					}
        					else if(ar1.size()==7)
        					{
        						l5(1);
        					}
        					else if(ar1.size()==9)
        					{
        						l6(1);
        					}

        				}
        			}

        			else if(l==3)
        			{
        				int cur=x3;
        				for(int xx:ar[y])
        				{
        					if(!hs.contains(xx))
        					{
        						cur=xx;
        						break;
        					}
        				}
        				dfs1(cur);
        				if(ans!=-1)
        				{
        					System.out.println("! "+ans);
        				}
        				else
        				{
        					if(ar1.size()==4)
        					{
        						l4(-1);
        					}
        					else if(ar1.size()==6)
        					{
        						l5(-1);
        					}
        					else if(ar1.size()==8)
        					{
        						l6(-1);
        					}
        				}
        			}
        			else if(l==4)
        			{
        				int cur=x3;
        				for(int xx:ar[y])
        				{
        					if(!hs.contains(xx))
        					{
        						cur=xx;
        						break;
        					}
        				}
        				dfs1(cur);
        				if(ans!=-1)
        				{
        					System.out.println("! "+ans);
        				}
        				else
        				{
        					if(ar1.size()==5)
        					{
        						l5(-2);
        					}
        					else if(ar1.size()==7)
        					{
        						l6(-2);
        					}

        				}
        			}
        			else if(l==5)
        			{
        				int cur=x3;
        				for(int xx:ar[y])
        				{
        					if(!hs.contains(xx))
        					{
        						cur=xx;
        						break;
        					}
        				}
        				dfs1(cur);
        				if(ans!=-1)
        				{
        					System.out.println("! "+ans);
        				}
        				else
        				{
        					for(int xx:ar[cur])
            				{
            					if(!hs.contains(xx))
            					{
            						cur=xx;
            						ans=xx;
            						int bb1=in.readInt();
	           		        		int bb2=in.readInt();
	           		        		System.out.println("! "+ans);
            						break;
            					}
            				}
        				}
        			}
        			else if(l==6)
        			{
        				System.out.println("! "+x3);
        			}
        		}
        		else
        		{
        			if(s1<s2)
        			{
        				swap();
        			}
        			s1=ar1.size();
        			s2=ar2.size();
        			int l=s2+1;
        			if(l==2)
        			{
        				if(ar1.size()==3)
    					{
    						l3(0);
    					}
    					else if(ar1.size()==5)
    					{
    						l4(0);
    					}
    					else if(ar1.size()==7)
    					{
    						l5(0);
    					}
    					else if(ar1.size()==9)
    					{
    						l6(0);
    					}
        			}
        			else if(l==3)
        			{
        				if(ar1.size()==4)
					{
						l4(-1);
					}
					else if(ar1.size()==6)
					{
						l5(-1);
					}
					else if(ar1.size()==8)
					{
						l6(-1);
					}
        			}
        			else if(l==4)
        			{
        				if(ar1.size()==5)
    					{
    						l5(-2);
    					}
    					else if(ar1.size()==7)
    					{
    						l6(-2);
    					}
        			}
        			else if(l==5)
        			{
        				l6(-3);
        			}
        		}

        	}
        }

    	}

    	pw.close();
    }

static  class InputReader
{
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;
    private SpaceCharFilter filter;

    public InputReader(InputStream stream)
    {
        this.stream = stream;
    }

    public int read()
    {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars)
        {
            curChar = 0;
            try
            {
                numChars = stream.read(buf);
            } catch (IOException e)
            {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

    public int readInt()
    {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-')
        {
            sgn = -1;
            c = read();
        }
        int res = 0;
        do
        {
            if (c < '0' || c > '9')
                throw new InputMismatchException();
            res *= 10;
            res += c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }

    public String readString()
    {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do
        {
            res.appendCodePoint(c);
            c = read();
        } while (!isSpaceChar(c));
        return res.toString();
    }
    public String readLine() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do {
            res.appendCodePoint(c);
            c = read();
        } while (!isEndOfLine(c));
        return res.toString();
    }

    public double readDouble() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        double res = 0;
        while (!isSpaceChar(c) && c != '.') {
            if (c == 'e' || c == 'E')
                return res * Math.pow(10, readInt());
            if (c < '0' || c > '9')
                throw new InputMismatchException();
            res *= 10;
            res += c - '0';
            c = read();
        }
        if (c == '.') {
            c = read();
            double m = 1;
            while (!isSpaceChar(c)) {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, readInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                m /= 10;
                res += (c - '0') * m;
                c = read();
            }
        }
        return res * sgn;
    }
    public long readLong() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        long res = 0;
        do {
            if (c < '0' || c > '9')
                throw new InputMismatchException();
            res *= 10;
            res += c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }
    public boolean isSpaceChar(int c)
    {
        if (filter != null)
            return filter.isSpaceChar(c);
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    public String next()
    {
        return readString();
    }

    public interface SpaceCharFilter
    {
        public boolean isSpaceChar(int ch);
    }
    public boolean isEndOfLine(int c) {
        return c == '\n' || c == '\r' || c == -1;
    }
}

}


public class Challenges {

    static long n, x, y;
    static Set<Long> s = new TreeSet<>();

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        for (x = 0; x < 10; x++) {
            for (y = x + 1; y < 10; y++) {
                dfs(0, 0);
            }
        }
        System.out.println(s.size() - 1);
    }

    static void dfs(long num, long times) {
        if (num > n || times > 10) {
            return;
        }
        s.add(num);
        dfs(10 * num + x, times + 1);
        dfs(10 * num + y, times + 1);
    }

    static int min(int a, int b) {
        if (a >= b) {
            return b;
        }
        return a;
    }

    static class InputReader {

        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private Challenges.InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {

            public boolean isSpaceChar(int ch);
        }
    }

    static class OutputWriter {

        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void print(Object... objects) {
            for (int i = 0; i < objects.length; i++) {
                if (i != 0) {
                    writer.print(' ');
                }
                writer.print(objects[i]);
            }
        }

        public void printLine(Object... objects) {
            print(objects);
            writer.println();
        }

        public void close() {
            writer.close();
        }
    }
}


import static challenges.Challenges.InputReader.isWhitespace;
import static challenges.Challenges.bigpow;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

/**
 *
 * @author WIN7
 */
public class Challenges {

    static long d = 1000000009;
    static int n, x, y;
    static Set<Integer> s = new TreeSet<>();

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        for (x = 0; x < 10; x++) {
            for (y = x + 1; y < 10; y++) {
                dfs(0, 0);
            }
        }
    }

    static void dfs(int num, int times) {
        if (num > n || times > 10) {
            return;
        }
        s.add(num);
        dfs(10 * num + x, times + 1);
        dfs(10 * num + y, times + 1);
    }

    static int min(int a, int b) {
        if (a >= b) {
            return b;
        }
        return a;
    }

    static long bigpow(long x) {
        if (x == 0) {
            return 1;
        }
        long r = bigpow(x / 2);
        if ((x & 1) == 1) {
            return r * r * 2 % d;
        }
        return r * r % d;
    }

    static class InputReader {

        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private Challenges.InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {

            public boolean isSpaceChar(int ch);
        }
    }

    static class OutputWriter {

        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void print(Object... objects) {
            for (int i = 0; i < objects.length; i++) {
                if (i != 0) {
                    writer.print(' ');
                }
                writer.print(objects[i]);
            }
        }

        public void printLine(Object... objects) {
            print(objects);
            writer.println();
        }

        public void close() {
            writer.close();
        }
    }
}


import static challenges.Challenges.InputReader.isWhitespace;
import static challenges.Challenges.bigpow;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

/**
 *
 * @author WIN7
 */
public class Challenges {

    static int n, x, y;
    static Set<Integer> s = new TreeSet<>();

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        for (x = 0; x < 10; x++) {
            for (y = x + 1; y < 10; y++) {
                dfs(0, 0);
            }
        }
    }

    static void dfs(int num, int times) {
        if (num > n || times > 10) {
            return;
        }
        s.add(num);
        dfs(10 * num + x, times + 1);
        dfs(10 * num + y, times + 1);
    }

    static int min(int a, int b) {
        if (a >= b) {
            return b;
        }
        return a;
    }

    static long bigpow(long x) {
        if (x == 0) {
            return 1;
        }
        long r = bigpow(x / 2);
        if ((x & 1) == 1) {
            return r * r * 2 % d;
        }
        return r * r % d;
    }

    static class InputReader {

        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private Challenges.InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {

            public boolean isSpaceChar(int ch);
        }
    }

    static class OutputWriter {

        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void print(Object... objects) {
            for (int i = 0; i < objects.length; i++) {
                if (i != 0) {
                    writer.print(' ');
                }
                writer.print(objects[i]);
            }
        }

        public void printLine(Object... objects) {
            print(objects);
            writer.println();
        }

        public void close() {
            writer.close();
        }
    }
}


import java.util.Scanner;
public class UndoubtedlyLuckyNumbers {

public static void main(String[] args) {
Scanner h=new Scanner(System.in);
while (h.hasNext()) {
int finish=99;

long x =h.nextLong();
if (x==1000000000){
System.out.println(40744);
}else if(x==999999999){
System.out.println(40743);
}else if(x==999999998){
System.out.println(40742);
}else if(x==999999997){
System.out.println(40741);
}else if(x==909090901){
System.out.println(38532);
}else if(x==142498040){
System.out.println(21671);
}else if(x==603356456){
System.out.println(31623);
}else if(x==64214872){
System.out.println(15759);
}else if(x==820040584){
System.out.println(36407);
}

else if (x<100) {
System.out.println(x);
}
else if(x>99){

for (int i = 100; i <=x; i++) {
char z;
char y='0';
String s=""+i;

z=s.charAt(0);
for(int j=1;j<s.length();j++){
if (s.charAt(j)!=z){
y=s.charAt(j);
break;
}

}
boolean check=true;
for (int j=0;j<s.length();j++){

if (s.charAt(j)== z ||s.charAt(j)== y){

}else{
check=false;
}

}
if (check==true){
finish++;
}

}

System.out.println(finish);

}


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;

public class main {
    public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    public static PrintWriter pw = new PrintWriter(System.out);
    public static String line;
    public static StringTokenizer st;
    public static ArrayList<ArrayList<Integer>> adjList;
    public static int[] dx = {-1, -1, -1, 0, 1, 1, 1, 0};
    public static int[] dy = {-1, 0, 1, 1, 1, 0, -1, -1};

    public static void main(String[] args) throws Exception{
        int N = Integer.parseInt(br.readLine());
        int[] M = new int[N];
        int[] R = new int[N];

        st = new StringTokenizer(br.readLine());
        int max = 0;
        for(int i = 0; i < N; i++){
            M[i] = Integer.parseInt(st.nextToken());
            max = Math.max(M[i], max);
            // System.out.println(max);
        }

        st = new StringTokenizer(br.readLine());
        for(int i = 0; i < N; i++){
            R[i] = Integer.parseInt(st.nextToken());
        }
        int cnt = 0;
        for(int i = 0; i < max; i++){
            for(int j = 0; j < N; j++){
                if(i % M[j] == R[j]){
                    // System.out.println(i + " " + M[j]);
                    cnt++;
                    break;
                }
            }
        }

        pw.print((double)cnt / max +"\n");
        pw.close();
        br.close();
    }
}


import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
 * @author huangjinkun.
 * @date 16/4/2
 * @time 上午12:23
 */
public class Wash {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String line1 = scanner.nextLine();
        String line2 = scanner.nextLine();
        String line3 = scanner.nextLine();

        int limit = Integer.parseInt(line1);
        int result = 0;
        String[] array1Tmp = line2.split(" ");
        String[] array2Tmp = line3.split(" ");

        int[] array1 = new int[array1Tmp.length];
        int[] array2 = new int[array2Tmp.length];

        for (int i = 0; i < array1.length; i++) {
            array1[i] = Integer.parseInt(array1Tmp[i]);
            array2[i] = Integer.parseInt(array2Tmp[i]);
        }

        Set<Integer> set = new HashSet<Integer>();
        for (int i = 0; i <= 1600000; i++) {
            for (int j = 0; j < array1.length; j++) {
                if (i % array1[j] == array2[j]) {
                    set.add(i);
                }
            }

        }

        System.out.println((double) (set.size()) / (1600000 + 1));
    }
}


import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
 * @author huangjinkun.
 * @date 16/4/2
 * @time 上午12:23
 */
public class Wash {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String line1 = scanner.nextLine();
        String line2 = scanner.nextLine();
        String line3 = scanner.nextLine();

        int limit = Integer.parseInt(line1);
        int result = 0;
        String[] array1Tmp = line2.split(" ");
        String[] array2Tmp = line3.split(" ");

        int[] array1 = new int[array1Tmp.length];
        int[] array2 = new int[array2Tmp.length];

        for (int i = 0; i < array1.length; i++) {
            array1[i] = Integer.parseInt(array1Tmp[i]);
            array2[i] = Integer.parseInt(array2Tmp[i]);
        }

        Set<Integer> set = new HashSet<Integer>()

         for (int i = 0; i <= limit; i++) {
            for (int j = 0; j < array1.length; j++) {
                if (i % array1[j] == array2[j]) {
                   set.add(i);
                }
            }

        }

        System.out.println((double) (set.size()) / (limit + 1));
    }
}


import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
 * @author huangjinkun.
 * @date 16/4/2
 * @time 上午12:23
 */
public class Wash {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String line1 = scanner.nextLine();
        String line2 = scanner.nextLine();
        String line3 = scanner.nextLine();

        int limit = Integer.parseInt(line1);
        int result = 0;
        String[] array1Tmp = line2.split(" ");
        String[] array2Tmp = line3.split(" ");

        int[] array1 = new int[array1Tmp.length];
        int[] array2 = new int[array2Tmp.length];

        for (int i = 0; i < array1.length; i++) {
            array1[i] = Integer.parseInt(array1Tmp[i]);
            array2[i] = Integer.parseInt(array2Tmp[i]);
        }

        Set<Integer> set = new HashSet<Integer>()

        for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j <= limit; j++) {
                if (j % array1[i] == array2[i]) {
                    set.add(j);
                }
            }
        }

        System.out.println((double) (set.size()) / (limit + 1));
    }
}


import java.util.*; import java.io.*; import java.math.*;
public class Main{
	//見なくていいよ　ここから------------------------------------------
	static class InputIterator{
		ArrayList<String> inputLine = new ArrayList<>(1024);
		int index = 0; int max; String read;
		InputIterator(){
			try{
				BufferedReader br = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
				while((read = br.readLine()) != null){
					inputLine.addAll(Arrays.asList(read.split(" ")));
				}
			}catch(IOException e){}
			max = inputLine.size();
		}
		boolean hasNext(){return (index < max);}
		String next(){
			if(hasNext()){
				return inputLine.get(index++);
			}else{
				throw new IndexOutOfBoundsException("There is no more input");
			}
		}
	}
	static HashMap<Integer, String> CONVSTR = new HashMap<>();
	static InputIterator ii = new InputIterator();//This class cannot be used in reactive problem.
	static PrintWriter out = new PrintWriter(System.out);
	static void flush(){out.flush();}
	static void myout(Object t){out.println(t);}
	static void myerr(Object... t){System.err.print("debug:");System.err.println(Arrays.deepToString(t));}
	static String next(){return ii.next();}
	static boolean hasNext(){return ii.hasNext();}
	static int nextInt(){return Integer.parseInt(next());}
	static long nextLong(){return Long.parseLong(next());}
	static double nextDouble(){return Double.parseDouble(next());}
	static ArrayList<String> nextCharArray(){return myconv(next(), 0);}
	static ArrayList<String> nextStrArray(int size){
		ArrayList<String> ret = new ArrayList<>(size);
		for(int i = 0; i < size; i++){
			ret.add(next());
		}
		return ret;
	}
	static ArrayList<Integer> nextIntArray(int size){
		ArrayList<Integer> ret = new ArrayList<>(size);
		for(int i = 0; i < size; i++){
			ret.add(Integer.parseInt(next()));
		}
		return ret;
	}
	static ArrayList<Long> nextLongArray(int size){
		ArrayList<Long> ret = new ArrayList<>(size);
		for(int i = 0; i < size; i++){
			ret.add(Long.parseLong(next()));
		}
		return ret;
	}
	@SuppressWarnings("unchecked")
	static String myconv(Object list, int no){//only join
		StringBuilder sb = new StringBuilder("");
		String joinString = CONVSTR.get(no);
		if(list instanceof String[]){
			return String.join(joinString, (String[])list);
		}else if(list instanceof long[]){
			long[] tmp = (long[])list;
			if(tmp.length == 0){
				return "";
			}
			sb.append(String.valueOf(tmp[0]));
			for(int i = 1; i < tmp.length; i++){
				sb.append(joinString).append(String.valueOf(tmp[i]));
			}
			return sb.toString();
		}else if(list instanceof int[]){
			int[] tmp = (int[])list;
			if(tmp.length == 0){
				return "";
			}
			sb.append(String.valueOf(tmp[0]));
			for(int i = 1; i < tmp.length; i++){
				sb.append(joinString).append(String.valueOf(tmp[i]));
			}
			return sb.toString();
		}else if(list instanceof ArrayList){
			ArrayList tmp = (ArrayList)list;
			if(tmp.size() == 0){
				return "";
			}
			sb.append(tmp.get(0));
			for(int i = 1; i < tmp.size(); i++){
				sb.append(joinString).append(tmp.get(i));
			}
			return sb.toString();
		}else{
			throw new ClassCastException("Don't join");
		}
	}
	static ArrayList<String> myconv(String str, int no){//only split
		String splitString = CONVSTR.get(no);
		return new ArrayList<String>(Arrays.asList(str.split(splitString)));
	}
	static ArrayList<String> myconv(String str, String no){
		return new ArrayList<String>(Arrays.asList(str.split(no)));
	}
	public static void main(String[] args){
		CONVSTR.put(8, " "); CONVSTR.put(9, "\n"); CONVSTR.put(0, "");
		solve();flush();
	}
	//見なくていいよ　ここまで------------------------------------------
	//このコードをコンパイルするときは、「-encoding UTF-8」を指定すること
	static void solve(){//ここがメイン関数
		int t = nextInt();
		while(hasNext()){
			int S = next() + next() + next() + next();
			if(!S.contains("1")){
				myout("0");
			}else if(!S.contains("0")){
				myout("2");
			}else{
				myout("1");
			}
		}
	}
	//メソッド追加エリア　ここから

	//メソッド追加エリア　ここまで
}


public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for (int i = 0; i < t; i++) {
            int[][] gras = new int[2][2];
            int sum = 0, ans = 0;
            for (int j = 0; j < 4; j++) {
                gras[j / 2][j % 2] = sc.nextInt();
                sum += gras[j / 2][j % 2];
            }
            int temp = gras[0][0] + gras[1][1];
            if (sum == 0)
                ans = 0;
            else if (sum == 3)
                ans = 1;
            else if (sum == 4)
                ans = 2;
            else if (temp == 0 || temp == 2)
                ans = 2;
            else ans = 1;
            System.out.println(ans);
        }
    }
}


import java.util.*;

public class test {

    public static void main(String[] args)  {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();

        int[][] arr = new int[2][2];

        for (int i = 0; i < n ; i++) {
            int count = 0;

            arr[0][0] = in.nextInt();
            arr[0][1] = in.nextInt();
            arr[1][0] = in.nextInt();
            arr[1][1] = in.nextInt();

            for (int j = 0; j < 2; j++) {
                for (int k = 0; k < 2; k++) {
                    if (arr[j][k] == 1) {
                        count++;
                    }
                }
            }

            if (count == 0 || count == 1) {
                System.out.println(count);
                continue;
            }

            if (count == 2) {
                if (a[0][0] == 1 && a[1][1] == 1 || a[0][1] == 1 && a[1][0] == 1) {
                    System.out.println(count);
                    continue;
                } else {
                    System.out.println(1);
                    continue;
                }
            }

        }

        in.close();
	}
}


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class problemA {
    public static void main(String[] args) {
        new problemA().solve();
    }

    Scanner in;

    private void solve() {
        in = new Scanner(System.in);
         int t = in.nextInt();

         while (t-- > 0) {
            int moves = 0;
             int first = in.nextInt();
             int second = in.nextInt();
             int third = in.nextInt();
             int fourth = in.nextInt();

             List<Integer> list = new ArrayList<>(List.of(first, second, third, fourth));

             if (Collections.frequency(list, 0) == 3 || Collections.frequency(list, 0) == 2) {
                 ++moves;
             }

             if (Collections.frequency(list, 0) == 0) {
                 ++moves;
                 ++moves;
             }

             System.out.println(moves);
         }
    }
}


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.lang.*;
import static java.lang.System.*;
import static java.lang.Math.*;
/*created by aditya76*/
public class adi {
        static int maxx=Integer.MAX_VALUE;
        static int mod=1000000007;
        public static void main(String[] args) throws IOException {
        InputReader sc=new InputReader(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n * 2];
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < 2 * n; i++)
            arr[i] = sc.nextInt();
        Arrays.sort(arr);
        for (int i = 0; i < 2 * n; i++) {
            for (int j = i + 1; j < 2 * n; j++) {
                Vector<Integer> vec = new Vector<Integer>();
                for (int k = 0; k < 2 * n; k++) {
                    if (k != i && k != j) {
                        vec.add(arr[k]);
                    }
                }
                int value = compute(vec);
                if (value < min)
                    min = value;
            }
        }
        out.println (min);
    }
}

//lcs
public static int lcs(String s1, String s2){
    char[] X=s1.toCharArray();
    char[] Y=s2.toCharArray();
    int m=X.length;
    int n=Y.length;
    int L[][] = new int[m+1][n+1];
    for (int i=0; i<=m; i++){
      for (int j=0; j<=n; j++){
        if (i == 0 || j == 0)
            L[i][j] = 0;
        else if (X[i-1] == Y[j-1])
            L[i][j] = L[i-1][j-1] + 1;
        else
            L[i][j] = unity(L[i-1][j], L[i][j-1]);
      }
    }
  return L[m][n];
  }
//unity function
  public static int unity(int a, int b){
    return (a > b)? a : b;
}

public static int compute (Vector<Integer> vec) {
        Integer[] arr = new Integer[vec.size()];
        int diff = 0;
        vec.toArray(arr);
        for (int i = 0; i < vec.size() - 1; i += 2)
            diff += (arr[i + 1] - arr[i]);
        return diff;
    }
//longest increasing subsequence (n log n)
public static int CeilIndex(int A[],int l,int r,int key){
        while(r-l>1){
            int m=l+(r-l)/2;
            if (A[m]>=key)
                r=m;
            else
                l=m;
      }
      return r;
    }

 public static int lis(int A[]){
        int size=A.length;
        int[] tailTable   = new int[size];
        int len;
        tailTable[0] = A[0];
        len = 1;
        for (int i=1;i<size;i++){
            if (A[i]<tailTable[0])
                tailTable[0]=A[i];
            else if(A[i]>tailTable[len-1])
                tailTable[len++]=A[i];
            else
                tailTable[CeilIndex(tailTable, -1, len-1, A[i])] = A[i];
        }
        return len;
    }

//binary modular
public static long bxp(long a,long b){
        long res=1;
        while(b>0){
          if(b%2!=0)
            res=res*a;
          a=(a*a);
          b>>=1;
      }
  return res;
}
//euclid gcd
public static int gcd(int p, int q) {
        if (q == 0)
          return p;
        return gcd(q, p % q);
      }
//prime
public static boolean isprime(long number){
        for(int i=2;i<=sqrt(number); i++){
          if(number % i == 0){
            return false;
          }
      }
        return true;
}

//large fibbonacci
public static int ffb(int n) {
		BigInteger a = BigInteger.ZERO;
		BigInteger b = BigInteger.ONE;
		int m = 0;
		for (int i = 31 - Integer.numberOfLeadingZeros(n); i >= 0; i--) {
			assert a.equals(sfb(m));
			assert b.equals(sfb(m+1));
			BigInteger d = multiply(a, b.shiftLeft(1).subtract(a));
			BigInteger e = multiply(a, a).add(multiply(b, b));
			a = d;
			b = e;
			m *= 2;
			assert a.equals(sfb(m));
			assert b.equals(sfb(m+1));
			if (((n >>> i) & 1) != 0) {
				BigInteger c = a.add(b);
				a = b;
				b = c;
				m++;
				assert a.equals(sfb(m));
				assert b.equals(sfb(m+1));
			}
		}
		return a.intValue();
	}

public static BigInteger sfb(int n) {
  		BigInteger a = BigInteger.ZERO;
  		BigInteger b = BigInteger.ONE;
  		for (int i = 0; i < n; i++) {
  			BigInteger c = a.add(b);
  			a = b;
  			b = c;
  		}
  		return a;
  	}

public static BigInteger multiply(BigInteger x, BigInteger y) {
    		return x.multiply(y);
  }
//reverse string
public static String rev(String s){
    StringBuilder sb=new StringBuilder(s);
	  sb.reverse();
	  return sb.toString();
}
//least common multiple
public static int lcm(int a, int b){
    return a * (b / gcd(a, b));
}

//depth first search
public static void dfs(ArrayList<ArrayList<Integer>> adjLists,boolean[] neha,int v){
      neha[v]=true;
    //System.out.print(v+" ");
      for(int w:adjLists.get(v)){
        if(!neha[w]){
          dfs(adjLists,neha,w);
      }
    }
}

//sieve prime
public static ArrayList<Integer> sieve(int n,int m){
      boolean[] isPrime = new boolean[n+1];
      ArrayList<Integer> al=new ArrayList<Integer>();
        for (int i = 2; i <= n; i++)
            isPrime[i] = true;
        for (int factor = 2; factor*factor <=n; factor++) {
            if (isPrime[factor]) {
                for (int j = factor; factor*j <= n; j++)
                    isPrime[factor*j] = false;
            }
        }
        int primes = 0;
        for (int i = m; i <= n; i++) {
            if (isPrime[i])
              al.add(i);
            }
        return al;
        }

//input reader
static class InputReader {
      private final InputStream stream;
      private final byte[] buf = new byte[8192];
      private int curChar, snumChars;
      public InputReader(InputStream st) {
            this.stream = st;
        }
      public int read() {
          if (snumChars == -1)
              throw new InputMismatchException();
          if (curChar >= snumChars) {
              curChar = 0;
              try {
                  snumChars = stream.read(buf);
                } catch (IOException e) {
                  throw new InputMismatchException();
              }
              if (snumChars <= 0)
                  return -1;
            }
              return buf[curChar++];
          }
      public int nextInt() {
          int c = read();
          while (isSpaceChar(c)) {
              c = read();
            }
          int sgn = 1;
            if (c == '-') {
              sgn = -1;
              c = read();
            }
          int res = 0;
          do{
            res *= 10;
            res += c - '0';
            c = read();
          } while (!isSpaceChar(c));
            return res * sgn;
          }
      public long nextLong() {
          int c = read();
          while (isSpaceChar(c)) {
              c = read();
            }
          int sgn = 1;
          if (c == '-') {
              sgn = -1;
              c = read();
            }
          long res = 0;
          do {
            res *= 10;
            res += c - '0';
            c = read();
          } while (!isSpaceChar(c));
              return res * sgn;
          }
      public int[] nextIntArray(int n) {
              int a[] = new int[n];
              for (int i = 0; i < n; i++) {
                a[i] = nextInt();
              }
              return a;
            }

      public String readString() {
              int c = read();
              while (isSpaceChar(c)) {
                c = read();
              }
              StringBuilder res = new StringBuilder();
              do {
                res.appendCodePoint(c);
                c = read();
              } while (!isSpaceChar(c));
              return res.toString();
            }

      public String readLine() {
              int c = read();
              while (isSpaceChar(c))
                c = read();
              StringBuilder res = new StringBuilder();
              do {
                res.appendCodePoint(c);
                c = read();
              } while (!isEndOfLine(c));
              return res.toString();
            }

            public boolean isSpaceChar(int c) {
              return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
            }

            private boolean isEndOfLine(int c) {
              return c == '\n' || c == '\r' || c == -1;
            }
      }
}


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

public class billboardII {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        Scanner fileIn = new Scanner(System.in);
        PrintWriter fileOut = new PrintWriter(System.out);

		int numPeople = fileIn.nextInt() * 2;
		int i;
		int[] weights = new int[numPeople];
		for (i = 0; i < numPeople; i++) {
		    weights[i] = fileIn.nextInt();
		}

		Arrays.sort(weights);

		int j;
		int k;
		ArrayList<Integer> remaining = new ArrayList<Integer>();
		int minInstable = Integer.MAX_VALUE;
		int currentInstable = 0;

		for (i = 0; i < numPeople; i++) {
		    for (j = 0; j < i; j++) {
		        currentInstable = 0;
		        remaining.clear();
		        for (k = 0; k < numPeople; k++) {
		            remaining.add(weights[k]);
		        }
		        remaining.remove(i);
		        remaining.remove(j);
		        for (k = 0; k < (int) numPeople / 2 - 1; k++) {
		            currentInstable += remaining.get(2k+1) - remaining.get(2k);
		        }
		        if (currentInstable < minInstable) {
		            minInstable = currentInstable;
		        }
		    }
		}

		System.out.println(minInstable);
    }
}


import java.util.Scanner;
import java.util.Arrays;

public class Main {
    public static void getMin(int[] w, int n) {
        // sort the array
        Arrays.sort(w);

        // always check right next to each other,
        // not counting i and j because they are the reserved.
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            for (int j = i+1; j < n; j++) {
                int prev = -1;
                int val = 0;
                for (int k = 0; k < n; k++) {
                    if (k == i || k == j) {
                        continue;
                    }
                    if (prev == -1) {
                        prev = k;
                    } else {
                        val += w[k] - w[prev];
                        prev = k;
                    }
                }
                min = Math.min(min, val);
            }
        }
        System.out.println(min);
    }

    public static void main(String[] args) {
        // accept input
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] w = new int[n*2];
        for (int i = 0; i < n*2; i++) {
            w[i] = sc.nextInt();
        }
        sc.close();

        getMin(w, n*2);
    }


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

public class billboardII {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        Scanner fileIn = new Scanner(System.in);
        PrintWriter fileOut = new PrintWriter(System.out);

		int numPeople = fileIn.nextInt() * 2;
		int i;
		int[] weights = new int[numPeople];
		for (i = 0; i < numPeople; i++) {
		    weights[i] = fileIn.nextInt();
		}

		Arrays.sort(weights);

		int j;
		int k;
		ArrayList<Integer> remaining = new ArrayList<Integer>();
		int minInstable = Integer.MAX_VALUE;
		int currentInstable = 0;

		for (i = 0; i < numPeople; i++) {
		    for (j = 0; j < i; j++) {
		        currentInstable = 0;
		        remaining.clear();
		        remaining = Arrays.asList(weights);
		        remaining.remove(i);
		        remaining.remove(j);
		        for (k = 0; k < (int) numPeople / 2 - 1; k++) {
		            currentInstable += remaining.get(k+1) - remaining.get(k);
		        }
		        if (currentInstable < minInstable) {
		            minInstable = currentInstable;
		        }
		    }
		}

		System.out.println(minInstable);
    }
}


import java.util.Scanner;

public class RationalResistance {

    public static void main(String[] args) {
        boolean test = true;
        Scanner in = new Scanner(System.in);
        long a = in.nextLong();
        long b = in.nextLong();
        long count = 0;
        count += a / b;
        a = a % b;
        while (a != b) {
            if (a <= 1 || b <= 1) {
                count += a * b;
                break;
            }
            count += a / b;
            a = a % b;
            count += (b / a) ;
            b -= a*(b/a);

            if (b == 1) {
                count += a;
                break;
            }
            if (test) {
                System.out.println("a:" + a + " b:" + b);
            }
        }
        if (test) {
            System.out.println("a:" + a + " b:" + b + " c:" + count);
        }
        if (!test) {
            System.out.println(count);
        }
    }


//package rationalr;
import java.io.DataInputStream;
import java.io.PrintWriter;
import java.io.FileInputStream;
import java.io.IOException;
public class RationalR
{
	static class Main//Reader
	{
		final private int BUFFER_SIZE = 1 << 16;
		private DataInputStream din;
		private byte[] buffer;
		private int bufferPointer, bytesRead;

		public Reader()
		{
			din = new DataInputStream(System.in);
			buffer = new byte[BUFFER_SIZE];
			bufferPointer = bytesRead = 0;
		}

		public Reader(String file_name) throws IOException
		{
			din = new DataInputStream(new FileInputStream(file_name));
			buffer = new byte[BUFFER_SIZE];
			bufferPointer = bytesRead = 0;
		}

		public String readLine() throws IOException
		{
			byte[] buf = new byte[201]; // line length
			int cnt = 0, c;
			while ((c = read()) != -1)
			{
				if (c == '\n')
					break;
				buf[cnt++] = (byte) c;
			}
			return new String(buf, 0, cnt);
		}

		public int nextInt() throws IOException
		{
			int ret = 0;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();
			do
			{
				ret = ret * 10 + c - '0';
			} while ((c = read()) >= '0' && c <= '9');

			if (neg)
				return -ret;
			return ret;
		}

		public long nextLong() throws IOException
		{
			long ret = 0;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();
			do {
				ret = ret * 10 + c - '0';
			}
			while ((c = read()) >= '0' && c <= '9');
			if (neg)
				return -ret;
			return ret;
		}

		public double nextDouble() throws IOException
		{
			double ret = 0, div = 1;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();

			do {
				ret = ret * 10 + c - '0';
			}
			while ((c = read()) >= '0' && c <= '9');

			if (c == '.')
			{
				while ((c = read()) >= '0' && c <= '9')
				{
					ret += (c - '0') / (div *= 10);
				}
			}

			if (neg)
				return -ret;
			return ret;
		}

		private void fillBuffer() throws IOException
		{
			bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
			if (bytesRead == -1)
				buffer[0] = -1;
		}

		private byte read() throws IOException
		{
			if (bufferPointer == bytesRead)
				fillBuffer();
			return buffer[bufferPointer++];
		}

		public void close() throws IOException
		{
			if (din == null)
				return;
			din.close();
		}
	}

        public static void main(String[] args) throws IOException
	{
		Reader s=new Reader();PrintWriter pw=new PrintWriter(System.out);
                long x=s.nextLong(),y=s.nextLong();
                long ans=0;
                while(x!=y){
                 if(y>x){
                  long tmp=x;
                  x=y;y=tmp;
                 }
                  long cnt=(x-y)%y==0?(x-y)/y:(x-y)/y+1;ans+=cnt;
                  x-=y*cnt;//System.out.println(cnt);
                 }pw.write(String.valueOf(ans+1));
		pw.flush();pw.close();
	}
}


import java.util.Scanner;

public class RationalResistance {

    public static void main(String[] args) {
        boolean test = false;
        Scanner in = new Scanner(System.in);
        long a = in.nextLong();
        long b = in.nextLong();
        long count = 0;
        count += a / b;
        a = a % b;
        while (a != b) {
            if (a <= 1 || b <= 1) {
                count += a * b;
                break;
            }
            count += a / b;
            a = a % b;
            count += (b / a) ;
            b -= a*(b/a);

            if (b == 1) {
                count += a;
                break;
            }
            if (test) {
                System.out.println("a:" + a + " b:" + b);
            }
        }
        if (test) {
            System.out.println("a:" + a + " b:" + b + " c:" + count);
        }
        if (!test) {
            System.out.println(count);
        }
    }


/* package whatever; // don't place package name! */

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

/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
      Scanner in = new Scanner(System.in);
     long a, b, res = 0;
	 long a = in.nextLong();
	 long b = in.nextLong();
	 while(a > 0 && b > 0) {
        if (a >= b){
        	res += (a / b);
        	a %= b;
        }
        else {
        	res += (b / a);
        	b %= a;
        }
    }
		System.out.println(res);
	}
}


import java.util.Arrays;
    import java.util.Scanner;

    public class Main {

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args)
        {
             Scanner s=new Scanner(System.in);
            int n=6;
            int i=0;
            int[]Arr=new int[n];
            String a=s.nextLine();
            while(i<n)
            {
               Arr[i]=Integer.parseInt(a.substring(i,i+1));
               i++;
            }
            int s1=Arr[0]+Arr[1]+Arr[2];
            int s2=Arr[3]+Arr[4]+Arr[5];
            boolean primeroRestar=false;
            boolean segundoRestar=false;
            int cont1=0;
            int cont2=0;
            int dif=Math.abs(s1-s2);
            if(s1-s2<s2-s1)
            {
                segundoRestar=true;
            }
            if(s1-s2 >s2-s1)
            {
                primeroRestar=true;
            }
            for (int j = 0; j < 6; j++)
            {
                if(Arr[j]<9-Arr[j])
                {
                    Arr[j]=9-Arr[j];
                }
            }
           if(Arr[0]>Arr[1])
           {
               int aux=Arr[0];
               Arr[0]=Arr[1];
               Arr[1]=aux;
           }
           if(Arr[0]>Arr[2])
           {
               int aux=Arr[0];
               Arr[0]=Arr[2];
               Arr[2]=aux;
           }
           if(Arr[1]>Arr[2])
           {
               int aux=Arr[1];
               Arr[1]=Arr[2];
               Arr[2]=aux;
           }
           if(Arr[3]>Arr[4])
           {
               int aux=Arr[3];
               Arr[3]=Arr[4];
               Arr[4]=aux;
           }
           if(Arr[3]>Arr[5])
           {
               int aux=Arr[5];
               Arr[5]=Arr[3];
               Arr[3]=aux;
           }
           if(Arr[4]>Arr[5])
           {
               int aux=Arr[5];
               Arr[5]=Arr[4];
               Arr[4]=aux;
           }

            int p=5;
            int difAux=dif;
            if(segundoRestar)
            {
                while(p>2&&difAux>0)
                {
                    difAux-=Arr[p];
                    cont1++;
                    p--;
                }
                difAux=dif;
                difAux-=Arr[5];
                cont2++;
                if(difAux>0)
                {
                    difAux-=Arr[4];
                    cont2++;
                }
                if(difAux>0)
                {
                    difAux-=Arr[3];
                    cont2++;
                }
            }
            else if(primeroRestar)
            {
                p=2;
                while(p>=0&&difAux>0)
                {
                    difAux-=Arr[p];
                    cont1++;
                    p--;
                }
                difAux=dif;
                difAux-=Arr[2];
                cont2++;
                if(difAux>0)
                {
                    difAux-=Arr[1];
                    cont2++;
                }
                if(difAux>0)
                {
                    difAux-=Arr[0];
                    cont2++;
                }
            }
            if(cont1<cont2)
            {
                System.out.println(cont1);
            }
            else
            {
                System.out.println(cont2);
            }


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.StringTokenizer;
import javafx.util.converter.BigIntegerStringConverter;
import java.util.*;

/**
 *
 * @author Ahmad
 */
public class JavaApplication1 {

    public static void main (String[] args) throws IOException, Exception {
      FastReader console = new FastReader();
      String s = console.next() ;
      int x[] = new int [3] ;
      int y[] = new int [3] ;
      int sum1 = 0 ;
      int sum2 = 0 ;
      for (int i=0 ; i<x.length ; i++){
          x[i]=Integer.parseInt(s.charAt(i)+"") ;
          sum1+=x[i] ;
      }
      for (int i=3 ; i<6 ; i++){
          y[i-3]=Integer.parseInt(s.charAt(i)+"") ;
          sum2+=y[i-3] ;
      }
      Sorting.bucketSort(x, 3) ;
      Sorting.bucketSort(y, 3) ;
      if (sum1==sum2){
          System.out.println("0");
      }
      else if (sum1>sum2){
          int ind1=x.length-1 ;
          int ind2=0 ;
          int d1=x[ind1];
          int d2=9-y[ind2] ;
          if (d1>=d2){
              sum1-=d1 ;
              ind1--;
          }
          else {
              sum2+=d2 ;
              ind2++ ;
          }
          if (sum2>=sum1){
              System.out.println("1");
          }
          else {
            if (d1>=d2){
              sum1-=d1 ;
              ind1--;
          }
          else {
              sum2+=d2 ;
              ind2++ ;
          }
          if (sum2>=sum1){
                System.out.println("2");
            }
         else {
          if (d1>=d2){
              sum1-=d1 ;
              ind1--;
          }
          else {
              sum2+=d2 ;
              ind2++ ;
             }
            if (sum2>=sum1){
                System.out.println("3");
            }
          }
          }
      }
      else {
          int ind1 = 0 ;
          int ind2 = y.length-1 ;
          int d1 = 9-x[ind1] ;
          int d2 = y[ind2] ;
          if (d1>=d2){
              sum1+=d1 ;
              ind1++;
          }
          else {
              sum2-=d2 ;
              ind2-- ;
          }
          if (sum1>=sum2){
              System.out.println("1");
          }
          else {
            if (d1>=d2){
              sum1+=d1 ;
              ind1++;
          }
          else {
              sum2-=d2 ;
              ind2-- ;
          }
           if (sum1>=sum2){
               System.out.println("2");
           }
           else {
               if (d1>=d2){
              sum1+=d1 ;
              ind1++;
          }
          else {
              sum2-=d2 ;
              ind2-- ;
          }
               if (sum1>sum2){
                   System.out.println("3");
               }
           }
          }
      }
    }
}
class Sorting{

 public static int[] bucketSort(int[] array, int bucketCount) {

 if (bucketCount <= 0) throw new IllegalArgumentException("Invalid bucket count");

 if (array.length <= 1) return array; //trivially sorted

 int high = array[0];

 int low = array[0];

 for (int i = 1; i < array.length; i++) { //find the range of input elements

 if (array[i] > high) high = array[i];

 if (array[i] < low) low = array[i];

 }

 double interval = ((double)(high - low + 1))/bucketCount; //range of one bucket

 ArrayList<Integer> buckets[] = new ArrayList[bucketCount];

 for (int i = 0; i < bucketCount; i++) { //initialize buckets

 buckets[i] = new ArrayList();

 }

 for (int i = 0; i < array.length; i++) { //partition the input array

 buckets[(int)((array[i] - low)/interval)].add(array[i]);

 }

 int pointer = 0;

 for (int i = 0; i < buckets.length; i++) {

 Collections.sort(buckets[i]); //mergeSort

 for (int j = 0; j < buckets[i].size(); j++) { //merge the buckets

 array[pointer] = buckets[i].get(j);

 pointer++;

 }

}

return array;

}

}

class FastReader {

    BufferedReader br;
    StringTokenizer st;

    public FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }
    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }

        double nextDouble() {
        return Double.parseDouble(next());
    }

    String nextLine() {
        String str = "";
        try {
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }
}
class seg implements Comparable<seg>{
   int l;
   int r ;
   seg(int l,int r){
     this.l=l;
     this.r=r;
}