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

namespace Zmaj
{
    class Uradi
    {
        long A, B, X, Y, temp, d;
        long[] arrayA = new long[100];
        int j = 0;

        public Uradi()
        {
            string[] input = Console.ReadLine().Split(' ');
            A = Convert.ToInt64(input[0]);
            B = Convert.ToInt64(input[1]);
            arrayA[0] = 1;
            temp = A;
        }

        public long Resenje()
        {
            while (temp != 1)
            {
                Delioci();
            }

            for (int i = 0; i < j; i++)
            {
                X = arrayA[i];
                Y = A / arrayA[i];
                d = X ^ Y;

                if (d == B)
                {
                    return X;
                }
            }
            return -1;
        }

        public void Delioci()
        {
            if (temp > 10000) { arrayA[j] = VelikiDelilac(); temp /= arrayA[j]; j++; }
            else MaliDelilac();
        }

        public long VelikiDelilac()
        {
            BigInteger a = new BigInteger(2), b = new BigInteger(2);
            long p;

            do
            {
                a = (a * a + 20) % (BigInteger)A;
                b = (b * b + 20) % (BigInteger)A;
                b = (b * b + 20) % (BigInteger)A;
                p = NZD((BigInteger)A, (a - b > 0) ? a - b : b - a);
                if (p > 1 && Nije(p)) return p;
            } 
            while (b != a);
                return 0;
        }

        public void MaliDelilac()
        {
            for (int i = 1; i <= Math.Sqrt(temp); i += 2)
            {
                if (temp % i == 0)
                {
                    arrayA[j] = i;
                    j++;
                }
            }

            temp = 1;
        }

        public void Ispisi(long k)
        {
            if (k == -1) Console.WriteLine(k);
            else Console.WriteLine(k + " " + A / k);
        }

        private bool Nije(long temp)
        {
            for (int i = 0; i < j; i++)
            {
                if (temp == arrayA[i]) return false;
            }

            return true;
        }

        private long NZD(BigInteger a, BigInteger b)
        {
            while (a != 0 && b != 0)
            {
                if (a > b)
                    a %= b;
                else
                    b %= a;
            }

            if (a == 0)
                return (long)b;
            else
                return (long)a;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Uradi[] X;
            int T = Convert.ToInt32(Console.ReadLine());

            long[] temp = new long[T];
            X = new Uradi[T];

            for (int i = 0; i < T; i++)
            {
                X[i] = new Uradi();
            }

            for (int i = 0; i < T; i++)
            {
                temp[i] = X[i].Resenje();
            }

            for (int i = 0; i < T; i++)
            {
                X[i].Ispisi(temp[i]);
            }
        }
    }
}