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

namespace Peti_Zadatak
{
    class Jednacina
    {
        long A, B, X, Y, t, d;
        long[] Niz = new long[100];
        int j = 0;
        public Jednacina()
        {
            string[] input = Console.ReadLine().Split(' ');
            A = Convert.ToInt64(input[0]);
            B = Convert.ToInt64(input[1]);
            Niz[0] = 1;
            t = A;
        }
        public long Rez()
        {
            while (t != 1)
            {
                Delioci();
            }
            for (int i = 0; i < j; i++)
            {
                X = Niz[i];
                Y = A / Niz[i];
                d = X ^ Y;
                if (d == B)
                {
                    return X;
                }
            }
            return -1;
        }
        public void Delioci()
        {
            if (t > 10000) { Niz[j] = VelikiD(); t /= Niz[j]; j++; }
            else MaliD();
        }
        public long VelikiD()
        {
            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 MaliD()
        {
            for (int i = 1; i <= Math.Sqrt(t); i += 2)
            {
                if (t % i == 0)
                {
                    Niz[j] = i;
                    j++;
                }
            }
            t = 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 (t == Niz[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)
        {
            Jednacina[] X;
            int T = Convert.ToInt32(Console.ReadLine());
            long[] t = new long[T];
            X = new Jednacina[T];
            for (int i = 0; i < T; i++)
            {
                X[i] = new Jednacina();
            }
            for (int i = 0; i < T; i++)
            {
                t[i] = X[i].Rez();
            }
            for (int i = 0; i < T; i++)
            {
                X[i].Ispisi(t[i]);
            }
        }
    }
}