import java.util.*;
public class P1{
  public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    int t=sc.nextInt();
    while (t-->0) {
      int h=sc.nextInt();
      int m=sc.nextInt();
      int ans=0;
      if (h==0 && m==0) {
        System.out.println("0");
      }
      else{
        if (m>0) {
          ans+=(60-m);
        }
        ans+=60*(23-h);
        System.out.println(ans);
      }
    }
  }
}


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

public class A {

    public static void main(String[] args) {
        Scanner sc = new Scanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));

        int N = sc.nextInt();

        for(int i = 0; i < N; i++) {
            int h = sc.nextInt(), s = sc.nextInt();
            out.println((23 - h) * 60 + 60 - s);
        }
    }

    public static PrintWriter out;

    //-----------MyScanner class for faster input----------
    public static class Scanner {
        BufferedReader br;
        StringTokenizer st;

        public Scanner() {
            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;
        }

    }
    //--------------------------------------------------------
}


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

public class A {

    public static void main(String[] args) {
        Scanner sc = new Scanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));

        int N = sc.nextInt();

        for(int i = 0; i < N; i++) {
            int h = sc.nextInt(), s = sc.nextInt();
            out.println((23 - h) * 60 + 60 - s);
        }
        out.close();
    }

    public static PrintWriter out;

    //-----------MyScanner class for faster input----------
    public static class Scanner {
        BufferedReader br;
        StringTokenizer st;

        public Scanner() {
            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;
        }

    }
    //--------------------------------------------------------
}


import java.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Candidate {
    public static void main(String[] args) throws Exception {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0){
            int h=sc.nextInt();
            int m=sc.nextInt();
            int c=(23-h)*60+(60-m);
            System.out.println(c);
        }
    }
}


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

public class PerfectPermutation {

	public static void main(String[] args) {

		Scanner in = new Scanner(System.in);

		int n = in.nextInt();

		in.nextLine();
		String[] time = new String[n];
		Integer[] result = new Integer[n];

		for(int i = 0; i < time.length; i++) {
			time[i] = in.nextLine();
		}

		for(int i=0;i<time.length;i++) {
			String [] splitter = time[i].split(" ");

			int hour = Integer.parseInt(splitter[0]);
			int mint = Integer.parseInt(splitter[1]);

			int h =0;
			int m =0;

			if(hour != 24) {

				h = 23 - hour;
			}
			if(mint != 60 || mint != 0) {

				m = 60 - mint;
			}

			result[i] = h * 60 + m;

		}

		for(int i = 0; i < result.length; i++) {
			System.out.println(result[i]);
		}
	}

}


package A;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class PerfectPermutation {

	public static void main(String[] args) {

		Scanner in = new Scanner(System.in);

		int n = in.nextInt();

		in.nextLine();
		String[] time = new String[n];
		Integer[] result = new Integer[n];

		for(int i = 0; i < time.length; i++) {
			time[i] = in.nextLine();
		}

		for(int i=0;i<time.length;i++) {
			String [] splitter = time[i].split(" ");

			int hour = Integer.parseInt(splitter[0]);
			int mint = Integer.parseInt(splitter[1]);

			int h =0;
			int m =0;

			if(hour != 24) {

				h = 23 - hour;
			}
			if(mint != 60 || mint != 0) {

				m = 60 - mint;
			}

			result[i] = h * 60 + m;

		}

		for(int i = 0; i < result.length; i++) {
			System.out.println(result[i]);
		}
	}

}


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		int t;
		int hh[] = new int[1440];
		int mm[] = new int[1440];
		int a[] = new int[1440];
		Scanner sc = new Scanner(System.in);
		t = sc.nextInt();
		for (int i = 0; i < t; i++) {
			hh[i] = sc.nextInt();
			mm[i] = sc.nextInt();
		}
		for (int j = 0; j < t; j++) {
			a[j] = (24 - hh[j]) * 60 - mm[j];
		}
		for (int i = 0; i < t; i++)
			System.out.printf("  \n" + a[i]);
	}

}


package ti;

import java.util.Scanner;

public class la {

	public static void main(String[] args) {
		int t;
		int hh[] = new int[1440];
		int mm[] = new int[1440];
		int a[] = new int[1440];
		Scanner sc = new Scanner(System.in);
		t = sc.nextInt();
		for (int i = 0; i < t; i++) {
			hh[i] = sc.nextInt();
			mm[i] = sc.nextInt();
		}
		for (int j = 0; j < t; j++) {
			a[j] = (24 - hh[j]) * 60 - mm[j];
		}
		for (int i = 0; i < t; i++)
			System.out.printf("  \n" + a[i]);
	}

}


import java.util.Scanner;

public class MinutesBeforeTheNewYear {

	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		int n=scan.nextInt();
		for(int i=0;i<n;i++) {
			int a=scan.nextInt();
			int b=scan.nextInt();
			if(a>0 || b>0) {
			System.out.println(1440-(a*60+b));
			}
		}

	}

}


import java.util.Scanner;

public class MinutesBeforeTheNewYear {

	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		int n=scan.nextInt();
		for(int i=0;i<n;i++) {
			int a=scan.nextInt();
			int b=scan.nextInt();
			if(a>0 || b>1) {
			System.out.println(1440-(a*60+b));
			}
		}

	}

}


import java.util.*;
public class hi
{
public static void main(String args[])
{
    Scanner in=new Scanner(System.in);
    int t=in.nextInt();
    for (int i=0;i<t;i++)
    {
        h=in.nextInt();
        m=in.nextInt();
        System.out.println(1440-h*60-m);
    }
}
}


import java.util.*;
public class hi
{
public static void main(String args[])
{
    Scanner in=new Scanner(System.in);
    int h,m;
    int t=in.nextInt();
    for (int i=0;i<t;i++)
    {
        h=in.nextInt();
        m=in.nextInt();
        System.out.println(1440-h*60-m);
    }
}
}


import java.util.*;
public class cpsolve {
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        int T=in.nextInt();
        for(int t=0;t<T;t++) {
            int h = in.nextInt();
            int m=in.nextInt();
            int ans=(23-h)*60+(60-m);
            System.out.println(ans);
        }
    }
}


package CF;
import java.util.Scanner;

public class main {

    public static void main(String[] args) {

        int t ,h, m ,lh ,lm ;
        Scanner scann = new Scanner(System.in);
        t= scann.nextInt();
        int i =0;
        while (i<t)
        {
            h= scann.nextInt();
            m= scann.nextInt();
                lh = (23 - h) * 60;
                lm = 60 - m ;
            System.out.println( lh + lm );
            i++;
        }
    }
}


import java.util.Scanner;

public class main {

    public static void main(String[] args) {

        int t ,h, m ,lh ,lm ;
        Scanner scann = new Scanner(System.in);
        t= scann.nextInt();
        int i =0;
        while (i<t)
        {
            h= scann.nextInt();
            m= scann.nextInt();
                lh = (23 - h) * 60;
                lm = 60 - m ;
            System.out.println( lh + lm );
            i++;
        }
    }
}


package CF;
import java.util.Scanner;
public class main {
    public static void main(String[] args) {
        int t, h, m;
        Scanner scann = new Scanner(System.in);
        t= scann.nextInt();
        while(t--)
       {
             h= scann.nextInt();
             m= scann.nextInt();
             int total= ((23 - h) * 60) + (60 - m);
             System.out.println(total);
        }
    }

}


package CF;
import java.util.Scanner;
public class main {
    public static void main(String[] args) {
        int t, h, m;
        Scanner scann = new Scanner(System.in);
        t= scann.nextInt();
        while(t--)
       {
             h= scann.nextInt();
             m= scann.nextInt();
             int total= ((23 - h) * 60) + (60 - m);
             System.out.println(total);
        }
    }
}


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

public class Mian{
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int t = in.nextInt();
		int[]arr = new int[t];
		for(int i = 0; i < t; i++){
		int h = in.nextInt();
		int m = in.nextInt();
		arr[i] = method(h,m);
	}
	for(int j = 0;j < arr.length; j++){
	System.out.println(arr[j]);
	}
	}
	public static int method(int h, int m){
	int count = 0;
	count = 1440 - (h * 60 + m);
	return count;
	}
}


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

