#include <bits/stdc++.h>
using namespace std;
int dekadniSistem(string s)
{
    int n=s.size(),j=0,k=0;
    for(int i=n-1;i>=0;i--)
    {
        k+=(s.at(i)-'0')*pow(2,j);
        j++;
    }
    return k;
}
string binarniSistem(int n)
{
    string binarni=" ";
    int i=0;
    while (n>0)
    {
        binarni+=(n%2)+'0';
        n=n/2;
        i++;
    }
    reverse(binarni.begin(),binarni.end());
    return binarni;
}
string xori(int x,int y)
{
    string s1,s2;
    s1=binarniSistem(x);
    s2=binarniSistem(y);
    int n,m;
    n=s1.size();
    m=s2.size();
    if(n>m)
    {
        string s3=" ";
        for(int i=0;i<n-m;i++)
            s3+=0+'0';
        s3+=s2;
        s2=s3;
        m=n;
    }
    else if(m>n)
    {
        string s3=" ";
        for(int i=0;i<m-n;i++)
            s3+=0+'0';
        s3+=s1;
        s1=s3;
        n=m;
    }
    string resenje=" ";
    for(int i=0;i<n-1;i++)
    {
        resenje+=((s1.at(i)-'0')+(s2.at(i+1)-'0'))+'0';
        if(resenje.at(i)==2+'0')
            resenje.at(i)=0+'0';
    }
    resenje.erase(0,1);
    return resenje;
}
int main()
{
    int t;
    cin>>t;
    bool p[t]={false};
    int a[t],b[t];
    for(int i=0;i<t;i++)
        cin>>a[i]>>b[i];
    for(int i=0;i<t;i++)
    {
        for(int j=1;j<sqrt(a[i]);j++)
        {
            if(a[i]%j==0)
                if(dekadniSistem(xori(j,a[i]/j))==b[i])
                {
                    b[i]=a[i]/j;
                    a[i]=j;
                    p[i]=true;
                }
        }
    }
    for(int i=0;i<t;i++)
    {
        if(p[i])
            cout<<a[i]<<" "<<b[i]<<endl;
        else
            cout<<-1<<endl;
    }
    return 0;
}