public class Mian{
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int t = in.nextInt();
		for(int i = 0; i < t; i++){
		int h = in.nextInt();
		int m = in.nextInt();
		method(h,m);
	}
	}
	public static void method(int h, int m){
	int count = 1440 - (h * 60 + m);
	System.out.print(count);
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class MinutesBeforetheNewYear
{
    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

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

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

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

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

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

    public static void main(String[] args)
    {
        FastReader fastReader = new FastReader();
        StringBuilder builder = new StringBuilder();

        int t =  fastReader.nextInt();

        for(int i=1;i<=t;i++)
        {
            int h = fastReader.nextInt();
            int m = fastReader.nextInt();

            builder.append(24*60-(h*60)-m).append("\n");
        }

        System.out.println(builder.toString());
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class MinutesBeforetheNewYear
{
    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

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

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

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

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

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

    public static void main(String[] args)
    {
        FastReader fastReader = new FastReader();
        StringBuilder builder = new StringBuilder();

        int t =  fastReader.nextInt();

        for(int i=1;i<=t;i++)
        {
            int h = fastReader.nextInt();
            int m = fastReader.nextInt();

            int sum = 60-m;
            ++h;
            if(h<24)
            {
                sum = sum + ((24-h)*60);
                builder.append(sum).append("\n");
            }
            else
                builder.append(sum).append("\n");
        }

        System.out.println(builder.toString());
    }
}


import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Set;
import java.util.TreeMap;

public class A {
	public static int ans = Integer.MAX_VALUE;
	public static int[] dp = new int[1000006];

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		InputReader s = new InputReader(System.in);
		PrintWriter p = new PrintWriter(System.out);
		int t = s.nextInt();
		while (t-- > 0) {
			int ans = 24 * 60;
			int hh = s.nextInt();
			int mm = s.nextInt();
			ans -= (hh * 60 + mm);
			System.out.println(ans);
		}
		p.flush();
		p.close();
	}

	public static int calmin(int n) {
		dp[0] = 0;
		for (int i = 1; i <= n; i++) {
			String str = i + "";
			int ans = Integer.MAX_VALUE;
			for (int j = 0; j < str.length(); j++) {
				if (str.charAt(j) == '0')
					continue;
				int sub = Integer.parseInt(str.charAt(j) + "");
				ans = Math.min(ans, 1 + dp[i - sub]);
			}
			dp[i] = ans;
		}
		return dp[n];
	}

	public static void dfs(ArrayList[] adj, int ci, int[] vis) {
		if (vis[ci] == 1) {
			return;
		}
		vis[ci] = 1;
		ArrayList<Integer> nbrs = adj[ci];
		for (int i = 0; i < nbrs.size(); i++) {
			if (vis[nbrs.get(i)] == 0) {
				dfs(adj, nbrs.get(i), vis);
			}
		}
	}

	public static class node {
		int index;
		HashMap<Integer, Integer> nbrs;

		node(int i) {
			this.index = i;
			this.nbrs = new HashMap<>();
		}
	}

	public static void union(int[] arr, int[] size, int a, int b) {
		int root_a = root(arr, a);
		int root_b = root(arr, b);
		if (size[root_a] >= size[root_b]) {
			arr[root_b] = root_a;
			size[root_a] += size[root_b];
		} else {
			arr[root_a] = root_b;
			size[root_b] += size[root_a];
		}
	}

	public static int root(int[] arr, int i) {
		while (arr[i] != i) {
			i = arr[i];
		}
		return i;
	}

	static class pair implements Comparable<pair> {
		Integer x, y;

		pair(int x, int y) {
			this.x = x;
			this.y = y;
		}

		public int compareTo(pair o) {
			int result = x.compareTo(o.x);
			if (result == 0)
				result = y.compareTo(o.y);

			return result;
		}

		public String toString() {
			return x + " " + y;
		}

		public boolean equals(Object o) {
			if (o instanceof pair) {
				pair p = (pair) o;
				return p.x - x == 0 && p.y - y == 0;
			}
			return false;
		}

		public int hashCode() {
			return new Long(x).hashCode() * 31 + new Long(y).hashCode();
		}
	}

	public static ArrayList Divisors(long n) {
		ArrayList<Long> div = new ArrayList<>();

		for (long i = 1; i <= Math.sqrt(n); i++) {
			if (n % i == 0) {
				div.add(i);

				if (n / i != i)
					div.add(n / i);
			}
		}
		return div;
	}

	public static int BinarySearch(long[] a, long k) {
		int n = a.length;
		int i = 0, j = n - 1;
		int mid = 0;

		if (k < a[0])
			return 0;
		else if (k >= a[n - 1])
			return n;
		else {
			while (j - i > 1) {
				mid = (i + j) / 2;

				if (k >= a[mid])
					i = mid;
				else
					j = mid;
			}
		}

		return i + 1;
	}

	public static long GCD(long a, long b) {
		if (b == 0)
			return a;
		else
			return GCD(b, a % b);
	}

	public static long LCM(long a, long b) {
		return (a * b) / GCD(a, b);
	}

	static class InputReader {

		private final InputStream stream;
		private final byte[] buf = new byte[8192];
		private int curChar, snumChars;
		private SpaceCharFilter filter;

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

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

		public long nextLong() {
			int c = snext();
			while (isSpaceChar(c)) {
				c = snext();
			}
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} 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 = snext();
			while (isSpaceChar(c)) {
				c = snext();
			}
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = snext();
			} while (!isSpaceChar(c));
			return res.toString();
		}

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

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

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

		public interface SpaceCharFilter {
			public boolean isSpaceChar(int ch);
		}
	}

	static class CodeX {
		public static void sort(long arr[]) {
			merge_sort(arr, 0, arr.length - 1);
		}

		private static void merge_sort(long A[], long start, long end) {
			if (start < end) {
				long mid = (start + end) / 2;
				merge_sort(A, start, mid);
				merge_sort(A, mid + 1, end);
				merge(A, start, mid, end);
			}

		}

		private static void merge(long A[], long start, long mid, long end) {
			long p = start, q = mid + 1;
			long Arr[] = new long[(int) (end - start + 1)];
			long k = 0;

			for (int i = (int) start; i <= end; i++) {
				if (p > mid)
					Arr[(int) k++] = A[(int) q++];

				else if (q > end)
					Arr[(int) k++] = A[(int) p++];

				else if (A[(int) p] < A[(int) q])
					Arr[(int) k++] = A[(int) p++];

				else
					Arr[(int) k++] = A[(int) q++];
			}
			for (int i = 0; i < k; i++) {
				A[(int) start++] = Arr[i];
			}

		}

	}

}


import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.InputMismatchException;

public class A {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		InputReader s = new InputReader(System.in);
		PrintWriter p = new PrintWriter(System.out);
		int t = s.nextInt();
		while (t-- > 0) {
			int h = s.nextInt();
			int m = s.nextInt();
			int ans = 24 * 60 - h * 60 - m;
			System.out.println(ans);
		}
		p.flush();
		p.close();
	}

	static class pair implements Comparable<pair> {
		Integer x, y;

		pair(int x, int y) {
			this.x = x;
			this.y = y;
		}

		public int compareTo(pair o) {
			int result = x.compareTo(o.x);
			if (result == 0)
				result = y.compareTo(o.y);

			return result;
		}

		public String toString() {
			return x + " " + y;
		}

		public boolean equals(Object o) {
			if (o instanceof pair) {
				pair p = (pair) o;
				return p.x - x == 0 && p.y - y == 0;
			}
			return false;
		}

		public int hashCode() {
			return new Long(x).hashCode() * 31 + new Long(y).hashCode();
		}
	}

	public static ArrayList Divisors(long n) {
		ArrayList<Long> div = new ArrayList<>();

		for (long i = 1; i <= Math.sqrt(n); i++) {
			if (n % i == 0) {
				div.add(i);

				if (n / i != i)
					div.add(n / i);
			}
		}
		return div;
	}

	public static int BinarySearch(long[] a, long k) {
		int n = a.length;
		int i = 0, j = n - 1;
		int mid = 0;

		if (k < a[0])
			return 0;
		else if (k >= a[n - 1])
			return n;
		else {
			while (j - i > 1) {
				mid = (i + j) / 2;

				if (k >= a[mid])
					i = mid;
				else
					j = mid;
			}
		}

		return i + 1;
	}

	public static long GCD(long a, long b) {
		if (b == 0)
			return a;
		else
			return GCD(b, a % b);
	}

	public static long LCM(long a, long b) {
		return (a * b) / GCD(a, b);
	}

	static class InputReader {

		private final InputStream stream;
		private final byte[] buf = new byte[8192];
		private int curChar, snumChars;
		private SpaceCharFilter filter;

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

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

		public long nextLong() {
			int c = snext();
			while (isSpaceChar(c)) {
				c = snext();
			}
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} 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 = snext();
			while (isSpaceChar(c)) {
				c = snext();
			}
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = snext();
			} while (!isSpaceChar(c));
			return res.toString();
		}

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

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

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

		public interface SpaceCharFilter {
			public boolean isSpaceChar(int ch);
		}
	}

	static class CodeX {
		public static void sort(long arr[]) {
			merge_sort(arr, 0, arr.length - 1);
		}

		private static void merge_sort(long A[], long start, long end) {
			if (start < end) {
				long mid = (start + end) / 2;
				merge_sort(A, start, mid);
				merge_sort(A, mid + 1, end);
				merge(A, start, mid, end);
			}

		}

		private static void merge(long A[], long start, long mid, long end) {
			long p = start, q = mid + 1;
			long Arr[] = new long[(int) (end - start + 1)];
			long k = 0;

			for (int i = (int) start; i <= end; i++) {
				if (p > mid)
					Arr[(int) k++] = A[(int) q++];

				else if (q > end)
					Arr[(int) k++] = A[(int) p++];

				else if (A[(int) p] < A[(int) q])
					Arr[(int) k++] = A[(int) p++];

				else
					Arr[(int) k++] = A[(int) q++];
			}
			for (int i = 0; i < k; i++) {
				A[(int) start++] = Arr[i];
			}

		}

	}

}


import java.util.Scanner;

public class MinutesBeforeNY {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int T = scanner.nextInt();
        while (T-- > 0) {
            int hh = scanner.nextInt();
            int mm = scanner.nextInt();
            System.out.println((60-mm) + (60 * (24 - (hh + 1))));
        }
    }
}


package Codeforces.Practice;

import java.util.Scanner;

public class MinutesBeforeNY {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int T = scanner.nextInt();
        while (T-- > 0) {
            int hh = scanner.nextInt();
            int mm = scanner.nextInt();
            System.out.println((60-mm) + (60 * (24 - (hh + 1))));
        }
    }
}


import java.util.*;

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

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        int cases;

        Scanner input = new Scanner(System.in);
        cases = input.nextInt();
        int h = 0;
        int m = 0;
        int time_remain = 0;
        int[] output = new int[cases];
        input.nextLine();

        for(int i = 0 ; i < cases ; i++){

          String in = input.nextLine();
          String[] st = in.split("\\s+");
          h = Integer.parseInt(st[0]);
          m = Integer.parseInt(st[1]);

          time_remain += (23 - h) * 60;
          time_remain += 60 - m;
          output[i] = time_remain;
          time_remain = 0;

        }

        for(int s : output){

	      System.out.println(s);

		}//end printing loop

    }

}


import java.util.*;
public class Solution
{
   public static void main(String ar[])
   {
       Scanner scan=new Scanner(System.in);
       int test=scan.nextInt();
       while(test-->0)
       {
           int h=scan.nextInt();
           int m=scan.nextInt();
           int ans=24*60;
           if(h==0)
            System.out.println(0);
            else
                System.out.println(ans-(h*60+m));
       }
   }
}


import java.util.*;
public class Solution
{
   public static void main(String ar[])
   {
       Scanner scan=new Scanner(System.in);
       int test=scan.nextInt();
       while(test-->0)
       {
           int h=scan.nextInt();
           int m=scan.nextInt();
           int ans=24*60;
           if(h+m==0)
            System.out.println(0);
            else
                System.out.println(ans-(h*60+m));
       }
   }
}


import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.time.LocalTime;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Main {

    public static void main(String[] args) {

    	FastScanner scan = new FastScanner();

    	int t = scan.nextInt();
    	for(int tt=0; tt<t; tt++) {
    		int h = scan.nextInt(), m = scan.nextInt();
    		long amount = 0;
    		amount = 60-m;
    		h++;
    		amount += (long)(24-h)*60;
    		System.out.println(amount);
    	}
    }

    public static void sort(int [] a) {
    	ArrayList<Integer> b = new ArrayList<>();
    	for(int i: a) b.add(i);
    	Collections.sort(b);
    	for(int i=0; i<a.length; i++) a[i]= b.get(i);
    }

    static class FastScanner{

    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	StringTokenizer st = new StringTokenizer("");
    	String next() {
    		while(!st.hasMoreTokens())
    			try {
    				st = new StringTokenizer(br.readLine());
    			} catch (IOException e){
    				e.printStackTrace();
    			}
    		return st.nextToken();
    	}

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

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

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

    }

}


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

public class P1283A_MinutesBeforeTheNewYear {
	public static void main(String subhani[]) throws IOException, NumberFormatException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(br.readLine());
		while (t-- > 0) {
			StringTokenizer st = new StringTokenizer(br.readLine());
			int h = Integer.parseInt(st.nextToken()), m = Integer.parseInt(st.nextToken());
			solution(h, m);
		}
	}

	public static void solution(int h, int m) {
		System.out.println(24 * 60 - (h * 60 + m));
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class A1283 {

	public static void main(String[] args) {
		FastReader sc = new FastReader();
		int t = sc.nextInt();

		while (t-- > 0) {
			int hh = sc.nextInt();
			int mm = sc.nextInt();
			System.out.println((24 - hh-1)*60 + 60-mm);
		}

	}

	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

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

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

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

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

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

	}

}


import java.util.Scanner;

public class MinutesBeforeTheNewYear {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for(int i = 0 ;i < n; i++){
            int hrs = sc.nextInt();
            int mins = sc.nextInt();
            int res = 1440;
            res = res - ((hrs*60) + mins);
            System.out.println(res);
        }
    }

}


public class MinutesBeforeTheNewYear {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for(int i = 0 ;i < n; i++){
            int hrs = sc.nextInt();
            int mins = sc.nextInt();
            int res = 1440;
            res = res - ((hrs*60) + mins);
            System.out.println(res);
        }
    }

}


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Created by JunHyung Lim on 2019-12-29
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(new BufferedInputStream(System.in));
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(System.out)));
        int count = scanner.nextInt();
        for (int i = 0; i < count; i++) {
            int hours = scanner.nextInt();
            int minutes = scanner.nextInt();
            writer.println((24 - hours) * 60 - minutes);
        }
        writer.flush();
    }
}


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Created by JunHyung Lim on 2019-12-29
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(new BufferedInputStream(System.in));
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(System.out)));
        int count = scanner.nextInt();
        for (int i = 0; i < count; i++) {
            int n = scanner.nextInt(); // 19
            int k = scanner.nextInt(); // 4
            int a = n / k; // 4
            int b = a + 1; // 5
            int bigger = k / 2;
            writer.println(bigger * b + (k - bigger) * a);// 8
        }
        writer.flush();
    }
}


import java.util.*;

public class minsbeforenewyear {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        StringBuilder sb = new StringBuilder();
        int t = sc.nextInt();
        while (t-- > 0) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            if (a < 23) {
                int diff = 23 - a;
                diff = (diff * 60);
                diff += (60 - b);
                sb.append(diff);
            } else {
                int diff = 60 - b;
                sb.append(diff);
            }
            sb.append("\n");
        }
        System.out.print(sb);
        sc.close();
    }
}


import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class Practice {

	public static int gcd(int n1, int n2)
    {
        if (n2 != 0)
            return gcd(n2, n1 % n2);
        else
            return n1;
    }

	static class pair {
        Integer x,y;
        pair (int x,int y){
            this.x=x;
            this.y=y;
        }
    }

	static class length implements Comparator<String>
	{
		public int compare(String a,String b)
		{
			return(a.length()-b.length());
		}
	}

	   static class FastReader
	   {
		   BufferedReader br;
		   StringTokenizer st;
		   public FastReader()
		   {
			   br = new BufferedReader(new InputStreamReader(System.in));
		   }

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

		   int nextInt()
		   {
			   return Integer.parseInt(next());
		   }
		   long nextLong()
		   {
			   return Long.parseLong(next());
		   }
		   double nextDouble()
	        {
	            return Double.parseDouble(next());
	        }

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

	  public static long Divisors(long n)
	   {
	       // Note that this loop runs till square root
		   long sum=0;
	       for (int i=1; i*i<=(n); i++)
	       {
	           if (n%i == 0)
	           {
	               // If divisors are equal, print only one
	               if (n/i == i)
	               {
	                  // printf("%d ", i);
	            	   sum=sum+1;
	               }

	               else // Otherwise print both
	               {
	                   //printf("%d %d ", i, n/i);
	            	   sum=sum+2;
	               }
	           }
	       }
	       return(sum);
	   }

	  public static long SmallestPrimeDivisor(long n)
	  {
	      // if divisible by 2
	      if (n % 2 == 0)
	          return 2;

	      // iterate from 3 to sqrt(n)
	      for (long i = 3; i * i <= n; i += 2) {
	          if (n % i == 0)
	              return i;
	      }

	      return n;
	  }

	   public static boolean isprime(long n)
	    {
	        int count=0;

	        for(long i=2;i*i<=n;i++)
	        {
	            if(n%i==0)
	            {
	                return(false);
	            }
	        }
	        return(true);
	    }

		public static void main(String []args) throws Exception
		{

			/*
			 FastReader in =new FastReader();
				//Scanner in=new Scanner(System.in);
			PrintWriter pw = new PrintWriter(System.out);
			//double x=2.0;
			BigInteger p = BigInteger.ONE;
			pw.print(p);
			pw.flush();
			pw.close();
			*/
			//Scanner in=new Scanner(System.in);
			 FastReader in =new FastReader();
			 PrintWriter pw = new PrintWriter(System.out);
			//double x=2.0;
			int t=in.nextInt();
			for(int i=0;i<t;i++)
			{
				int n=in.nextInt();
				int m=in.nextInt();
				pw.println((23-n)*60+(60-m));
			}

	        pw.flush();
			pw.close();

	}

}


package practice1;
//import java.io.InputStreamRea
//package FastIO;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class Practice {

	public static int gcd(int n1, int n2)
    {
        if (n2 != 0)
            return gcd(n2, n1 % n2);
        else
            return n1;
    }

	static class pair {
        Integer x,y;
        pair (int x,int y){
            this.x=x;
            this.y=y;
        }
    }

	static class length implements Comparator<String>
	{
		public int compare(String a,String b)
		{
			return(a.length()-b.length());
		}
	}

	   static class FastReader
	   {
		   BufferedReader br;
		   StringTokenizer st;
		   public FastReader()
		   {
			   br = new BufferedReader(new InputStreamReader(System.in));
		   }

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

		   int nextInt()
		   {
			   return Integer.parseInt(next());
		   }
		   long nextLong()
		   {
			   return Long.parseLong(next());
		   }
		   double nextDouble()
	        {
	            return Double.parseDouble(next());
	        }

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

	  public static long Divisors(long n)
	   {
	       // Note that this loop runs till square root
		   long sum=0;
	       for (int i=1; i*i<=(n); i++)
	       {
	           if (n%i == 0)
	           {
	               // If divisors are equal, print only one
	               if (n/i == i)
	               {
	                  // printf("%d ", i);
	            	   sum=sum+1;
	               }

	               else // Otherwise print both
	               {
	                   //printf("%d %d ", i, n/i);
	            	   sum=sum+2;
	               }
	           }
	       }
	       return(sum);
	   }

	  public static long SmallestPrimeDivisor(long n)
	  {
	      // if divisible by 2
	      if (n % 2 == 0)
	          return 2;

	      // iterate from 3 to sqrt(n)
	      for (long i = 3; i * i <= n; i += 2) {
	          if (n % i == 0)
	              return i;
	      }

	      return n;
	  }

	   public static boolean isprime(long n)
	    {
	        int count=0;

	        for(long i=2;i*i<=n;i++)
	        {
	            if(n%i==0)
	            {
	                return(false);
	            }
	        }
	        return(true);
	    }

		public static void main(String []args) throws Exception
		{

			/*
			 FastReader in =new FastReader();
				//Scanner in=new Scanner(System.in);
			PrintWriter pw = new PrintWriter(System.out);
			//double x=2.0;
			BigInteger p = BigInteger.ONE;
			pw.print(p);
			pw.flush();
			pw.close();
			*/
			//Scanner in=new Scanner(System.in);
			 FastReader in =new FastReader();
			 PrintWriter pw = new PrintWriter(System.out);
			//double x=2.0;
			int t=in.nextInt();
			for(int i=0;i<t;i++)
			{
				int n=in.nextInt();
				int m=in.nextInt();
				pw.println((23-n)*60+(60-m));
			}

	        pw.flush();
			pw.close();

	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class A {

	public static void main(String[] args) {
		FastScanner fs=new FastScanner();
		int n=fs.nextInt();
		int[] a=new int[n];
		for (int i=0; i<n; i++) a[i]=fs.nextInt();
		int gcd=0;
		for (int i:a) gcd=gcd(i, gcd);
		int max=a[0];
		for (int i:a) max=Math.max(max, i);
		max/=gcd;
		int moves=max-n;
		if (moves%2==0) {
			System.out.println("Bob");
		}
		else {
			System.out.println("Alice");
		}

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

	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");

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

		}

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

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

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

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

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

/*

 */
public class A {

	public static void main(String[] a) throws InterruptedException {
		Thread t=new Thread(null, null, "!", 1<<28) {
			public void run() {
				new A();
			}
		};
		t.start();
		t.join();
	}

	public A() {
		FastScanner fs=new FastScanner();
		int T=fs.nextInt();
		for (int tt=0; tt<T; tt++) {
			int h=fs.nextInt(), m=fs.nextInt();
			h++;
			int ans=60-m;
			ans+=(60*(24-h));
			System.out.println(ans);
		}
	}

	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreElements())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			return st.nextToken();
		}

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

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

}


import java.util.*;
public class newyear {
    public static void main (String[]args){
        Scanner scan = new Scanner(System.in);
        int i = scan.nextInt();
        for (int ii=0; ii<i; ii++){
            int h = scan.nextInt();
            int m = scan.nextInt();
            int sum = 0;
            if(h<=24&&h>=0&&m<60){
                h=23-h;
                int x =h;
                m=60-m;
                x*=60;
                sum+=x+m;
                System.out.println(sum);

            }

        }
    }
}


import java.util.*;
import java.io.*;
public class Main
{
	public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int sum=0;
int n=sc.nextInt();
while(n--!=0)
{
    int h=sc.nextInt();
    int m=sc.nextInt();
    sum=((23-h)*60)+(60-m);
    System.out.println(sum);
}
}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.StringTokenizer;

public class TaskA {
    public static String doMain(Reader reader) throws IOException {
        MyReader in = new MyReader(reader);
        int tests = in.nextInt();
        StringBuilder sb = new StringBuilder();
        for (int t = 0; t < tests; ++t){
            int h = in.nextInt();
            int m = in.nextInt();
            int ans = 0;
            ans += 60*(24-h)-m;
            sb.append(ans).append("\n");
        }
        return sb.toString();
    }

    public static void main(String[] args) throws IOException {
        String result = doMain(new InputStreamReader(System.in));
        System.out.println(result);
    }

    static class MyReader {
        BufferedReader bf;

        StringTokenizer st;

        String last;

        MyReader(Reader reader) throws IOException {
            bf = new BufferedReader(reader);
            readNextLine();
        }

        String nextToken() throws IOException {
            while (!st.hasMoreTokens()) {
                readNextLine();
            }
            return st.nextToken();
        }

        void readNextLine() throws IOException {
            last = bf.readLine();
            if (last == null) last = "";
            st = new StringTokenizer(last);
        }

        String nextLine() throws IOException {
            String s = last;
            readNextLine();
            return s;
        }

        long nextLong() throws IOException {
            return Long.parseLong(nextToken());
        }

        int nextInt() throws IOException {
            return Integer.parseInt(nextToken());
        }

        double nextDouble() throws IOException {
            return Double.parseDouble(nextToken());
        }

        int[] readIntArray(int n) throws IOException {
            int[] answer = new int[n];
            for (int i = 0; i < n; ++i) {
                answer[i] = nextInt();
            }
            return answer;
        }

        long[] readLongArray(int n) throws IOException {
            long[] answer = new long[n];
            for (int i = 0; i < n; ++i) {
                answer[i] = nextLong();
            }
            return answer;
        }

        double[] readDoubleArray(int n) throws IOException {
            double[] answer = new double[n];
            for (int i = 0; i < n; ++i) {
                answer[i] = nextDouble();
            }
            return answer;
        }
    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Ahmad
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskA solver = new TaskA();
        solver.solve(1, in, out);
    }

    static class TaskA {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int x = in.nextInt();
            while (x-- != 0) {
                int h = in.nextInt();
                int m = in.nextInt();
                System.out.println(((24 * 60) - (h * 60) - m) + "\n");
            }
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Ahmad
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskA solver = new TaskA();
        try {
            int testNumber = 1;
            while (true)
                solver.solve(testNumber++, in, out);
        } catch (UnknownError e) {
            out.close();
        }
    }

    static class TaskA {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int x = in.nextInt();
            while (x-- != 0) {
                int h = in.nextInt();
                int m = in.nextInt();
                System.out.println(((24 * 60) - (h * 60) - m) + "\n");
            }
        }

    }
}


import java.util.*;

import java.util.*;

public class ArrayList {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            System.out.println((60*(23-sc.nextInt()))+60-sc.nextInt());

        }
    }

}


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner s=new Scanner(System.in);
		int n=s.nextInt();
		while(n-->0) {
			System.out.println((60*(23-s.nextInt()))+60-s.nextInt());
		}
	}
}


import java.util.Scanner;

public class A {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();

        for (int i = 0; i < n; i++) {
            int hh = scanner.nextInt();
            int mm = scanner.nextInt();

            System.out.println((23-hh) * 60 + (60 - mm));
        }
    }
}


package org.egzi.codeforces.r611;

import java.util.Scanner;

public class A {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();

        for (int i = 0; i < n; i++) {
            int hh = scanner.nextInt();
            int mm = scanner.nextInt();

            System.out.println((23-hh) * 60 + (60 - mm));
        }
    }
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e5 + 2;
char s[maxn*10];
int c[maxn];

int main(){
	int n;
	scanf("%d",&n);
	memset(c,0,sizeof(c));
	for(int i = 0; i < n; i++){
		scanf("%s",s);
		for(int j = 0; s[j] != '\0'; j++){
			c[i] ^= (1 << (s[j] - 'a'));
		}
	}
	sort(c,c+n);
	ll ans = 0;
	for(int i = 0; i < n; i++){
		ans += upper_bound(c, c + n, c[i]) - lower_bound(c, c + n, c[i]) - 1;
		for(int j = 0; j < 26; j++){
			ans += upper_bound(c, c + n, c[i] ^ (1 << j)) - lower_bound(c, c + n, c[i] ^ (1 << j));
		}
	}
	cout << ans / 2 << endl;
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e5 + 2;
char s[maxn*10];
int c[maxn];

int main(){
	int n;
	scanf("%d",&n);
	memset(c,0,sizeof(c));
	for(int i = 0; i < n; i++){
		scanf("%s",s);
		for(int j = 0; s[j] != '\0'; j++){
			c[i] ^= (1 << (s[j] - 'a'));
		}
	}
	sort(c,c+n);
	int ans = 0;
	for(int i = 0; i < n; i++){
		ans += upper_bound(c, c + n, c[i]) - lower_bound(c, c + n, c[i]) - 1;
		for(int j = 0; j < 26; j++){
			ans += upper_bound(c, c + n, c[i] ^ (1 << j)) - lower_bound(c, c + n, c[i] ^ (1 << j));
		}
	}
	cout << ans / 2 << endl;
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e5 + 2;
char s[maxn*10];
int cunt[maxn][26];

int main(){
	memset(cunt,0,sizeof(cunt));
	int n;
	scanf("%d",&n);
	for(int i = 0; i < n; i++){
		scanf("%s",s);
		for(int j = 0; s[j] != '\0'; j++){
			cunt[i][s[j]-'a']++;
		}
	}
	int ans = 0;
	for(int i = 0; i < n; i++){
		for(int j = i + 1; j < n; j++){
			int odd = 0;
			for(int k = 0; k < 26; k++){
				if((cunt[i][k] + cunt[j][k])&1)odd++;
				if(odd > 1)break;
			}
			if(odd <= 1)ans++;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int vis[30];
map<string,int>mp;
vector<string>v;
int main()
{
    ll ans;
    int n,i,j,x=0;
    cin>>n;
    for(i=0;i<n;i++)
    {
        memset(vis,0,sizeof(vis));
        string s,ss="";
        cin>>s;
        int len=s.size();
        for(j=0;j<len;j++)
            vis[s[j]-'a']++;
        for(j=0;j<26;j++)
        {
            if(vis[j]%2==1)ss+=char(j+'a');
            vis[j]=0;
        }
        if(ss.size()==0)x++;
        else
        {
            mp[ss]++;
            if(mp[ss]==1)v.push_back(ss);
        }
        //cout<<ss<<endl;
        //cout<<mp[ss]<<endl;
    }
    //cout<<mp["b"]<<endl;
    ans=x*(x-1)/2;
    //cout<<ans<<endl;
    for(i=0;i<v.size();i++)
    {
        string s1=v[i],s2;
        if(s1.size()==1)
        {
            ans+=x*mp[s1];
            continue;
        }
        ans+=mp[s1]*(mp[s1]-1)/2;
        //cout<<ans<<endl;
        for(j=0;j<s1.size();j++)
        {
            s2=s1;
            s2.erase(j,1);
            if(mp[s2]>0)ans+=mp[s2]*mp[s1];
        }
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int vis[30];
map<string,int>mp;
vector<string>v;
int main()
{
    ll ans;
    int n,i,j,x=0;
    cin>>n;
    for(i=0;i<n;i++)
    {
        memset(vis,0,sizeof(vis));
        string s,ss="";
        cin>>s;
        int len=s.size();
        for(j=0;j<len;j++)
            vis[s[j]-'a']++;
        for(j=0;j<26;j++)
        {
            if(vis[j]%2==1)ss+=char(j+'a');
            vis[j]=0;
        }
        if(ss.size()==0)x++;
        else
        {
            mp[ss]++;
            if(mp[ss]==1)v.push_back(ss);
        }
        //cout<<ss<<endl;
        //cout<<mp[ss]<<endl;
    }
    //cout<<mp["b"]<<endl;
    ans=x*(x-1)/2;
    //cout<<ans<<endl;
    for(i=0;i<v.size();i++)
    {
        string s1=v[i],s2;
        if(s1.size()==1)ans+=x*mp[s1];
        ans+=mp[s1]*(mp[s1]-1)/2;
        //cout<<ans<<endl;
        for(j=0;j<s1.size();j++)
        {
            s2=s1;
            s2.erase(j,1);
            if(mp[s2]>0)ans+=mp[s2]*mp[s1];
        }
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll vis[30];
map<string,ll>mp;
vector<string>v;
int main()
{
    ll ans;
    ll n,i,j,x=0;
    cin>>n;
    for(i=0;i<n;i++)
    {
        memset(vis,0,sizeof(vis));
        string s,ss="";
        cin>>s;
        ll len=s.size();
        for(j=0;j<len;j++)
            vis[s[j]-'a']++;
        for(j=0;j<26;j++)
        {
            if(vis[j]%2==1)ss+=char(j+'a');
            vis[j]=0;
        }
        if(ss.size()==0)x++;
        else
        {
            mp[ss]++;
            if(mp[ss]==1)v.push_back(ss);
        }
        //cout<<ss<<endl;
        //cout<<mp[ss]<<endl;
    }
    //cout<<mp["b"]<<endl;
    ans=x*(x-1)/2;
    //cout<<ans<<endl;
    for(i=0;i<v.size();i++)
    {
        string s1=v[i],s2;
        if(s1.size()==1)ans+=x*mp[s1];
        ans+=mp[s1]*(mp[s1]-1)/2;
        //cout<<ans<<endl;
        for(j=0;j<s1.size();j++)
        {
            s2=s1;
            s2.erase(j,1);
            if(mp[s2]>0)ans+=mp[s2]*mp[s1];
        }
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int vis[30];
map<string,int>mp;
vector<string>v;
int main()
{
    ll ans;
    int n,i,j,x=0;
    cin>>n;
    for(i=0;i<n;i++)
    {
        memset(vis,0,sizeof(vis));
        string s,ss="";
        cin>>s;
        int len=s.size();
        for(j=0;j<len;j++)
            vis[s[j]-'a']++;
        for(j=0;j<26;j++)
        {
            if(vis[j]%2==1)ss+=char(j+'a');
            vis[j]=0;
        }
        if(ss.size()==0)x++;
        else
        {
            mp[ss]++;
            if(mp[ss]==1)v.push_back(ss);
        }
        //cout<<ss<<endl;
        //cout<<mp[ss]<<endl;
    }
    //cout<<mp["b"]<<endl;
    ans=x*(x-1)/2;
    //cout<<ans<<endl;
    for(i=0;i<v.size();i++)
    {
        string s1=v[i],s2;
        if(s1.size()==1)ans+=x*mp[s1];
        ans+=mp[s1]*(mp[s1]-1)/2;
        //cout<<ans<<endl;
        for(j=0;j<s1.size();j++)
        {
            s2=s1;
            s2.erase(i,1);
            if(mp[s2]>0)ans+=mp[s2]*mp[s1];
        }
    }
    cout<<ans<<endl;
}


// luogu-judger-enable-o2
// Author -- Frame

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<iostream>
#include<unordered_map>
#include<chrono>

#define lowbit(x) ((x)&(-x))
#define Finline __inline__ __attribute__ ((always_inline))
#define DEBUG printf("Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

const int inf=0x3f3f3f3f,Inf=0x7fffffff;
const ll INF=0x7fffffffffffffff;
const double eps=1e-8;

uint seed=19260817;
Finline uint Rand(){return seed=seed*998244353u+1000000007u;}
template <typename _Tp>_Tp gcd(const _Tp &a,const _Tp &b){return (!b)?a:gcd(b,a%b);}
template <typename _Tp>Finline _Tp abs(const _Tp &a){return a>0?a:-a;}
template <typename _Tp>Finline _Tp max(const _Tp &a,const _Tp &b){return a<b?b:a;}
template <typename _Tp>Finline _Tp min(const _Tp &a,const _Tp &b){return a<b?a:b;}
template <typename _Tp>Finline void chmax(_Tp &a,const _Tp &b){(a<b)&&(a=b);}
template <typename _Tp>Finline void chmin(_Tp &a,const _Tp &b){(a>b)&&(a=b);}
template <typename _Tp>Finline bool _cmp(const double &a,const double &b){return abs(a-b)<=eps;}
template <typename _Tp>Finline void read(_Tp& x)
{
    register char ch(getchar());
    bool f(false);
    while(ch<48||ch>57) f|=ch==45,ch=getchar();
    x=ch&15,ch=getchar();
    while(ch>=48&&ch<=57) x=(((x<<2)+x)<<1)+(ch&15),ch=getchar();
    if(f) x=-x;
}
template <typename _Tp,typename... Args>Finline void read(_Tp &t,Args &...args)
{
    read(t);read(args...);
}
Finline int read_str(char *s)
{
    register char ch(getchar());
    while(ch<65||ch>122||(ch>90&&ch<97)) ch=getchar();
    register char *tar=s;
    *tar=ch,ch=getchar();
    while((ch>=65&&ch<=90)||(ch>=97&&ch<=122)) *(++tar)=ch,ch=getchar();
    return tar-s+1;
}

const int N=1000005;
struct custom_hash{
    Finline static uint64_t splitmix64(uint64_t x)
    {
        x+=0x9e3779b97f4a7c15;
        x=(x^(x>>30))*0xbf58476d1ce4e5b9;
        x=(x^(x>>27))*0x94d049bb133111eb;
        return x^(x>>31);
    }
    Finline size_t operator()(uint64_t x)const
    {
        static const uint64_t FIXED_RANDOM=std::chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x+FIXED_RANDOM);
    }
};
std::unordered_map<ll,int> cnt;
char s[N];
int main()
{
	int n;
	read(n);
	ll ans=0;
	for(int i=1;i<=n;++i)
	{
		int len=read_str(s+1);
		int tmp=0;
		for(int j=1;j<=len;++j)
		{
			tmp^=(1<<(s[j]-97));
		}
		if(cnt.find(tmp)!=cnt.end())
		{
			ans+=cnt[tmp];
		}
		for(int i=0;i<25;++i)
		{
			if(cnt.find(tmp^(1<<i))!=cnt.end())
			{
				ans+=cnt[tmp^(1<<i)];
			}
		}
		++cnt[tmp];
	}
	printf("%lld\n",ans);
    return 0;
}


// luogu-judger-enable-o2
// Author -- Frame

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<iostream>
#include<unordered_map>
#include<chrono>

#define lowbit(x) ((x)&(-x))
#define Finline __inline__ __attribute__ ((always_inline))
#define DEBUG printf("Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

const int inf=0x3f3f3f3f,Inf=0x7fffffff;
const ll INF=0x7fffffffffffffff;
const double eps=1e-8;

uint seed=19260817;
Finline uint Rand(){return seed=seed*998244353u+1000000007u;}
template <typename _Tp>_Tp gcd(const _Tp &a,const _Tp &b){return (!b)?a:gcd(b,a%b);}
template <typename _Tp>Finline _Tp abs(const _Tp &a){return a>0?a:-a;}
template <typename _Tp>Finline _Tp max(const _Tp &a,const _Tp &b){return a<b?b:a;}
template <typename _Tp>Finline _Tp min(const _Tp &a,const _Tp &b){return a<b?a:b;}
template <typename _Tp>Finline void chmax(_Tp &a,const _Tp &b){(a<b)&&(a=b);}
template <typename _Tp>Finline void chmin(_Tp &a,const _Tp &b){(a>b)&&(a=b);}
template <typename _Tp>Finline bool _cmp(const double &a,const double &b){return abs(a-b)<=eps;}
template <typename _Tp>Finline void read(_Tp& x)
{
    register char ch(getchar());
    bool f(false);
    while(ch<48||ch>57) f|=ch==45,ch=getchar();
    x=ch&15,ch=getchar();
    while(ch>=48&&ch<=57) x=(((x<<2)+x)<<1)+(ch&15),ch=getchar();
    if(f) x=-x;
}
template <typename _Tp,typename... Args>Finline void read(_Tp &t,Args &...args)
{
    read(t);read(args...);
}
Finline int read_str(char *s)
{
    register char ch(getchar());
    while(ch<65||ch>122||(ch>90&&ch<97)) ch=getchar();
    register char *tar=s;
    *tar=ch,ch=getchar();
    while((ch>=65&&ch<=90)||(ch>=97&&ch<=122)) *(++tar)=ch,ch=getchar();
    return tar-s+1;
}

const int N=1000005;
struct custom_hash{
    Finline static uint64_t splitmix64(uint64_t x)
    {
        x+=0x9e3779b97f4a7c15;
        x=(x^(x>>30))*0xbf58476d1ce4e5b9;
        x=(x^(x>>27))*0x94d049bb133111eb;
        return x^(x>>31);
    }
    Finline size_t operator()(uint64_t x)const
    {
        static const uint64_t FIXED_RANDOM=std::chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x+FIXED_RANDOM);
    }
};
std::unordered_map<ll,int> cnt;
char s[N];
int main()
{
	int n;
	read(n);
	ll ans=0;
	for(int i=1;i<=n;++i)
	{
		int len=read_str(s+1);
		ll tmp=0;
		for(int j=1;j<=len;++j)
		{
			tmp^=(1<<(s[j]-97));
		}
		if(cnt.find(tmp)!=cnt.end())
		{
			ans+=cnt[tmp];
		}
		for(int i=0;i<26;++i)
		{
			if(cnt.find(tmp^(1<<i))!=cnt.end())
			{
				ans+=cnt[tmp^(1<<i)];
			}
		}
		++cnt[tmp];
	}
	printf("%lld\n",ans);
    return 0;
}


// luogu-judger-enable-o2
// Author -- Frame

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<iostream>

#define lowbit(x) ((x)&(-x))
#define Finline __inline__ __attribute__ ((always_inline))
#define DEBUG printf("Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

const int inf=0x3f3f3f3f,Inf=0x7fffffff;
const ll INF=0x7fffffffffffffff;
const double eps=1e-8;

uint seed=19260817;
Finline uint Rand(){return seed=seed*998244353u+1000000007u;}
template <typename _Tp>_Tp gcd(const _Tp &a,const _Tp &b){return (!b)?a:gcd(b,a%b);}
template <typename _Tp>Finline _Tp abs(const _Tp &a){return a>0?a:-a;}
template <typename _Tp>Finline _Tp max(const _Tp &a,const _Tp &b){return a<b?b:a;}
template <typename _Tp>Finline _Tp min(const _Tp &a,const _Tp &b){return a<b?a:b;}
template <typename _Tp>Finline void chmax(_Tp &a,const _Tp &b){(a<b)&&(a=b);}
template <typename _Tp>Finline void chmin(_Tp &a,const _Tp &b){(a>b)&&(a=b);}
template <typename _Tp>Finline bool _cmp(const double &a,const double &b){return abs(a-b)<=eps;}
template <typename _Tp>Finline void read(_Tp& x)
{
    register char ch(getchar());
    bool f(false);
    while(ch<48||ch>57) f|=ch==45,ch=getchar();
    x=ch&15,ch=getchar();
    while(ch>=48&&ch<=57) x=(((x<<2)+x)<<1)+(ch&15),ch=getchar();
    if(f) x=-x;
}
template <typename _Tp,typename... Args>Finline void read(_Tp &t,Args &...args)
{
    read(t);read(args...);
}
Finline int read_str(char *s)
{
    register char ch(getchar());
    while(ch<65||ch>122||(ch>90&&ch<97)) ch=getchar();
    register char *tar=s;
    *tar=ch,ch=getchar();
    while((ch>=65&&ch<=90)||(ch>=97&&ch<=122)) *(++tar)=ch,ch=getchar();
    return tar-s+1;
}

const int N=1000005;
int cnt[67108867];
char s[N];
int main()
{
	int n;
	read(n);
	ll ans=0;
	for(int i=1;i<=n;++i)
	{
		int len=read_str(s+1);
		int tmp=0;
		for(int j=1;j<=len;++j)
		{
			tmp^=(1<<(s[j]-97));
		}
		ans+=cnt[tmp];
		for(int i=0;i<25;++i)
		{
			ans+=cnt[tmp^(1<<i)];
		}
		++cnt[tmp];
	}
	printf("%lld\n",ans);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#pragma gcc diagnostic warning "-std=c++11"

int n;
string s;
long long ans;
unordered_map <int, int> cnt;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> n;
  while (n--) {
    cin >> s;
    int mask = 0;
    for (char c : s) {
      mask ^= 1 << (c - 'a');
    }
    if (cnt.find(mask) != cnt.end()) {
      ans += cnt[mask];
    }
    for (int i = 0; i < 26; ++i) {
      if (cnt.find(mask ^ 1 << i) != cnt.end()) {
        ans += cnt[mask ^ 1 << i];
      }
    }
    ++cnt[mask];
  }
  cout << ans << '\n';
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#pragma gcc diagnostic warning "-std=c++11"

const int M = (1 << 26) + 5;

string s;
int n, cnt[M];
long long ans;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> n;
  while (n--) {
    cin >> s;
    int mask = 0;
    for (char c : s) {
      mask ^= 1 << (c - 'a');
    }
    ans += cnt[mask];
    for (int i = 0; i < 26; ++i) {
      ans += cnt[mask ^ 1 << i];
    }
    ++cnt[mask];
  }
  cout << ans << '\n';
  return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define LL long long
#define DD double
#define Pb push_back
#define Bp pop_back
#define Pf push_front
#define Fp pop_front
#define Ub upper_bound
#define Lb lower_bound
#define In insert
#define Mp make_pair
#define All(x) x.begin(), x.end()
#define mem(a, b) memset(a, b, sizeof(a))
#define fast ios_base::sync_with_stdio(0);cin.tie(0)
#define X first
#define Y second
#define mx1 10
#define mx2 110
#define mx3 1010
#define mx4 10010
#define mx5 100010
#define mx6 1000010

typedef vector<int> Vi;
typedef vector<DD> Vd;
typedef vector<bool> Vb;
typedef vector<Vi> VVi;
typedef pair<int, int> Pii;
typedef pair<DD, DD> Pdd;
typedef vector<Pii> Vpi;
typedef vector<Pdd> Vpd;
typedef queue<int> Qi;
typedef stack<int> Si;
typedef deque<int> Di;

int _toggle(int N, int pos) {return N = N ^ (1 << pos);}
int _set(int N, int pos) {return N = N | (1 << pos);}
int _reset(int N, int pos) {return N = N & ~(1 << pos);}
bool _check(int N, int pos) {return (bool)(N & (1 << pos));}
bool _upper(char a) {return a >= 'A' && a <= 'Z';}
bool _lower(char a) {return a >= 'a' && a <= 'z';}
bool _digit(char a) {return a >= '0' && a <= '9';}

int dx[] = {1, -1, 0, 0, -1, -1, 1, 1};
int dy[] = {0, 0, 1, -1, -1, 1, -1, 1};

///**************************DONE****************************///

int cnt[67110000];

int main()
{
    fast;
    int n;
    cin >> n;

    LL ans = 0;
    for(int i = 0; i < n; i++){
        string s;
        cin >> s;

        int a[27];
        mem(a, 0);

        int L = s.size();
        for(int j = 0; j < L; j++)
            a[s[j] - 'a']++;

        int mask = 0;
        for(int j = 0; j < 26; j++){
            a[j] %= 2;
            if(a[j])
                mask = _set(mask, j);
        }

        ans += cnt[mask];

        for(int j = 0; j < 26; j++)
            ans += cnt[_toggle(mask, j)];

        cnt[mask]++;
    }

    cout << ans << '\n';
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define LL long long
#define DD double
#define Pb push_back
#define Bp pop_back
#define Pf push_front
#define Fp pop_front
#define Ub upper_bound
#define Lb lower_bound
#define In insert
#define Mp make_pair
#define All(x) x.begin(), x.end()
#define mem(a, b) memset(a, b, sizeof(a))
#define fast ios_base::sync_with_stdio(0);cin.tie(0)
#define X first
#define Y second
#define mx1 10
#define mx2 110
#define mx3 1010
#define mx4 10010
#define mx5 100010
#define mx6 1000010

typedef vector<int> Vi;
typedef vector<DD> Vd;
typedef vector<bool> Vb;
typedef vector<Vi> VVi;
typedef pair<int, int> Pii;
typedef pair<DD, DD> Pdd;
typedef vector<Pii> Vpi;
typedef vector<Pdd> Vpd;
typedef queue<int> Qi;
typedef stack<int> Si;
typedef deque<int> Di;

int _toggle(int N, int pos) {return N = N ^ (1 << pos);}
int _set(int N, int pos) {return N = N | (1 << pos);}
int _reset(int N, int pos) {return N = N & ~(1 << pos);}
bool _check(int N, int pos) {return (bool)(N & (1 << pos));}
bool _upper(char a) {return a >= 'A' && a <= 'Z';}
bool _lower(char a) {return a >= 'a' && a <= 'z';}
bool _digit(char a) {return a >= '0' && a <= '9';}

int dx[] = {1, -1, 0, 0, -1, -1, 1, 1};
int dy[] = {0, 0, 1, -1, -1, 1, -1, 1};

///**************************DONE****************************///

int main()
{
    fast;
    int n;
    cin >> n;

    LL ans = 0;
    map<int, int> cnt;
    for(int i = 0; i < n; i++){
        string s;
        cin >> s;

        int a[27];
        mem(a, 0);

        int L = s.size();
        for(int j = 0; j < L; j++)
            a[s[j] - 'a']++;

        int mask = 0;
        for(int j = 0; j < 26; j++){
            a[j] %= 2;
            if(a[j])
                mask = _set(mask, j);
        }

        ans += cnt[mask];

        for(int j = 0; j < 26; j++)
            ans += cnt[_toggle(mask, j)];

        cnt[mask]++;
    }

    cout << ans << '\n';
    return 0;
}


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

int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int n;
    cin >> n;
    map <int, int> cnt;
    for (int i = 0; i < n; ++i) {
        string a;
        cin >> a;
        int msk = 0;
        for(auto c : a) {
            msk ^= 1 << (c - 'a');
        }
        cnt[msk]++;
    }
    int ans = 0;
    map <int, int> cnt2 = cnt;
    for(auto pair : cnt) {
        int msk = pair.first;
        int cn = pair.second;
        ans += ((cn - 1) * cn);

        for (int i = 0; i < 26; ++i) {
            int nmask = msk ^ (1 << i);
            int how = cnt2[nmask];
            ans += how * cn;
        }
    }
    cout << ans / 2 << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int n;
    cin >> n;
    map <ll, ll> cnt;
    for (int i = 0; i < n; ++i) {
        string a;
        cin >> a;
        int msk = 0;
        for(auto c : a) {
            msk ^= 1 << (c - 'a');
        }
        cnt[msk]++;
    }
    ll ans = 0;
    map <ll, ll> cnt2 = cnt;
    for(auto pair : cnt) {
        ll msk = pair.first;
        ll cn = pair.second;
        ans += ((cn - 1) * cn);

        for (int i = 0; i < 26; ++i) {
            ll nmask = msk ^ (1 << i);
            ll how = cnt2[nmask];
            ans += how * cn;
        }
    }
    cout << ans / 2 << endl;
    return 0;
}


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

long long n,f[26],sum,ans;
string s;
bitset<26>a;
map<long long,long long>m;
int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin>>n;
	f[0]=1;
	for(int i=1;i<26;i++)
	{
		f[i]=f[i-1]*2;
	}
	for(int i=1;i<=n;i++)
	{
		cin>>s;
		sum=0;
		for(int j=0;j<s.length();j++)
		{
			a[s[j]-'a']=!a[s[j]-'a'];
		}
		for(int j=0;j<26;j++)
		{
			sum+=f[j]*a[j];
			a[j]=0;
		}
		ans+=m[sum];
		for(int j=0;j<26;j++)
		{
			ans+=m[sum^f[j]];
		}
		m[sum]++;
	}
	cout<<ans<<'\n';
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define pll pair<long long,long long>
using namespace std;

long long n,m;
pll p[105];
bitset<105>b;
int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>p[i].fi;
		p[i].se=i;
	}
	sort(p+1,p+1+n);
	for(int i=1;i<=n;i++)
	{
		b[p[i].se]=i%2;
	}
	for(int i=1;i<=n;i++)
	{
		cout<<b[i]<<" ";
	}
	cout<<'\n';
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <unordered_map>

using namespace std;
const int maxn = 100005, maxl = 1000005;

int n;
long long ans;
char str[maxl];
int bs[maxn];
unordered_map<int, int> num;
unordered_map<int, int>::iterator it, jt, tt;

int main()
{
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        scanf("%s", str);
        int len = strlen(str);
        for(int j = 0; j < len; j++) {
            bs[i] ^= (1 << (str[j]-'a'));
        }
        num[bs[i]]++;
        //cout << num[bs[i]] << endl;
    }

    //for(int i = 0; i < n; i++) {
//        for(int j = i + 1; j < n; j++) {
//            int t = (bs[i]^bs[j]);
//            int cnt = 0;
//            while(t) {
//                t = t&(t-1);
//                cnt++;
//                if(cnt >= 2)
//                    break;
//            }
//            if(cnt < 2)
//                ans++;
//        }
    for(it = num.begin(); it != num.end(); it++) {
        //tt = it;
        int temp = it->second;
        ans += (long long)temp * (temp - 1);
//        for(jt = ++tt; jt != num.end(); jt++) {
//            int t = (it->first^jt->first);
//            int cnt = 0;
//            while(t) {
//                t = t&(t-1);
//                cnt++;
//                if(cnt >= 2)
//                    break;
//            }
//            if(cnt < 2)
//                ans += it->second * jt->second;
        int s = it->first;
        for(int i = 0; i < 26; i++) {
            int t = s ^ (1 << i);
            tt = num.find(t);
            if(tt != num.end())
                ans += (long long)temp * num[t];
        }
    }
    printf("%lld\n", (ans >> 1));
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <unordered_map>

using namespace std;
const int maxn = 100005, maxl = 1000005;

int n;
long long ans;
char str[maxl];
int bs[maxn];
unordered_map<int, int> num;
unordered_map<int, int>::iterator it, jt, tt;

int main()
{
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        scanf("%s", str);
        int len = strlen(str);
        for(int j = 0; j < len; j++) {
            bs[i] ^= (1 << (str[j]-'a'));
        }
        num[bs[i]]++;
        //cout << num[bs[i]] << endl;
    }

    //for(int i = 0; i < n; i++) {
//        for(int j = i + 1; j < n; j++) {
//            int t = (bs[i]^bs[j]);
//            int cnt = 0;
//            while(t) {
//                t = t&(t-1);
//                cnt++;
//                if(cnt >= 2)
//                    break;
//            }
//            if(cnt < 2)
//                ans++;
//        }
    for(it = num.begin(); it != num.end(); it++) {
        //tt = it;
        int temp = it->second;
        ans += temp * (temp - 1);
//        for(jt = ++tt; jt != num.end(); jt++) {
//            int t = (it->first^jt->first);
//            int cnt = 0;
//            while(t) {
//                t = t&(t-1);
//                cnt++;
//                if(cnt >= 2)
//                    break;
//            }
//            if(cnt < 2)
//                ans += it->second * jt->second;
        int s = it->first;
        for(int i = 0; i < 26; i++) {
            int t = s ^ (1 << i);
            tt = num.find(t);
            if(tt != num.end())
                ans += temp * num[t];
        }
    }
    printf("%lld\n", (ans >> 1));
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second

const int N = 2e5 + 7;

int test, n, m, u, v, deg[N];
vector <int> edge[N], rv[N], b, w, sv;

mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> test;
	while(test--){
		cin >> n >> m;
		for(int i = 1; i <= n; i++){
			edge[i].clear();
			rv[i].clear();
			deg[i] = 0;
		}
		b.clear();
		w.clear();
		sv.clear();
		for(int i = 1; i <= m; i++){
			cin >> u >> v;
			edge[u].pb(v);
			rv[v].pb(u);
		}
		for(int i = 1; i <= n; i++){
			int mx = 0, mi = 0;
			for(int p : rv[i]){
				mx = max(mx, deg[p]);
				mi = min(mi, deg[p]);
			}
			if(mx == 2 && mi == -2){
				sv.pb(i);
			}
			else if(mx == 2){
				w.pb(i);
				deg[i] = mi - 1;
			}
			else if(mi == -2){
				b.pb(i);
				deg[i] = mx + 1;
			}
			else if(mx > abs(mi)){
				w.pb(i);
				deg[i] = mi - 1;
			}
			else if(mx < abs(mi)){
				b.pb(i);
				deg[i] = mx + 1;
			}
			else{
				int t = rd() % 2;
				if(t == 0){
					b.pb(i);
					deg[i] = mx + 1;
				}
				else{
					w.pb(i);
					deg[i] = mi - 1;
				}
			}
		}
		//cout << (int)sv.size() << endl;
		if((int)b.size() > (int)w.size()) swap(b, w);
		cout << (int)b.size() + (int)sv.size() << "\n";
		for(int u : b) cout << u << " ";
		for(int u : sv) cout << u << " ";
		cout << "\n";
	}
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second

const int N = 2e5 + 7;

int test, n, m, u, v, deg[N];
vector <int> edge[N], rv[N], b, w, sv;

mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> test;
	while(test--){
		cin >> n >> m;
		for(int i = 1; i <= n; i++){
			edge[i].clear();
			rv[i].clear();
			deg[i] = 0;
		}
		b.clear();
		w.clear();
		sv.clear();
		for(int i = 1; i <= m; i++){
			cin >> u >> v;
			edge[u].pb(v);
			rv[v].pb(u);
		}
		for(int i = 1; i <= n; i++){
			int mx = 0, mi = 0;
			for(int p : rv[i]){
				mx = max(mx, deg[p]);
				mi = min(mi, deg[p]);
			}
			if(mx == 2 && mi == -2){
				sv.pb(i);
			}
			else if(mx == 2){
				w.pb(i);
				deg[i] = mi - 1;
			}
			else if(mi == -2){
				b.pb(i);
				deg[i] = mx + 1;
			}
			else if(mx == 1){
				w.pb(i);
				deg[i] = mi - 1;
			}
			else if(mi == -1){
				b.pb(i);
				deg[i] = mx + 1;
			}
			else{
				int t = rd() % 2;
				if(t == 0){
					b.pb(i);
					deg[i] = mx + 1;
				}
				else{
					w.pb(i);
					deg[i] = mi - 1;
				}
			}
		}
		//cout << (int)sv.size() << endl;
		if((int)b.size() > (int)w.size()) swap(b, w);
		cout << (int)b.size() + (int)sv.size() << "\n";
		for(int u : b) cout << u << " ";
		for(int u : sv) cout << u << " ";
		cout << "\n";
	}
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second

const int N = 2e5 + 7;

int test, n, m, u, v, deg[N], mx[N], mi[N];
vector <int> edge[N], rv[N], b, w, sv;

mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> test;
	while(test--){
		cin >> n >> m;
		for(int i = 1; i <= n; i++){
			edge[i].clear();
			rv[i].clear();
			deg[i] = mx[i] = mi[i] = 0;
		}
		b.clear();
		w.clear();
		sv.clear();
		for(int i = 1; i <= m; i++){
			cin >> u >> v;
			edge[u].pb(v);
			rv[v].pb(u);
		}
		for(int i = 1; i <= n; i++){
			if(mx[i] == 2 && mi[i] == -2){
				sv.pb(i);
				deg[i] = 0;
				continue;
			}
			if(mx[i] == 2){
				deg[i] = mi[i] - 1;
				w.pb(i);
				for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
				continue;
			}
			else if(mi[i] == -2){
				deg[i] = mx[i] + 1;
				b.pb(i);
				for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
				continue;
			}
			if(mx[i] == 0 && mi[i] == 0){
				deg[i] = 1;
				b.pb(i);
				for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
				continue;
			}
			int MX = 0, MI = 0;
			for(int v : edge[i]){
				MX = max(MX, mx[v]);
				MI = min(MI, mi[v]);
			}
			if(MX == 2 && MI > -2){
				deg[i] = mx[i] + 1;
				b.pb(i);
			}
			else if(MX < 2 && MI == -2){
				deg[i] = mi[i] - 1;
				w.pb(i);
			}
			else if(mx[i] + 1 <= MX){
				deg[i] = mx[i] + 1;
				b.pb(i);
			}
			else if(mi[i] - 1 >= MI){
				deg[i] = mi[i] - 1;
				w.pb(i);
			}
			else{
				deg[i] = mx[i] + 1;
				b.pb(i);
			}
			for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
		}
		if((int)b.size() > (int)w.size()) swap(b, w);
		int sz = (int)b.size() + (int)sv.size();
		if(sz * 7 > n * 4){
			b.clear();
			w.clear();
			sv.clear();
			for(int i = 1; i <= n; i++){
				deg[i] = mx[i] = mi[i] = 0;
			}
			for(int i = 1; i <= n; i++){
				if(mx[i] == 2 && mi[i] == -2){
					sv.pb(i);
					deg[i] = 0;
					continue;
				}
				if(mx[i] == 2){
					deg[i] = mi[i] - 1;
					w.pb(i);
					for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
					continue;
				}
				else if(mi[i] == -2){
					deg[i] = mx[i] + 1;
					b.pb(i);
					for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
					continue;
				}
				if(mx[i] == 0 && mi[i] == 0){
					deg[i] = -1;
					w.pb(i);
					for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
					continue;
				}
				int MX = 0, MI = 0;
				for(int v : edge[i]){
					MX = max(MX, mx[v]);
					MI = min(MI, mi[v]);
				}
				if(MX == 2 && MI > -2){
					deg[i] = mx[i] + 1;
					b.pb(i);
				}
				else if(MX < 2 && MI == -2){
					deg[i] = mi[i] - 1;
					w.pb(i);
				}
				else if(mx[i] + 1 <= MX){
					deg[i] = mx[i] + 1;
					b.pb(i);
				}
				else if(mi[i] - 1 >= MI){
					deg[i] = mi[i] - 1;
					w.pb(i);
				}
				else{
					deg[i] = mi[i] - 1;
					w.pb(i);
				}
				for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
			}
		}
		if((int)b.size() > (int)w.size()) swap(b, w);
		sz = (int)b.size() + (int)sv.size();
		if(sz * 7 > n * 4){
			b.clear();
			w.clear();
			sv.clear();
			for(int i = 1; i <= n; i++){
				deg[i] = mx[i] = mi[i] = 0;
			}
			for(int i = 1; i <= n; i++){
				if(mx[i] == 2 && mi[i] == -2){
					sv.pb(i);
					deg[i] = 0;
					continue;
				}
				if(mx[i] == 2){
					deg[i] = mi[i] - 1;
					w.pb(i);
					for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
					continue;
				}
				else if(mi[i] == -2){
					deg[i] = mx[i] + 1;
					b.pb(i);
					for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
					continue;
				}
				if(mx[i] == 0 && mi[i] == 0){
					int pp = rd() % 2;
					if(pp == 0){
						b.pb(i);
						deg[i] = 1;
					}
					else{
						w.pb(i);
						deg[i] = -1;
					}
					for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
					continue;
				}
				int MX = 0, MI = 0;
				for(int v : edge[i]){
					MX = max(MX, mx[v]);
					MI = min(MI, mi[v]);
				}
				if(MX == 2 && MI > -2){
					deg[i] = mx[i] + 1;
					b.pb(i);
				}
				else if(MX < 2 && MI == -2){
					deg[i] = mi[i] - 1;
					w.pb(i);
				}
				else if(mx[i] + 1 <= MX){
					deg[i] = mx[i] + 1;
					b.pb(i);
				}
				else if(mi[i] - 1 >= MI){
					deg[i] = mi[i] - 1;
					w.pb(i);
				}
				else{
					int pp = rd() % 3;
					if(pp == 0){
						b.pb(i);
						deg[i] = mx[i] + 1;
					}
					else{
						w.pb(i);
						deg[i] = mi[i] -1;
					}
				}
				for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
			}
		}
		if((int)b.size() > (int)w.size()) swap(b, w);
		sz = (int)b.size() + (int)sv.size();
		assert(sz * 7 <= n * 4);
		cout << (int)b.size() + (int)sv.size() << "\n";
		for(int u : b) cout << u << " ";
		for(int u : sv) cout << u << " ";
		cout << "\n";
	}
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second

const int N = 2e5 + 7;

int test, n, m, u, v, deg[N], mx[N], mi[N];
vector <int> edge[N], rv[N], b, w, sv;

mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> test;
	while(test--){
		cin >> n >> m;
		for(int i = 1; i <= n; i++){
			edge[i].clear();
			rv[i].clear();
			deg[i] = mx[i] = mi[i] = 0;
		}
		b.clear();
		w.clear();
		sv.clear();
		for(int i = 1; i <= m; i++){
			cin >> u >> v;
			edge[u].pb(v);
			rv[v].pb(u);
		}
		for(int i = 1; i <= n; i++){
			if(mx[i] == 2 && mi[i] == -2){
				sv.pb(i);
				deg[i] = 0;
				continue;
			}
			if(mx[i] == 2){
				deg[i] = mi[i] - 1;
				w.pb(i);
				for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
				continue;
			}
			else if(mi[i] == -2){
				deg[i] = mx[i] + 1;
				b.pb(i);
				for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
				continue;
			}
			if(mx[i] == 0 && mi[i] == 0){
				deg[i] = 1;
				b.pb(i);
				for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
				continue;
			}
			int MX = 0, MI = 0;
			for(int v : edge[i]){
				MX = max(MX, mx[v]);
				MI = min(MI, mi[v]);
			}
			if(MX == 2 && MI > -2){
				deg[i] = mx[i] + 1;
				b.pb(i);
			}
			else if(MX < 2 && MI == -2){
				deg[i] = mi[i] - 1;
				w.pb(i);
			}
			else if(mx[i] + 1 <= MX){
				deg[i] = mx[i] + 1;
				b.pb(i);
			}
			else if(mi[i] - 1 >= MI){
				deg[i] = mi[i] - 1;
				w.pb(i);
			}
			else{
				deg[i] = mx[i] + 1;
				b.pb(i);
			}
			for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
		}
		if((int)b.size() > (int)w.size()) swap(b, w);
		int sz = (int)b.size() + (int)sv.size();
		if(sz * 7 > n * 4){
			b.clear();
			w.clear();
			sv.clear();
			for(int i = 1; i <= n; i++){
				deg[i] = mx[i] = mi[i] = 0;
			}
			for(int i = 1; i <= n; i++){
				if(mx[i] == 2 && mi[i] == -2){
					sv.pb(i);
					deg[i] = 0;
					continue;
				}
				if(mx[i] == 2){
					deg[i] = mi[i] - 1;
					w.pb(i);
					for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
					continue;
				}
				else if(mi[i] == -2){
					deg[i] = mx[i] + 1;
					b.pb(i);
					for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
					continue;
				}
				if(mx[i] == 0 && mi[i] == 0){
					deg[i] = -1;
					w.pb(i);
					for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
					continue;
				}
				int MX = 0, MI = 0;
				for(int v : edge[i]){
					MX = max(MX, mx[v]);
					MI = min(MI, mi[v]);
				}
				if(MX == 2 && MI > -2){
					deg[i] = mx[i] + 1;
					b.pb(i);
				}
				else if(MX < 2 && MI == -2){
					deg[i] = mi[i] - 1;
					w.pb(i);
				}
				else if(mx[i] + 1 <= MX){
					deg[i] = mx[i] + 1;
					b.pb(i);
				}
				else if(mi[i] - 1 >= MI){
					deg[i] = mi[i] - 1;
					w.pb(i);
				}
				else{
					deg[i] = mi[i] - 1;
					w.pb(i);
				}
				for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
			}
		}
		if((int)b.size() > (int)w.size()) swap(b, w);
		sz = (int)b.size() + (int)sv.size();
		while(sz * 7 > n * 4){
			b.clear();
			w.clear();
			sv.clear();
			for(int i = 1; i <= n; i++){
				deg[i] = mx[i] = mi[i] = 0;
			}
			for(int i = 1; i <= n; i++){
				if(mx[i] == 2 && mi[i] == -2){
					sv.pb(i);
					deg[i] = 0;
					continue;
				}
				if(mx[i] == 2){
					deg[i] = mi[i] - 1;
					w.pb(i);
					for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
					continue;
				}
				else if(mi[i] == -2){
					deg[i] = mx[i] + 1;
					b.pb(i);
					for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
					continue;
				}
				if(mx[i] == 0 && mi[i] == 0){
					int pp = rd() % 2;
					if(pp == 0){
						b.pb(i);
						deg[i] = 1;
					}
					else{
						w.pb(i);
						deg[i] = -1;
					}
					for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
					continue;
				}
				int MX = 0, MI = 0;
				for(int v : edge[i]){
					MX = max(MX, mx[v]);
					MI = min(MI, mi[v]);
				}
				if(MX == 2 && MI > -2){
					deg[i] = mx[i] + 1;
					b.pb(i);
				}
				else if(MX < 2 && MI == -2){
					deg[i] = mi[i] - 1;
					w.pb(i);
				}
				else if(mx[i] + 1 <= MX){
					deg[i] = mx[i] + 1;
					b.pb(i);
				}
				else if(mi[i] - 1 >= MI){
					deg[i] = mi[i] - 1;
					w.pb(i);
				}
				else{
					int pp = rd() % 3;
					if(pp == 0){
						b.pb(i);
						deg[i] = mx[i] + 1;
					}
					else{
						w.pb(i);
						deg[i] = mi[i] -1;
					}
				}
				for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
			}
			if((int)b.size() > (int)w.size()) swap(b, w);
			sz = (int)b.size() + (int)sv.size();
		}
		assert(sz * 7 <= n * 4);
		cout << (int)b.size() + (int)sv.size() << "\n";
		for(int u : b) cout << u << " ";
		for(int u : sv) cout << u << " ";
		cout << "\n";
	}
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second

const int N = 2e5 + 7;

int test, n, m, u, v, deg[N], mx[N], mi[N];
vector <int> edge[N], rv[N], b, w, sv;

mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> test;
	while(test--){
		cin >> n >> m;
		for(int i = 1; i <= n; i++){
			edge[i].clear();
			rv[i].clear();
			deg[i] = mx[i] = mi[i] = 0;
		}
		b.clear();
		w.clear();
		sv.clear();
		for(int i = 1; i <= m; i++){
			cin >> u >> v;
			edge[u].pb(v);
			rv[v].pb(u);
		}
		for(int i = 1; i <= n; i++){
			if(mx[i] == 2 && mi[i] == -2){
				sv.pb(i);
				deg[i] = 0;
				continue;
			}
			if(mx[i] == 2){
				deg[i] = mi[i] - 1;
				w.pb(i);
				for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
				continue;
			}
			else if(mi[i] == -2){
				deg[i] = mx[i] + 1;
				b.pb(i);
				for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
				continue;
			}
			if(mx[i] == 0 && mi[i] == 0){
				deg[i] = 1;
				b.pb(i);
				for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
				continue;
			}
			int MX = 0, MI = 0;
			for(int v : edge[i]){
				MX = max(MX, mx[v]);
				MI = min(MI, mi[v]);
			}
			if(MX == 2 && MI > -2){
				deg[i] = mx[i] + 1;
				b.pb(i);
			}
			else if(MX < 2 && MI == -2){
				deg[i] = mi[i] - 1;
				w.pb(i);
			}
			else if(mx[i] + 1 <= MX){
				deg[i] = mx[i] + 1;
				b.pb(i);
			}
			else if(mi[i] - 1 >= MI){
				deg[i] = mi[i] - 1;
				w.pb(i);
			}
			else{
				deg[i] = mx[i] + 1;
				b.pb(i);
			}
			for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
		}
		//cout << (int)sv.size() << endl;
		if((int)b.size() > (int)w.size()) swap(b, w);
		cout << (int)b.size() + (int)sv.size() << "\n";
		for(int u : b) cout << u << " ";
		for(int u : sv) cout << u << " ";
		cout << "\n";
	}
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second

const int N = 2e5 + 7;

int test, n, m, u, v, deg[N], mx[N], mi[N];
vector <int> edge[N], rv[N], b, w, sv;

mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> test;
	while(test--){
		cin >> n >> m;
		for(int i = 1; i <= n; i++){
			edge[i].clear();
			rv[i].clear();
			deg[i] = mx[i] = mi[i] = 0;
		}
		b.clear();
		w.clear();
		sv.clear();
		for(int i = 1; i <= m; i++){
			cin >> u >> v;
			edge[u].pb(v);
			rv[v].pb(u);
		}
		for(int i = 1; i <= n; i++){
			if(mx[i] == 2 && mi[i] == -2){
				sv.pb(i);
				deg[i] = 0;
				continue;
			}
			if(mx[i] == 2){
				deg[i] = mi[i] - 1;
				w.pb(i);
				for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
				continue;
			}
			else if(mi[i] == -2){
				deg[i] = mx[i] + 1;
				b.pb(i);
				for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
				continue;
			}
			if(mx[i] == 0 && mi[i] == 0){
				deg[i] = 1;
				b.pb(i);
				for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
				continue;
			}
			int MX = 0, MI = 0;
			for(int v : edge[i]){
				MX = max(MX, mx[v]);
				MI = min(MI, mi[v]);
			}
			if(MX == 2 && MI > -2){
				deg[i] = mx[i] + 1;
				b.pb(i);
			}
			else if(MX < 2 && MI == -2){
				deg[i] = mi[i] - 1;
				w.pb(i);
			}
			else if(mx[i] + 1 <= MX){
				deg[i] = mx[i] + 1;
				b.pb(i);
			}
			else if(mi[i] - 1 >= MI){
				deg[i] = mi[i] - 1;
				w.pb(i);
			}
			else{
				deg[i] = mx[i] + 1;
				b.pb(i);
			}
			for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
		}
		if((int)b.size() > (int)w.size()) swap(b, w);
		int sz = (int)b.size() + (int)sv.size();
		if(sz * 7 > n * 4){
			b.clear();
			w.clear();
			sv.clear();
			for(int i = 1; i <= n; i++){
				deg[i] = mx[i] = mi[i] = 0;
			}
			for(int i = 1; i <= n; i++){
				if(mx[i] == 2 && mi[i] == -2){
					sv.pb(i);
					deg[i] = 0;
					continue;
				}
				if(mx[i] == 2){
					deg[i] = mi[i] - 1;
					w.pb(i);
					for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
					continue;
				}
				else if(mi[i] == -2){
					deg[i] = mx[i] + 1;
					b.pb(i);
					for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
					continue;
				}
				if(mx[i] == 0 && mi[i] == 0){
					deg[i] = -1;
					w.pb(i);
					for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
					continue;
				}
				int MX = 0, MI = 0;
				for(int v : edge[i]){
					MX = max(MX, mx[v]);
					MI = min(MI, mi[v]);
				}
				if(MX == 2 && MI > -2){
					deg[i] = mx[i] + 1;
					b.pb(i);
				}
				else if(MX < 2 && MI == -2){
					deg[i] = mi[i] - 1;
					w.pb(i);
				}
				else if(mx[i] + 1 <= MX){
					deg[i] = mx[i] + 1;
					b.pb(i);
				}
				else if(mi[i] - 1 >= MI){
					deg[i] = mi[i] - 1;
					w.pb(i);
				}
				else{
					deg[i] = mi[i] - 1;
					w.pb(i);
				}
				for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
			}
		}
		if((int)b.size() > (int)w.size()) swap(b, w);
		sz = (int)b.size() + (int)sv.size();
		if(sz * 7 > n * 4){
			b.clear();
			w.clear();
			sv.clear();
			for(int i = 1; i <= n; i++){
				deg[i] = mx[i] = mi[i] = 0;
			}
			for(int i = 1; i <= n; i++){
				if(mx[i] == 2 && mi[i] == -2){
					sv.pb(i);
					deg[i] = 0;
					continue;
				}
				if(mx[i] == 2){
					deg[i] = mi[i] - 1;
					w.pb(i);
					for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
					continue;
				}
				else if(mi[i] == -2){
					deg[i] = mx[i] + 1;
					b.pb(i);
					for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
					continue;
				}
				if(mx[i] == 0 && mi[i] == 0){
					int pp = rd() % 2;
					if(pp == 0){
						b.pb(i);
						deg[i] = 1;
					}
					else{
						w.pb(i);
						deg[i] = -1;
					}
					for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
					continue;
				}
				int MX = 0, MI = 0;
				for(int v : edge[i]){
					MX = max(MX, mx[v]);
					MI = min(MI, mi[v]);
				}
				if(MX == 2 && MI > -2){
					deg[i] = mx[i] + 1;
					b.pb(i);
				}
				else if(MX < 2 && MI == -2){
					deg[i] = mi[i] - 1;
					w.pb(i);
				}
				else if(mx[i] + 1 <= MX){
					deg[i] = mx[i] + 1;
					b.pb(i);
				}
				else if(mi[i] - 1 >= MI){
					deg[i] = mi[i] - 1;
					w.pb(i);
				}
				else{
					int pp = rd() % 2;
					if(pp == 0){
						b.pb(i);
						deg[i] = mx[i] + 1;
					}
					else{
						w.pb(i);
						deg[i] = mi[i] -1;
					}
				}
				for(int v : edge[i]) mi[v] = min(mi[v], deg[i]), mx[v] = max(mx[v], deg[i]);
			}
		}
		cout << (int)b.size() + (int)sv.size() << "\n";
		for(int u : b) cout << u << " ";
		for(int u : sv) cout << u << " ";
		cout << "\n";
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX=2e5+5;

int main(){
     int i, j, k;
     int T;
     scanf("%d", &T);
     while(T--){
          int n, m;
          scanf("%d %d", &n, &m);
          vector< vector<int> > adj(n+1);
          for(i=0; i<m; ++i){
               int u, v;
               scanf("%d %d", &u, &v);
               adj[u].push_back(v);
          }
          for(i=1; i<=n; ++i) sort(adj[i].begin(), adj[i].end());
          vector< int > ans, chk(n+1, 0);
          for(i=1; i<=n; ++i){
               if(chk[i]) continue;
               for(auto x:adj[i]){
                    if(chk[x]) continue;
                    for(auto y:adj[x]){
                         if(chk[y]) continue;
                         chk[y]=1;
                         ans.push_back(y);
                    }
               }
          }
          printf("%d\n", int(ans.size()));
          for(auto x:ans) printf("%d ", x);
          puts("");
     }
}


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

int main(){
     int i, j, k;
     int T;
     scanf("%d", &T);
     while(T--){
          int n, m;
          scanf("%d %d", &n, &m);
          vector< vector<int> > adj(n+1);
          vector< int > in(n+1, 0), dis(n+1, 0);
          for(i=0; i<m; ++i){
               int u, v;
               scanf("%d %d", &u, &v);
               adj[u].push_back(v);
               in[v]++;
          }
          vector< int > ans, can, chk(n+1, 0);
          for(i=1; i<=n; ++i) if(in[i]==0) can.push_back(i);
          while(!can.empty()){
               vector< int > nxt;
               for(auto x:can){
                    for(auto y:adj[x]){
                         dis[y]=max(dis[y], 1+dis[x]);
                         dis[y]%=3;
                         if(!chk[y] && dis[y]==2) ans.push_back(y), chk[y]=1;
                         nxt.push_back(y);
                         in[y]--;
                    }
               }
               can.clear();
               for(auto x:nxt) if(in[x]==0) can.push_back(x);
          }
          printf("%d\n", int(ans.size()));
          for(auto x:ans) printf("%d ", x);
          puts("");
     }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX=2e5+5;

int main(){
     int i, j, k;
     int T;
     scanf("%d", &T);
     while(T--){
          int n, m;
          scanf("%d %d", &n, &m);
          vector< vector<int> > adj(n+1);
          for(i=0; i<m; ++i){
               int u, v;
               scanf("%d %d", &u, &v);
               adj[u].push_back(v);
          }
          vector< int > ans, chk(n+1, 0);
          for(i=1; i<=n; ++i){
               if(chk[i]) continue;
               for(auto x:adj[i]){
                    if(chk[x]) continue;
                    for(auto y:adj[x]){
                         if(chk[y]) continue;
                         chk[y]=1;
                         ans.push_back(y);
                    }
               }
          }
          printf("%d\n", int(ans.size()));
          for(auto x:ans) printf("%d ", x);
          puts("");
     }
}


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

int main(){
     int i, j, k;
     int T;
     scanf("%d", &T);
     while(T--){
          int n, m;
          scanf("%d %d", &n, &m);
          vector< vector<int> > adj(n+1), par(n+1);
          vector< int > in(n+1, 0);
          for(i=0; i<m; ++i){
               int u, v;
               scanf("%d %d", &u, &v);
               adj[u].push_back(v);
               par[v].push_back(u);
               in[v]++;
          }
          vector< int > ans, can, chk(n+1, 0), order;
          for(i=1; i<=n; ++i) if(in[i]==0) can.push_back(i);
          while(!can.empty()){
               vector< int > nxt;
               for(auto x:can){
                    order.push_back(x);
                    for(auto y:adj[x]){
                         nxt.push_back(y);
                         in[y]--;
                    }
               }
               can.clear();
               sort(nxt.begin(), nxt.end());
               nxt.erase(unique(nxt.begin(), nxt.end()), nxt.end());
               for(auto x:nxt) if(in[x]==0) can.push_back(x);
          }
          for(auto x:order){
               int rem=0;
               for(auto y:par[x]){
                    if(chk[y]) continue;
                    for(auto z:par[y]){
                         if(!chk[z]) rem=1;
                    }
               }
               if(rem) ans.push_back(x), chk[x]=1;
          }
          printf("%d\n", int(ans.size()));
          for(auto x:ans) printf("%d ", x);
          puts("");
     }
}