#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cmath>
#include <cassert>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <complex>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <utility>
#include <functional>
#include <iterator>

using namespace std;

#define dump(n) cerr<<"# "<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define all(c) begin(c),end(c)
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int,int> pii;

void addtokens(vs& tokens,string& line)
{
	for(int i=0,j=0;i<line.size();i=j){
		if(line[i]==' '){
			j=i+1;
			continue;
		}

		if(line[i]=='"')
			j=line.find('"',i+1)+1;
		else if(line[i]=='(' || line[i]==')' || line[i]==',')
			j=i+1;
		else
			while(j<line.size() && isalpha(line[j]))
				j++;
		tokens.push_back(line.substr(i,j-i));
	}
}

int main()
{
	for(int n;cin>>n;){
		cin.ignore();
		vs lines(n);
		rep(i,n) getline(cin,lines[i]);

		vs tokens;
		rep(i,n)
			addtokens(tokens,lines[i]);

		stack<int> tries;
		int pos=-1;
		for(int i=0;i<tokens.size();){
			if(tokens[i]=="try"){
				tries.push(i);
				i++;
			}
			else if(tokens[i]=="throw"){
				if(pos==-1)
					pos=i;
				i+=4;
			}
			else if(tokens[i]=="catch"){
				if(tries.top()<pos){
					if(tokens[i+2]==tokens[pos+2]){
						cout<<tokens[i+4].substr(1,tokens[i+4].size()-2)<<endl;
						pos=-1;
					}
				}
				tries.pop();
				i+=6;
			}
		}
		if(pos!=-1)
			cout<<"Unhandled Exception"<<endl;
	}

	return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <utility>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <sstream>
#define mp make_pair
#define ll long long
using namespace std;
int n, pr[1<<17], id, en[1<<17];
char cmd[1<<17], s[1<<17];
string s1[1<<17], s2[1<<17], exc;
int st[1<<17], ss;
int main()
{
    scanf("%d\n", &n);
    int top=0;
    for (int x=0; x<n; x++)
    {
        gets(s);
        int i=0;
        while (s[i]==' ') i++;
        if (s[i]=='t' && s[i+1]=='r')
        {
            cmd[x]=1;
        }
        if (s[i]=='t' && s[i+1]=='h')
        {
            id=x;
            i+=5;
            while (s[i]==' ') i++; i++;
            while (s[i]==' ') i++;
            int j=i;
            while (s[j]!=' ' && s[j]!=')' && s[j]!=',') j++;
            exc=string(s+i, s+j);
        }
        if (s[i]=='c')
        {
            cmd[x]=2;
            i+=5;
            while (s[i]==' ') i++; i++;
            while (s[i]==' ') i++;
            int j=i;
            while (s[j]!=' ' && s[j]!=')' && s[j]!=',') j++;
            s1[x]=string(s+i, s+j);
            i=j;
            while (s[i]!='"') i++; j=i+1;
            while (s[j]!='"') j++;
            s2[x]=string(s+i+1, s+j);
        }
    }
    for (int i=0; i<n; i++)
    {
        if (!cmd[i]) continue;
        if (cmd[i]==2)
        {
            ss--;
            en[st[ss]]=i;
        }
        else
        {
            if (ss) pr[i]=st[ss-1]; else pr[i]=-1;
            st[ss++]=i;
        }
    }
    int cp=id;
    while (cp>=0 && cmd[cp]!=1) cp--;
    while (cp>=0)
    {
        int end=en[cp];
        if (s1[end]==exc)
        {
            puts(s2[end].c_str());
            return 0;
        }
        cp=pr[cp];
    }
    puts("Unhandled Exception");
    return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <utility>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <sstream>
#define mp make_pair
#define ll long long
using namespace std;
int n, pr[1<<17], id, en[1<<17];
char cmd[1<<17], s[1<<17];
string s1[1<<17], s2[1<<17], exc;
int st[1<<17], ss;
int main()
{
    scanf("%d\n", &n);
    int top=0;
    for (int x=0; x<n; x++)
    {
        gets(s);
        int i=0;
        while (s[i]==' ') i++;
        if (s[i]=='t' && s[i+1]=='r')
        {
            cmd[x]=1;
        }
        if (s[i]=='t' && s[i+1]=='h')
        {
            id=x;
            i+=5;
            while (s[i]==' ') i++; i++;
            while (s[i]==' ') i++;
            int j=i;
            while (s[j]!=' ' && s[j]!=')' && s[j]!=',') j++;
            exc=string(s+i, s+j);
        }
        if (s[i]=='c')
        {
            cmd[x]=2;
            i+=5;
            while (s[i]==' ') i++; i++;
            while (s[i]==' ') i++;
            int j=i;
            while (s[j]!=' ' && s[j]!=')' && s[j]!=',') j++;
            s1[x]=string(s+i, s+j);
            i=j;
            while (s[i]!='"') i++; j=i+1;
            while (s[j]!='"') j++;
            s2[x]=string(s+i+1, s+j);
        }
    }
    for (int i=0; i<n; i++)
    {
        if (!cmd[i]) continue;
        if (cmd[i]==2)
        {
            ss--;
            en[st[ss]]=i;
        }
        else
        {
            if (ss) pr[i]=st[ss-1]; else pr[i]=-1;
            st[ss++]=i;
        }
    }
    int cp=id;
    while (cp>=0 && (cmd[cp]!=1 || en[cp]<=id)) cp--;
    while (cp>=0)
    {
        int end=en[cp];
        if (s1[end]==exc)
        {
            puts(s2[end].c_str());
            return 0;
        }
        cp=pr[cp];
    }
    puts("Unhandled Exception");
    return 0;
}


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

const int max_n = 1, inf = 1e9 + 100;

bool solve(long long a, long long n, long long p, long long h) {
    if (h == 0) {
        return false;
    }
    if (a * n < p) {
        return h >= a;
    }
    a %= p;
    if (a * n < 1e3) {
        vector<int> v;
        long long cur = a;
        for (int i = 0; i < n; ++i) {
            v.push_back(cur);
            cur += a;
            if (cur > p) {
                cur -= p;
            }
        }
        sort(v.begin(), v.end());
        if (v[0] > h) {
            return false;
        }
        for (int i = 0; i + 1 < n; ++i) {
            if (v[i + 1] - v[i] > h) {
                return false;
            }
        }
        return true;
    }
    long long a1 = min(p % a, a - p % a);
    long long n1 = (n * a) / p;
    long long p1 = a;
    return solve(a1, n1, p1, h);
}

int main()
{
    //freopen("input.txt", "r", stdin);
    int T;
    cin >> T;
    while (T--) {
        int a, n, p, h;
        cin >> a >> n >> p >> h;
        cout << (solve(a, n, p, h) ? "YES" : "NO") << "\n";
    }
    return 0;
}


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

const int max_n = 1, inf = 1e9 + 100;

bool solve(long long a, long long n, long long p, long long h) {
    if (h == 0) {
        return false;
    }
    a %= p;
    if (a * n < p) {
        return h >= a;
    }
    if (a * n < 1e3) {
        vector<int> v;
        long long cur = a;
        for (int i = 0; i < n; ++i) {
            v.push_back(cur);
            cur += a;
            if (cur > p) {
                cur -= p;
            }
        }
        sort(v.begin(), v.end());
        if (v[0] > h) {
            return false;
        }
        for (int i = 0; i + 1 < n; ++i) {
            if (v[i + 1] - v[i] > h) {
                return false;
            }
        }
        return true;
    }
    long long a1 = min(p % a, a - p % a);
    long long n1 = (n * a / p);

    if ((n * a) % p + a < p / a * a) {
        --n1;
    }
    long long p1 = a;
    if (a1 * n1 < p1) {
        return h >= max(a1, p1 - a1 * n1);
    }
    return solve(a1, n1, p1, h);
}

int main()
{
    //freopen("input.txt", "r", stdin);
    int T;
    cin >> T;
    while (T--) {
        int a, n, p, h;
        cin >> a >> n >> p >> h;
        cout << (solve(a, n, p, h) ? "YES" : "NO") << "\n";
    }
    return 0;
}


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

const int max_n = 1, inf = 1e9 + 100;

bool solve(long long a, long long n, long long p, long long h) {
    if (h == 0) {
        return false;
    }
    a %= p;
    if (a * n < p) {
        return h >= a;
    }
    long long a1 = min(p % a, a - p % a);
    long long n1 = (n * a / p);

    if ((n * a) % p + a < p / a * a) {
        --n1;
    }
    long long p1 = a;
    if (a1 * n1 < p1) {
        return h >= max(a1, p1 - a1 * n1);
    }
    return solve(a1, n1, p1, h);
}

int main()
{
    //freopen("input.txt", "r", stdin);
    int T;
    cin >> T;
    while (T--) {
        int a, n, p, h;
        cin >> a >> n >> p >> h;
        cout << (solve(a, n, p, h) ? "YES" : "NO") << "\n";
    }
    return 0;
}


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

const int max_n = 1, inf = 1e9 + 100;

bool solve(long long a, long long n, long long p, long long h) {
    if (h == 0) {
        return false;
    }
    a %= p;
    if (a * n < p) {
        return h >= a;
    }
    if (a * n < 1e3) {
        vector<int> v;
        long long cur = a;
        for (int i = 0; i < n; ++i) {
            v.push_back(cur);
            cur += a;
            if (cur > p) {
                cur -= p;
            }
        }
        sort(v.begin(), v.end());
        if (v[0] > h) {
            return false;
        }
        for (int i = 0; i + 1 < n; ++i) {
            if (v[i + 1] - v[i] > h) {
                return false;
            }
        }
        return true;
    }
    long long a1 = min(p % a, a - p % a);
    long long n1 = (n * a / p);
    long long p1 = a;
    if (a1 * n1 < p1) {
        return h >= max(a1, p1 - a1 * n1);
    }
    return solve(a1, n1, p1, h);
}

int main()
{
    //freopen("input.txt", "r", stdin);
    int T;
    cin >> T;
    while (T--) {
        int a, n, p, h;
        cin >> a >> n >> p >> h;
        cout << (solve(a, n, p, h) ? "YES" : "NO") << "\n";
    }
    return 0;
}


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

const int max_n = 1, inf = 1e9 + 100;

bool solve(long long a, long long n, long long p, long long h, bool fst = true) {
    if (h == 0) {
        return false;
    }
    if (a * n < p) {
        if (fst) {
            return h >= a;
        } else {
            return h >= max(a, p - a * n);
        }
    }
    a %= p;
    if (a * n < 1e3) {
        vector<int> v;
        long long cur = a;
        for (int i = 0; i < n; ++i) {
            v.push_back(cur);
            cur += a;
            if (cur > p) {
                cur -= p;
            }
        }
        sort(v.begin(), v.end());
        if (v[0] > h) {
            return false;
        }
        for (int i = 0; i + 1 < n; ++i) {
            if (v[i + 1] - v[i] > h) {
                return false;
            }
        }
        return true;
    }
    long long a1 = min(p % a, a - p % a);
    long long n1 = (n * a) / p;
    long long p1 = a;
    return solve(a1, n1, p1, h, false);
}

int main()
{
    //freopen("input.txt", "r", stdin);
    int T;
    cin >> T;
    while (T--) {
        int a, n, p, h;
        cin >> a >> n >> p >> h;
        cout << (solve(a, n, p, h) ? "YES" : "NO") << "\n";
    }
    return 0;
}


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

const int max_n = 1, inf = 1e9 + 100;

bool solve(long long a, long long n, long long p, long long h) {
    if (h == 0) {
        return false;
    }
    a %= p;
    if (a * n < p) {
        return h >= a;
    }
    /*if (a * n < 1e3) {
        vector<int> v;
        long long cur = a;
        for (int i = 0; i < n; ++i) {
            v.push_back(cur);
            cur += a;
            if (cur > p) {
                cur -= p;
            }
        }
        sort(v.begin(), v.end());
        if (v[0] > h) {
            return false;
        }
        for (int i = 0; i + 1 < n; ++i) {
            if (v[i + 1] - v[i] > h) {
                return false;
            }
        }
        return true;
    }*/
    long long a1 = min(p % a, a - p % a);
    long long n1 = (n * a / p);
    if ((n * a) % p + a < p) {
        --n1;
    }
    long long p1 = a;
    if (a1 * n1 < p1) {
        return h >= max(a1, p1 - a1 * n1);
    }
    return solve(a1, n1, p1, h);
}

int main()
{
    //freopen("input.txt", "r", stdin);
    int T;
    cin >> T;
    while (T--) {
        int a, n, p, h;
        cin >> a >> n >> p >> h;
        cout << (solve(a, n, p, h) ? "YES" : "NO") << "\n";
    }
    return 0;
}


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

const int max_n = 1, inf = 1e9 + 100;

bool solve(long long a, long long n, long long p, long long h) {
    if (h == 0) {
        return false;
    }
    a %= p;
    if (a * n < p) {
        return h >= a;
    }
    if (a * n < 1e3) {
        vector<int> v;
        long long cur = a;
        for (int i = 0; i < n; ++i) {
            v.push_back(cur);
            cur += a;
            if (cur > p) {
                cur -= p;
            }
        }
        sort(v.begin(), v.end());
        if (v[0] > h) {
            return false;
        }
        for (int i = 0; i + 1 < n; ++i) {
            if (v[i + 1] - v[i] > h) {
                return false;
            }
        }
        return true;
    }
    long long a1 = min(p % a, a - p % a);
    long long n1 = (n * a / p);
    if ((n * a) % p + a < p) {
        --n1;
    }
    long long p1 = a;
    if (a1 * n1 < p1) {
        return h >= max(a1, p1 - a1 * n1);
    }
    return solve(a1, n1, p1, h);
}

int main()
{
    //freopen("input.txt", "r", stdin);
    int T;
    cin >> T;
    while (T--) {
        int a, n, p, h;
        cin >> a >> n >> p >> h;
        cout << (solve(a, n, p, h) ? "YES" : "NO") << "\n";
    }
    return 0;
}


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

const int max_n = 1, inf = 1e9 + 100;

bool solve(long long a, long long n, long long p, long long h, bool fst = true) {
    if (h == 0) {
        return false;
    }
    a %= p;
    if (a * n < p) {
        if (fst) {
            return h >= a;
        } else {
            return h >= max(a, p - a * n);
        }
    }
    if (a * n < 1e3) {
        vector<int> v;
        long long cur = a;
        for (int i = 0; i < n; ++i) {
            v.push_back(cur);
            cur += a;
            if (cur > p) {
                cur -= p;
            }
        }
        sort(v.begin(), v.end());
        if (v[0] > h) {
            return false;
        }
        for (int i = 0; i + 1 < n; ++i) {
            if (v[i + 1] - v[i] > h) {
                return false;
            }
        }
        return true;
    }
    long long a1 = min(p % a, a - p % a);
    long long n1 = (n * a / p);
    long long p1 = a;
    return solve(a1, n1, p1, h, false);
}

int main()
{
    //freopen("input.txt", "r", stdin);
    int T;
    cin >> T;
    while (T--) {
        int a, n, p, h;
        cin >> a >> n >> p >> h;
        cout << (solve(a, n, p, h) ? "YES" : "NO") << "\n";
    }
    return 0;
}


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

const int max_n = 1, inf = 1e9 + 100;

bool solve(long long a, long long n, long long p, long long h, bool fst = true) {
    if (h == 0) {
        return false;
    }
    if (a * n < p) {
        if (fst) {
            return h >= a;
        } else {
            return h >= max(a, p - a * n);
        }
    }
    a %= p;
    if (a * n < 1e3) {
        vector<int> v;
        long long cur = a;
        for (int i = 0; i < n; ++i) {
            v.push_back(cur);
            cur += a;
            if (cur > p) {
                cur -= p;
            }
        }
        sort(v.begin(), v.end());
        if (v[0] > h) {
            return false;
        }
        for (int i = 0; i + 1 < n; ++i) {
            if (v[i + 1] - v[i] > h) {
                return false;
            }
        }
        return true;
    }
    long long a1 = min(p % a, a - p % a);
    long long n1 = (n * a) / p;
    long long p1 = a;
    return solve(a1, n1, p, h, false);
}

int main()
{
    //freopen("input.txt", "r", stdin);
    int T;
    cin >> T;
    while (T--) {
        int a, n, p, h;
        cin >> a >> n >> p >> h;
        cout << (solve(a, n, p, h) ? "YES" : "NO") << "\n";
    }
    return 0;
}


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

const int max_n = 1, inf = 1e9 + 100;

bool solve(long long a, long long n, long long p, long long h) {
    if (h == 0) {
        return false;
    }
    a %= p;
    if (a * n < p) {
        return h >= a;
    }
    if (a * n < 1e3) {
        vector<int> v;
        long long cur = a;
        for (int i = 0; i < n; ++i) {
            v.push_back(cur);
            cur += a;
            if (cur > p) {
                cur -= p;
            }
        }
        sort(v.begin(), v.end());
        if (v[0] > h) {
            return false;
        }
        for (int i = 0; i + 1 < n; ++i) {
            if (v[i + 1] - v[i] > h) {
                return false;
            }
        }
        return true;
    }
    long long a1 = min(p % a, a - p % a);
    long long n1 = (n * a / p);
    long long p1 = p;
    if (a1 * n1 < p1) {
        return h >= max(a1, p1 - a1 * n1);
    }
    return solve(a1, n1, p1, h);
}

int main()
{
    //freopen("input.txt", "r", stdin);
    int T;
    cin >> T;
    while (T--) {
        int a, n, p, h;
        cin >> a >> n >> p >> h;
        cout << (solve(a, n, p, h) ? "YES" : "NO") << "\n";
    }
    return 0;
}


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

const int max_n = 1, inf = 1e9 + 100;

bool solve(long long a, long long n, long long p, long long h, bool fst = true) {
    if (h == 0) {
        return false;
    }
    a %= p;
    if (a * n < p) {
        if (fst) {
            return h >= a;
        } else {
            return h >= max(a, p - a * n);
        }
    }
    if (a * n < 1e3) {
        vector<int> v;
        long long cur = a;
        for (int i = 0; i < n; ++i) {
            v.push_back(cur);
            cur += a;
            if (cur > p) {
                cur -= p;
            }
        }
        sort(v.begin(), v.end());
        if (v[0] > h) {
            return false;
        }
        for (int i = 0; i + 1 < n; ++i) {
            if (v[i + 1] - v[i] > h) {
                return false;
            }
        }
        return true;
    }
    long long a1 = min(p % a, a - p % a);
    long long n1 = n / (p / a);
    long long p1 = a;
    return solve(a1, n1, p1, h, false);
}

int main()
{
    //freopen("input.txt", "r", stdin);
    int T;
    cin >> T;
    while (T--) {
        int a, n, p, h;
        cin >> a >> n >> p >> h;
        cout << (solve(a, n, p, h) ? "YES" : "NO") << "\n";
    }
    return 0;
}


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

#define fi first
#define se second

set < pair <long long, long long> > f;
vector <long long> crt( 100000 );

    ll nextjump( ll a, ll n, ll p)
    {
        int def;
        if ( a * n < p )
        {
            def = max(a, p - a * n);
            return def;
        }

        int cur = a * n / p;
    if ( a * n % p < a * ( p / a - 1 ) ) cur--;
    return nextjump( min ( p % a, a - p % a ), cur, a);
}

ll ans;

int main()
{
	ll m;
	cin >> m;

	ll a;
	ll n;
	ll p;
	ll h;

	for (ll i = 1; i <= m; i++)
    {
		cin >> a >> n >> p >> h;
		a = a % p;
		if ( a * n <= p ) ans = a; else ans = nextjump( a, n, p );
		if ( ans <= h ) cout << "YES" << endl; else cout << "NO" << endl;
	}
	return 0;
}


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

#define fi first
#define se second

set < pair <long long, long long> > f;
vector <long long> crt( 100000 );

    ll nextjump( ll a, ll n, ll p)
    {
        ll def = 0;
        if ( a * n < p )
        {
            def = max(a, p - a * n);
            return def;
        }

        ll cur = a * n / p;

        if ( a * n % p < a * ( p / a - 1 ) ) cur--;
	return nextjump( min ( p % a, a - p % a ), cur, a);
}

ll ans;

int main()
{
	ll m;
	cin >> m;

	ll a;
	ll n;
	ll p;
	ll h;

	for (ll i = 1; i <= m; i++)
    {
		cin >> a >> n >> p >> h;
		a = a % p;
		if ( a * n < p ) ans = a; else ans = nextjump( a, n, p );
		if ( ans <= h ) cout << "YES" << endl; else cout << "NO";
	}
	return 0;
}


#ifdef ONPC
# define _GLIBCXX_DEBUG
#define deb(a) cerr << "yo " << #a << " = " << a << endl;
#else
#define deb(a)
#endif
#include <bits/stdc++.h>
#define endl "\n"
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

typedef long long ll;
typedef long double ld;
ll MOD = 1e9 + 7;

ll get(ll a, ll n, ll p){
    if (a * n < p){
        return max(p - a * n, a);
    }
    ll e = 0;
    if ((a * n % p < a * (p / a - 1))){
        e = 1;
    }
    return get(a - p % a, a * n / p - e, a);
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout << fixed << setprecision(20);
    ll t;
    cin >> t;
    while(t--){
        ll a, n, p, h;
        cin >> a >> n >> p >> h;
        if (a * n < p){
            cout << "NO" << endl;
            continue;
        }
        if (get(a, n, p) < h){
            cout << "YES" << endl;
            continue;
        }
        else{
            cout << "NO" << endl;
            continue;
        }
    }

#ifdef ONPC
    cerr << endl << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
#endif
}
//egor lox


#ifdef ONPC
# define _GLIBCXX_DEBUG
#define deb(a) cerr << "yo " << #a << " = " << a << endl;
#else
#define deb(a)
#endif
#include <bits/stdc++.h>
#define endl "\n"
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

typedef long long ll;
typedef long double ld;
ll MOD = 1e9 + 7;

ll get(ll a, ll n, ll p){
    if (a * n < p){
        return max(p - a * n, a);
    }
    ll e = 0;
    if ((a * n % p < a * (p / a - 1))){
        e = 1;
    }
    return get(a - p % a, a * n / p - e, a);
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout << fixed << setprecision(20);
    ll t;
    cin >> t;
    while(t--){
        ll a, n, p, h;
        cin >> a >> n >> p >> h;
        if (a * n < p){
            cout << "NO" << endl;
            continue;
        }
        if (get(a, n, p) <= h){
            cout << "YES" << endl;
            continue;
        }
        else{
            cout << "NO" << endl;
            continue;
        }
    }

#ifdef ONPC
    cerr << endl << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
#endif
}
//egor lox


#ifdef ONPC
# define _GLIBCXX_DEBUG
#define deb(a) cerr << "yo " << #a << " = " << a << endl;
#else
#define deb(a)
#endif
#include <bits/stdc++.h>
#define endl "\n"
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

typedef long long ll;
typedef long double ld;
ll MOD = 1e9 + 7;

ll get(ll a, ll n, ll p){
    if (a * n < p){
        return max(p - a * n, a);
    }
    ll e = 0;
    if ((a * n % p < a * (p / a - 1))){
        e = 1;
    }
    return get(a - p % a, a * n / p - e, a);
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout << fixed << setprecision(20);
    ll t;
    cin >> t;
    while(t--){
        ll a, n, p, h;
        cin >> a >> n >> p >> h;
        if (a * n < p){
            if (h < a) {
                cout << "NO" << endl;
                continue;
            }
            cout << "YES";
            continue;
        }
        if (get(a, n, p) <= h){
            cout << "YES" << endl;
            continue;
        }
        else{
            cout << "NO" << endl;
            continue;
        }
    }

#ifdef ONPC
    cerr << endl << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
#endif
}
//egor lox


#ifdef ONPC
# define _GLIBCXX_DEBUG
#define deb(a) cerr << "yo " << #a << " = " << a << endl;
#else
#define deb(a)
#endif
#include <bits/stdc++.h>
#define endl "\n"
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

typedef long long ll;
typedef long double ld;
ll MOD = 1e9 + 7;

ll get(ll a, ll n, ll p){
    if (a * n < p){
        return max(p - a * n, a);
    }
    ll e = 0;
    if ((a * n % p < a * (p / a - 1))){
        e = 1;
    }
    return get(a - p % a, a * n / p - e, a);
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout << fixed << setprecision(20);
    ll t;
    cin >> t;
    while(t--){
        ll a, n, p, h;
        cin >> a >> n >> p >> h;
        if (a * n < p){
            if (h < a) {
                cout << "NO" << endl;
                continue;
            }
            cout << "YES" << endl;
            continue;
        }
        if (get(a, n, p) <= h){
            cout << "YES" << endl;
            continue;
        }
        else{
            cout << "NO" << endl;
            continue;
        }
    }

#ifdef ONPC
    cerr << endl << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
#endif
}
//egor lox


#ifdef ONPC
# define _GLIBCXX_DEBUG
#define deb(a) cerr << "yo " << #a << " = " << a << endl;
#else
#define deb(a)
#endif
#include <bits/stdc++.h>
#define endl "\n"
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

typedef long long ll;
typedef long double ld;
ll MOD = 1e9 + 7;

ll get(ll a, ll n, ll p){
    if (a * n < p){
        return max(p - a * n, a);
    }
    ll e = 0;
    if ((a * n % p < a * (p / a - 1))){
        e = 1;
    }
    return get(a - p % a, a * n / p - e, a);
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout << fixed << setprecision(20);
    ll t;
    cin >> t;
    while(t--){
        ll a, n, p, h;
        cin >> a >> n >> p >> h;
        a %= p;
        if (a * n < p){
            if (h < a) {
                cout << "NO" << endl;
                continue;
            }
            cout << "YES" << endl;
            continue;
        }
        if (get(a, n, p) <= h){
            cout << "YES" << endl;
            continue;
        }
        else{
            cout << "NO" << endl;
            continue;
        }
    }

#ifdef ONPC
    cerr << endl << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
#endif
}
//egor lox


#include<bits/stdc++.h>
using namespace std;
int test,a,n,p,h;
bool ans;
bool solve(int a,int p,int n)
{
	if (1ll*a*n<p)
	{
		if (max(a,p-a*n)>h) return 0;
		return 1;
	}
	int cr=1ll*a*n/p,ps=1ll*a*n%p;
	if (ps<((p/a)-1)*a) cr--;
	return solve(p%a,a,cr);
}
int main()
{
	scanf("%d",&test);
	while(test--)
	{
		scanf("%d%d%d%d",&a,&n,&p,&h);
		if (1ll*a*n<p)
		{
			if (a<=h) printf("YES\n");
			else printf("NO\n");
			continue;
		}
		ans=solve(a,p,n);
		if (!ans) printf("NO\n");
		else printf("YES\n");
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int test,a,n,p,h;
bool ans;
bool solve(int a,int p,int n)
{
	if (1ll*a*n<p)
	{
		if (max(a,p-a*n)>h) return 0;
		return 1;
	}
	int cr=1ll*a*n/p,ps=1ll*a*n%p;
	if (ps<((p/a)-1)*a) cr--;
	return solve(p%a,a,cr);
}
int main()
{
	scanf("%d",&test);
	while(test--)
	{
		scanf("%d%d%d%d",&a,&n,&p,&h);a%=p;
		if (1ll*a*n<p)
		{
			if (a<=h) printf("YES\n");
			else printf("NO\n");
			continue;
		}
		ans=solve(a,p,n);
		if (!ans) printf("NO\n");
		else printf("YES\n");
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int test,a,n,p,h;
bool ans;
bool solve(int a,int p,int n)
{
if (1ll*a*n<p)
{
if (max(a,p-a*n)>h) return 0;
return 1;
}
int cr=1ll*a*n/p,ps=1ll*a*n%p;
if (ps<((p/a)-1)*a) cr--;
return solve(p%a,a,cr);
}
int main()
{
scanf("%d",&test);
while(test--)
{
scanf("%d%d%d%d",&a,&n,&p,&h);a%=p;
if (1ll*a*n<p)
{
if (a<=h) printf("YES\n");
else printf("NO\n");
continue;
}
ans=solve(a,p,n);
if (!ans) printf("NO\n");
else printf("YES\n");
}
return 0;
}
//aa


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

typedef long long lint;
typedef long double louble;

template<typename T1,typename T2> inline T1 max(T1 a,T2 b){return a<b?b:a;}
template<typename T1,typename T2> inline T1 min(T1 a,T2 b){return a<b?a:b;}

const char lf = '\n';

namespace ae86
{
	const int bufl = 1 << 15;

	char buf[bufl],*s=buf,*t=buf;

	inline int fetch()
	{
		if(s==t){t=(s=buf)+fread(buf,1,bufl,stdin);if(s==t)return EOF;}
		return *s++;
	}

	inline lint ty()
	{
		lint a=0;int b=1,c=fetch();
		while(!isdigit(c))b^=c=='-',c=fetch();
		while(isdigit(c))a=a*10+c-48,c=fetch();
		return b?a:-a;
	}
}
using ae86::ty;

lint cal(lint a,lint n,lint p)
{
	lint b=a*n/p;
	if(b<=0)return max(a,p-a*n);
	if(a*n%p<(p/a-1)*a)b--;
	return cal(a-p%a,b,a);
}

void finder()
{
	lint a=ty(),n=ty(),p=ty(),h=ty();
	a%=p;
	cout<<(((a*n<p?a:cal(a,n,p))>h)?"NO":"YES")<<lf;
}

int main()
{
	ios::sync_with_stdio(0),cout.tie(nullptr);

	int t=ty();
	while(t--)finder();

	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while (t--) {
		long long a, n, p, h, temp = 0, tempn, tempp;
		cin >> a >> n >> p >> h;
		a = a % p;
		bool check = 0;
		while (temp == 0) {
			if (a * n >= p) {
				check = 1;
				tempn = a * (n + 1 - p / a) / p;
				tempp = a;
				a = min(a - p % a, p % a);
				p = tempp;
				n = tempn;
			}
			else {
				temp = a;
				if (check) {
					temp = max(temp, p - n * a);
				}
			}
		}
		if (h >= temp) {
			cout << "YES" << "\n";
		}
		else {
			cout << "NO" << "\n";
		}
	}
}


#include <cstdio>
#include<iostream>
#include <algorithm>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
int t,n,a,p,h;
ll max_dist(ll a,ll n,ll p,int flag){
    if (a*n<p){
        ll res=a;
        if (flag) res=max(res,p-a*n);
        return  res;
    }
    ll sum=a*n/p,tmp=a-p%a;
    ll first=a*(p/a-1);
    if (first>a*n%p) sum--;
    return max_dist(min(tmp,a-tmp),sum,a,1);
}
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>a>>n>>p>>h;
        a%=p;
        if (max_dist(a,n,p,0)>h) cout<<"NO\n";
         else cout<<"YES\n";
    }

}


#include <cstdio>
#include<iostream>
#include <algorithm>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
int T,N,A,P,H;
ll max_dist(ll a,ll n,ll p,int had){
    if (a*n<p){
        ll ans=a;
        if (had) ans=max(ans,p-a*n);
        return  ans;
    }
    ll sum=a*n/p,tmp=a-p%a;
    ll first=a*(p/a-1);
    if (first>a*n%p) sum--;
    return max_dist(min(tmp,a-tmp),sum,a,1);
}
int main()
{
    cin>>T;
    while(T--)
    {
        cin>>A>>N>>P>>H;
        A%=P;
        if (max_dist(A,N,P,0)>H) cout<<"NO\n"; else cout<<"YES\n";
    }

}


#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
int t, a, n, p, h;
ll x, y;

int maxdis(ll x, ll n, ll s)
{
	if (x>(s>>1))
		x=s-x;
	if (x*n>s) return maxdis(s % x,(x*n+s % x)/s, x);
	else return max(x, s-x*(n-1));
}

int main()
{
	int i,j;
	scanf("%d", &t);
	for (i=0; i<t; i++)
	{
		scanf("%d%d%d%d", &a, &n, &p, &h);
		a=a % p;
		int maxd=((ll)a*n<p)?a:maxdis(a%p, n+1, p);
		if (maxd<=h)
			printf("YES\n");
		else
			printf("NO\n");
	}
}


#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
int t, a, n, p, h;
ll x, y;
int v[10000];

int exgcd(ll a, ll b)
{
	if (!b)
	{
		x=1;y=0;
		return a;
	}
	ll g=exgcd(b, a % b);
	ll t=x;
	//x=(y % p+p)%p;y=((t-a/b*y)%p+p)% p;
	x=y;y=t-a/b*y;
	return g;
}
int main()
{
	int i,j;
	scanf("%d", &t);
	for (i=0; i<t; i++)
	{
		scanf("%d%d%d%d", &a, &n, &p, &h);
		exgcd(a, p);
		x=(x%p+p)%p;
		int sum=0, now=0, cnt=0;
		for (j=1; 1<=p; j++)
		{
			now=(x+now) % p;
			if (now<=n)
			{
				sum=0;
				cnt++;
				if (cnt==n) break;
			}
			else
			{
				sum++;
				if (sum>=h) break;
			}
		}
		if (cnt==n)
			printf("YES\n");
		else
			printf("NO\n");
			/*
		int sum=0, now=0, cnt=0;
		for (j=1; j<=n; j++)
		{
			now=(now+a) % p;
			v[j]=now;
		}
		sort(v,v+n+1);
		for (j=1; j<=n; j++)
			if (v[j]-v[j-1]>h)
				cnt=1;
		if (cnt==0)
			printf("YES\n");
		else
			printf("NO\n");
			*/
	}
}


#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
int t, a, n, p, h, x, y;

int exgcd(ll a, ll b)
{
	if (!b)
	{
		x=1;y=0;
		return a;
	}
	ll g=exgcd(b, a % b);
	ll t=x;
	x=(y % p+p)%p;y=((t-a/b*y)%p+p)% p;
	return g;
}
int main()
{
	int i,j;
	scanf("%d", &t);
	for (i=0; i<t; i++)
	{
		scanf("%d%d%d%d", &a, &n, &p, &h);
		exgcd(a, p);
		int sum=0, now=0, cnt=0;
		for (j=1; 1<=p; j++)
		{
			now=(x+now) % p;
			if (now<=n)
			{
				sum=0;
				cnt++;
				if (cnt==n) break;
			}
			else
			{
				sum++;
				if (sum>=h) break;
			}
		}
		if (cnt==n)
			printf("YES\n");
		else
			printf("NO\n");
	}
}


#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
int t, a, n, p, h;
ll x, y;
int v[10000];

int exgcd(ll a, ll b)
{
	if (!b)
	{
		x=1;y=0;
		return a;
	}
	ll g=exgcd(b, a % b);
	ll t=x;
	x=(y % p+p)%p;y=((t-a/b*y)%p+p)% p;
	return g;
}
int main()
{
	int i,j;
	scanf("%d", &t);
	for (i=0; i<t; i++)
	{
		scanf("%d%d%d%d", &a, &n, &p, &h);
		exgcd(a, p);
		int sum=0, now=0, cnt=0;
		for (j=1; j<=p; j++)
		{
			now=(now+a) % p;
			v[j]=now;
		}
		now=0;
		for (j=1; j<p; j++)
		{
			now=(x+now) % p;
			if (v[now]!=j)
				printf("%d", j);
			if (now<=n)
			{
				sum=0;
				cnt++;
				//if (cnt==n) break;
			}
			else
			{
				sum++;
				if (sum>=h) break;
			}
		}
		if (cnt>=n)
			printf("YES\n");
		else
			printf("NO\n");
			/*
		int sum=0, now=0, cnt=0;
		for (j=1; j<=n; j++)
		{
			now=(now+a) % p;
			v[j]=now;
		}
		sort(v,v+n+1);
		for (j=1; j<=n; j++)
			if (v[j]-v[j-1]>h)
				cnt=1;
		if (cnt==0)
			printf("YES\n");
		else
			printf("NO\n");
			*/
	}
}


#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
int t, a, n, p, h;
ll x, y;
int v[10000];

int exgcd(ll a, ll b)
{
	if (!b)
	{
		x=1;y=0;
		return a;
	}
	ll g=exgcd(b, a % b);
	ll t=x;
	x=(y % p+p)%p;y=((t-a/b*y)%p+p)% p;
	return g;
}
int main()
{
	int i,j;
	scanf("%d", &t);
	for (i=0; i<t; i++)
	{
		scanf("%d%d%d%d", &a, &n, &p, &h);
		exgcd(a, p);
		int sum=0, now=0, cnt=0;
		for (j=1; j<=p; j++)
		{
			now=(x+now) % p;
			if (now<=n)
			{
				sum=0;
				cnt++;
				//if (cnt==n) break;
			}
			else
			{
				sum++;
				if (sum>=h) break;
			}
		}
		if (cnt>=n)
			printf("YES\n");
		else
			printf("NO\n");
			/*
		int sum=0, now=0, cnt=0;
		for (j=1; j<=n; j++)
		{
			now=(now+a) % p;
			v[j]=now;
		}
		sort(v,v+n+1);
		for (j=1; j<=n; j++)
			if (v[j]-v[j-1]>h)
				cnt=1;
		if (cnt==0)
			printf("YES\n");
		else
			printf("NO\n");
			*/
	}
}


#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
int t, a, n, p, h;
ll x, y;
int v[10000];

int exgcd(ll a, ll b)
{
	if (!b)
	{
		x=1;y=0;
		return a;
	}
	ll g=exgcd(b, a % b);
	ll t=x;
	x=(y % p+p)%p;y=((t-a/b*y)%p+p)% p;
	return g;
}
int main()
{
	int i,j;
	scanf("%d", &t);
	for (i=0; i<t; i++)
	{
		scanf("%d%d%d%d", &a, &n, &p, &h);
		if (h==0)
		{
			printf("NO\n");
			continue;
		}
		exgcd(a, p);
		int sum=0, now=0, cnt=0;
		for (j=1; j<p; j++)
		{
			now=(x+now) % p;
			if (v[now]!=j)
				printf("%d", j);
			if (now<=n)
			{
				sum=0;
				cnt++;
				if (cnt==n) break;
			}
			else
			{
				sum++;
				if (sum>=h) break;
			}
		}
		if (cnt>=n)
			printf("YES\n");
		else
			printf("NO\n");
			/*
		int sum=0, now=0, cnt=0;
		for (j=1; j<=n; j++)
		{
			now=(now+a) % p;
			v[j]=now;
		}
		sort(v,v+n+1);
		for (j=1; j<=n; j++)
			if (v[j]-v[j-1]>h)
				cnt=1;
		if (cnt==0)
			printf("YES\n");
		else
			printf("NO\n");
			*/
	}
}


#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
int t, a, n, p, h;
ll x, y;
int v[10000];

int exgcd(ll a, ll b)
{
	if (!b)
	{
		x=1;y=0;
		return a;
	}
	ll g=exgcd(b, a % b);
	ll t=x;
	x=(y % p+p)%p;y=((t-a/b*y)%p+p)% p;
	return g;
}
int main()
{
	int i,j;
	scanf("%d", &t);
	for (i=0; i<t; i++)
	{
		scanf("%d%d%d%d", &a, &n, &p, &h);
		int tmp=exgcd(a, p);
		int sum=0, now=0, cnt=0;
		for (j=1; j<=p; j++)
		{
			now=(x+now) % p;
			if (now<=n)
			{
				sum=0;
				cnt++;
				//if (cnt==n) break;
			}
			else
			{
				sum++;
				if (sum>=h) break;
			}
		}
		if (tmp>1)
			printf("NO\n");
		else
		if (cnt>=n)
			printf("YES\n");
		else
			printf("NO\n");
			/*
		int sum=0, now=0, cnt=0;
		for (j=1; j<=n; j++)
		{
			now=(now+a) % p;
			v[j]=now;
		}
		sort(v,v+n+1);
		for (j=1; j<=n; j++)
			if (v[j]-v[j-1]>h)
				cnt=1;
		if (cnt==0)
			printf("YES\n");
		else
			printf("NO\n");
			*/
	}
}


#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
int t, a, n, p, h;
ll x, y;

int exgcd(ll a, ll b)
{
	if (!b)
	{
		x=1;y=0;
		return a;
	}
	ll g=exgcd(b, a % b);
	ll t=x;
	x=(y % p+p)%p;y=((t-a/b*y)%p+p)% p;
	return g;
}
int main()
{
	int i,j;
	scanf("%d", &t);
	for (i=0; i<t; i++)
	{
		scanf("%d%d%d%d", &a, &n, &p, &h);
		exgcd(a, p);
		int sum=0, now=0, cnt=0;
		for (j=1; 1<=p; j++)
		{
			now=(x+now) % p;
			if (now==0) now=p;
			if (now<=n)
			{
				sum=0;
				cnt++;
				if (cnt==n) break;
			}
			else
			{
				sum++;
				if (sum>=h) break;
			}
		}
		if (cnt==n)
			printf("YES\n");
		else
			printf("NO\n");
	}
}


#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
int t, a, n, p, h;
ll x, y;
int v[10000];

int exgcd(ll a, ll b)
{
	if (!b)
	{
		x=1;y=0;
		return a;
	}
	ll g=exgcd(b, a % b);
	ll t=x;
	x=(y % p+p)%p;y=((t-a/b*y)%p+p)% p;
	return g;
}
int main()
{
	int i,j;
	scanf("%d", &t);
	for (i=0; i<t; i++)
	{
		scanf("%d%d%d%d", &a, &n, &p, &h);
		exgcd(a, p);
		int sum=0, now=0, cnt=0;
		for (j=1; j<=p; j++)
		{
			now=(now+a) % p;
			v[j]=now;
		}
		now=0;
		for (j=1; j<p; j++)
		{
			now=(x+now) % p;
			if (v[now]!=j)
				printf("%d", j);
			if (now<=n)
			{
				sum=0;
				cnt++;
				if (cnt==n) break;
			}
			else
			{
				sum++;
				if (sum>=h) break;
			}
		}
		if (i==66)
			printf("%d%d%d%d", a, n, p, h);
		else
		if (cnt>=n)
			printf("YES\n");
		else
			printf("NO\n");
			/*
		int sum=0, now=0, cnt=0;
		for (j=1; j<=n; j++)
		{
			now=(now+a) % p;
			v[j]=now;
		}
		sort(v,v+n+1);
		for (j=1; j<=n; j++)
			if (v[j]-v[j-1]>h)
				cnt=1;
		if (cnt==0)
			printf("YES\n");
		else
			printf("NO\n");
			*/
	}
}


#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
int t, a, n, p, h;
ll x, y;
int v[10000];

int exgcd(ll a, ll b)
{
	if (!b)
	{
		x=1;y=0;
		return a;
	}
	ll g=exgcd(b, a % b);
	ll t=x;
	x=(y % p+p)%p;y=((t-a/b*y)%p+p)% p;
	return g;
}
int main()
{
	int i,j;
	scanf("%d", &t);
	for (i=0; i<t; i++)
	{
		scanf("%d%d%d%d", &a, &n, &p, &h);
		if (h==0)
		{
			printf("NO\n");
			continue;
		}
		exgcd(a, p);
		int sum=0, now=0, cnt=0;
		for (j=1; j<p; j++)
		{
			now=(x+now) % p;
			if (now<=n)
			{
				sum=0;
				cnt++;
				if (cnt==n) break;
			}
			else
			{
				sum++;
				if (sum>=h) break;
			}
		}
		if (cnt>=n)
			printf("YES\n");
		else
			printf("NO\n");
			/*
		int sum=0, now=0, cnt=0;
		for (j=1; j<=n; j++)
		{
			now=(now+a) % p;
			v[j]=now;
		}
		sort(v,v+n+1);
		for (j=1; j<=n; j++)
			if (v[j]-v[j-1]>h)
				cnt=1;
		if (cnt==0)
			printf("YES\n");
		else
			printf("NO\n");
			*/
	}
}


#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
int t, a, n, p, h;
ll x, y;
int v[10000];

int exgcd(ll a, ll b)
{
	if (!b)
	{
		x=1;y=0;
		return a;
	}
	ll g=exgcd(b, a % b);
	ll t=x;
	x=(y % p+p)%p;y=((t-a/b*y)%p+p)% p;
	return g;
}
int main()
{
	int i,j;
	scanf("%d", &t);
	for (i=0; i<t; i++)
	{
		scanf("%d%d%d%d", &a, &n, &p, &h);
		exgcd(a, p);
		int sum=0, now=0, cnt=0;
		for (j=1; j<=p; j++)
		{
			now=(now+a) % p;
			v[j]=now;
		}
		now=0;
		for (j=1; j<p; j++)
		{
			now=(x+now) % p;
			if (v[now]!=j)
				printf("%d", j);
			if (now<=n)
			{
				sum=0;
				cnt++;
				if (cnt==n) break;
			}
			else
			{
				sum++;
				if (sum>=h) break;
			}
		}
		if (i==65)
			printf("%d%d%d%d", a, n, p, h);
		else
		if (cnt>=n)
			printf("YES\n");
		else
			printf("NO\n");
			/*
		int sum=0, now=0, cnt=0;
		for (j=1; j<=n; j++)
		{
			now=(now+a) % p;
			v[j]=now;
		}
		sort(v,v+n+1);
		for (j=1; j<=n; j++)
			if (v[j]-v[j-1]>h)
				cnt=1;
		if (cnt==0)
			printf("YES\n");
		else
			printf("NO\n");
			*/
	}
}


#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
int t, a, n, p, h;
ll x, y;
int v[10000];

int exgcd(ll a, ll b)
{
	if (!b)
	{
		x=1;y=0;
		return a;
	}
	ll g=exgcd(b, a % b);
	ll t=x;
	x=(y % p+p)%p;y=((t-a/b*y)%p+p)% p;
	return g;
}
int main()
{
	int i,j;
	scanf("%d", &t);
	for (i=0; i<t; i++)
	{
		scanf("%d%d%d%d", &a, &n, &p, &h);
		/*
		exgcd(a, p);
		int sum=0, now=0, cnt=0;
		for (j=1; 1<=p; j++)
		{
			now=(x+now) % p;
			if (now<=n)
			{
				sum=0;
				cnt++;
				if (cnt==n) break;
			}
			else
			{
				sum++;
				if (sum>=h) break;
			}
		}
		if (cnt==n)
			printf("YES\n");
		else
			printf("NO\n");
			*/
		int sum=0, now=0, cnt=0;
		for (j=1; j<=n; j++)
		{
			now=(now+a) % p;
			v[j]=now;
		}
		sort(v,v+n+1);
		for (j=1; j<=n; j++)
			if (v[j]-v[j-1]>h)
				cnt=1;
		if (cnt==0)
			printf("YES\n");
		else
			printf("NO\n");
	}
}


#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
int t, a, n, p, h;
ll x, y;
int v[10000];

int exgcd(ll a, ll b)
{
	if (!b)
	{
		x=1;y=0;
		return a;
	}
	ll g=exgcd(b, a % b);
	ll t=x;
	x=(y % p+p)%p;y=((t-a/b*y)%p+p)% p;
	return g;
}
int main()
{
	int i,j;
	scanf("%d", &t);
	for (i=0; i<t; i++)
	{
		scanf("%d%d%d%d", &a, &n, &p, &h);
		exgcd(a, p);
		int sum=0, now=0, cnt=0;
		for (j=1; 1<=p; j++)
		{
			now=(x+now) % p;
			if (now<=n)
			{
				sum=0;
				cnt++;
				//if (cnt==n) break;
			}
			else
			{
				sum++;
				if (sum>=h) break;
			}
		}
		if (cnt>=n)
			printf("YES\n");
		else
			printf("NO\n");
			/*
		int sum=0, now=0, cnt=0;
		for (j=1; j<=n; j++)
		{
			now=(now+a) % p;
			v[j]=now;
		}
		sort(v,v+n+1);
		for (j=1; j<=n; j++)
			if (v[j]-v[j-1]>h)
				cnt=1;
		if (cnt==0)
			printf("YES\n");
		else
			printf("NO\n");
			*/
	}
}


#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
int t, a, n, p, h;
ll x, y;
int v[10000];

int exgcd(ll a, ll b)
{
	if (!b)
	{
		x=1;y=0;
		return a;
	}
	ll g=exgcd(b, a % b);
	ll t=x;
	x=(y % p+p)%p;y=((t-a/b*y)%p+p)% p;
	return g;
}
int main()
{
	int i,j;
	scanf("%d", &t);
	for (i=0; i<t; i++)
	{
		scanf("%d%d%d%d", &a, &n, &p, &h);
		exgcd(a, p);
		int sum=0, now=0, cnt=0;
		for (j=1; j<=p; j++)
		{
			now=(now+a) % p;
			v[j]=now;
		}
		now=0;
		for (j=1; j<p; j++)
		{
			now=(x+now) % p;
			if (v[now]!=j)
				printf("%d", j);
			if (now<=n)
			{
				sum=0;
				cnt++;
				if (cnt==n) break;
			}
			else
			{
				sum++;
				if (sum>=h) break;
			}
		}
		if (cnt>=n)
			printf("YES\n");
		else
			printf("NO\n");
			/*
		int sum=0, now=0, cnt=0;
		for (j=1; j<=n; j++)
		{
			now=(now+a) % p;
			v[j]=now;
		}
		sort(v,v+n+1);
		for (j=1; j<=n; j++)
			if (v[j]-v[j-1]>h)
				cnt=1;
		if (cnt==0)
			printf("YES\n");
		else
			printf("NO\n");
			*/
	}
}


#pragma comment(linker, "/STACK:66777216")
#define _CRT_SECURE_NO_WARNINGS
//#include <bits/stdc++.h>
//#include <unordered_set>
//#include <unordered_map>
#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <fstream>
#include <cassert>
#include <iomanip>
#include <complex>
#include <cstring>
#include <cstdio>
#include <bitset>
#include <string>
#include <vector>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map>

#define forn(i,n) for(int i = 0; i < (int)(n); ++ i)
#define for1(i,n) for(int i = 1; i <= (int)(n); ++ i)
#define fore(i,a,b) for(int i = (int)(a); i <= (int)(b); ++ i)
#define ford(i,n) for(int i = (int)(n)-1; i >= 0; -- i)
#define ford1(i,n) for(int i = (int)(n); i >= 1; -- i)
#define fored(i,a,b) for(int i = (int)(b); i >= (int)(a);--i)
#define mp make_pair
#define pb push_back
#define sz(v) ((int)((v).size()))
#define all(v) (v).begin(), (v).end()
//#define fi first
//#define se second

using namespace std;

typedef long long i64;
typedef unsigned long long u64;
typedef long double ld;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
typedef vector<pii> vpi;
typedef vector<ld> vd;
typedef pair<ld,ld> pdd;
typedef vector<pdd> vpd;
typedef unsigned long long ULL;
typedef long long LL;
typedef long double LD;

int solvefor(int a, int b, int h){
	if(b <= h)
		return 0;
	if(a <= h)
		return (b-h+(a-1))/a;
	int u = solvefor(a-b%a, a, h);
	int e = b*u;
	int q = (e/a);
	//q must be done and now it is not yet possible
	int st = ((q+1)*a)%b+1;//st is the starting point
	assert(st <= a);
	//st+a*k <= b-1
	int k = (b-1-st)/a;
	return (k+1)+q;
}
void solve(){
	int T;
	scanf("%d", &T);
	while(T--){
		int a, n, p, h;
		scanf("%d%d%d%d", &a, &n, &p, &h);
		int u = solvefor(a, p, h);
//		cerr<<u<<endl;
		bool res = (n>=u);
		if(n >= u)
			puts("YES");
		else
			puts("NO");
	}
}

void testgen(){
	FILE * f = fopen("input.txt", "w");
//	fprintf(f, "%d %d\n", i, j);
//	srand(time(NULL));
//	LL T = 8000;
//	fprintf(f, "%d\n", T);
	fclose(f);
}
int main() {
#ifdef HOME
//	testgen();
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#else
#define task "end"
//	freopen(task".in", "r", stdin);
//	freopen(task".out", "w", stdout);
#endif

	cout<<fixed;
	cout.precision(2);
	cerr<<fixed;
	cerr.precision(3);

	solve();

#ifdef HOME
	cerr<<"Execution time = "<<clock()/1000.0<<"ms\n";
#endif
	return 0;
}


#pragma comment(linker, "/STACK:66777216")
#define _CRT_SECURE_NO_WARNINGS
//#include <bits/stdc++.h>
//#include <unordered_set>
//#include <unordered_map>
#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <fstream>
#include <cassert>
#include <iomanip>
#include <complex>
#include <cstring>
#include <cstdio>
#include <bitset>
#include <string>
#include <vector>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map>

#define forn(i,n) for(int i = 0; i < (int)(n); ++ i)
#define for1(i,n) for(int i = 1; i <= (int)(n); ++ i)
#define fore(i,a,b) for(int i = (int)(a); i <= (int)(b); ++ i)
#define ford(i,n) for(int i = (int)(n)-1; i >= 0; -- i)
#define ford1(i,n) for(int i = (int)(n); i >= 1; -- i)
#define fored(i,a,b) for(int i = (int)(b); i >= (int)(a);--i)
#define mp make_pair
#define pb push_back
#define sz(v) ((int)((v).size()))
#define all(v) (v).begin(), (v).end()
//#define fi first
//#define se second

using namespace std;

typedef long long i64;
typedef unsigned long long u64;
typedef long double ld;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
typedef vector<pii> vpi;
typedef vector<ld> vd;
typedef pair<ld,ld> pdd;
typedef vector<pdd> vpd;
typedef unsigned long long ULL;
typedef long long LL;
typedef long double LD;
const int INF = 1000000000+5;
int solvefor(int a, int b, int h){
	if(b <= h)
		return 0;
	if(a <= h)
		return (b-h+(a-1))/a;
	int u = solvefor(a-b%a, a, h);
	int e = b*u;
	int q = (e/a);
	//q must be done and now it is not yet possible
	int st = ((q+1)*a)%b+1;//st is the starting point
	assert(st <= a);
	//st+a*k <= b-1
	int k = (b-1-st)/a;
	return (k+1)+q;
}
bool canbe(set<int> & se, int h){
	int cr = 0;
	for(set<int>::iterator it = se.begin(); it!=se.end(); ++ it){
		if(*it-cr > h)return false;
		cr = *it;
	}
	return true;
}
int perebor(int a, int b, int h){
	set<int> se;
	se.insert(b);
	for(int i = 1; i <= b; ++ i){
		if(canbe(se,h))
			return i-1;
		se.insert((i*a)%b);
	}
	return INF;
}
void solve(){
	int T;
	scanf("%d", &T);
	while(T--){
		int a, n, p, h;
		scanf("%d%d%d%d", &a, &n, &p, &h);
		if(h==0){
			puts("NO");
			continue;
		}
		int u = solvefor(a, p, h);
//		cerr<<u<<endl;
/*		if(u!=perebor(a,p,h)){
			cout<<"1\n";
			cout<<a<<" 0 "<<p<<" "<<h<<endl;
			return;
			assert(false);
		}*/
		bool res = (n>=u);
		if(n >= u)
			puts("YES");
		else
			puts("NO");
	}
}

int gcd(int a, int b){
	return a?gcd(b%a,a):b;
}
void testgen(){
	FILE * f = fopen("input.txt", "w");
//	fprintf(f, "%d %d\n", i, j);
	srand(time(NULL));
	int T = 50*50*50;
	for(int p = 1; p <= 50; ++ p){
		for(int a = 1; a < p; ++ a){
			if(gcd(a,p)!=1)
				continue;
			for(int h = 1; h < p; ++ h){
				fprintf(f, "%d 0 %d %d\n", a,p, h);
			}
		}
	}
//	fprintf(f, "%d\n", T);
	fclose(f);
}
int main() {
#ifdef HOME
//	testgen();
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#else
#define task "end"
//	freopen(task".in", "r", stdin);
//	freopen(task".out", "w", stdout);
#endif

	cout<<fixed;
	cout.precision(2);
	cerr<<fixed;
	cerr.precision(3);

	solve();

#ifdef HOME
	cerr<<"Execution time = "<<clock()/1000.0<<"ms\n";
#endif
	return 0;
}


#pragma comment(linker, "/STACK:66777216")
#define _CRT_SECURE_NO_WARNINGS
//#include <bits/stdc++.h>
//#include <unordered_set>
//#include <unordered_map>
#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <fstream>
#include <cassert>
#include <iomanip>
#include <complex>
#include <cstring>
#include <cstdio>
#include <bitset>
#include <string>
#include <vector>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map>

#define forn(i,n) for(int i = 0; i < (int)(n); ++ i)
#define for1(i,n) for(int i = 1; i <= (int)(n); ++ i)
#define fore(i,a,b) for(int i = (int)(a); i <= (int)(b); ++ i)
#define ford(i,n) for(int i = (int)(n)-1; i >= 0; -- i)
#define ford1(i,n) for(int i = (int)(n); i >= 1; -- i)
#define fored(i,a,b) for(int i = (int)(b); i >= (int)(a);--i)
#define mp make_pair
#define pb push_back
#define sz(v) ((int)((v).size()))
#define all(v) (v).begin(), (v).end()
//#define fi first
//#define se second

using namespace std;

typedef long long i64;
typedef unsigned long long u64;
typedef long double ld;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
typedef vector<pii> vpi;
typedef vector<ld> vd;
typedef pair<ld,ld> pdd;
typedef vector<pdd> vpd;
typedef unsigned long long ULL;
typedef long long LL;
typedef long double LD;
void gcd(int a, int b, int & x, int & y){
	if(a==0){
		x = 0;y = 1;return;
	}
	int x1, y1;
	gcd(b%a,a,x1,y1);
	x = y1-( (int)b/a )*x1;
	y = x1;
}
int a, n, p, h;
inline int divup(int a, int b){
	return (a+b-1)/b;
}
pii getfor(int a, int b){
	//we will count a*0 on the answer
	if(b <= h){
		return mp(1,0);//answer last
	}
	if(a <= h){
		int q = divup(b-h,a);
		return mp(q+1,q*a);
	}
	pii c = getfor(a-b%a,a);
	int q1 = ( (LL)c.first*b )/a;
	int last = c.second-(a-b%a);
	last = last+((int)b/a)*a;
	int u = ((LL)q1*a)%b;
	if(u==0)u = b;
	if(last < u)
		-- q1;
	return mp(q1+1, last);
}
int chack(int a, int b, int h){
	set<int> se;
	se.insert(0);
	int cr = 0;
	do{
		bool ok = true;
		for(set<int>::iterator it = se.begin(); it!=se.end(); ++ it){
			int v1 = *it;
			int v2 = b;
			set<int>::iterator it2 = it;
			++ it2;
			if(it2!=se.end()){
				v2 = *it2;
			}
			if( v2-v1 > h){
				ok = false;
				break;
			}
		}
		if(ok)break;
		++ cr;
		se.insert( ((LL)cr*a)%b );
	}while(1);
	return sz(se);
}
char * solvefor(){
	pii g = getfor(a,p);
/*	if(g.first!=chack(a,p,h)){
		cerr<<a<<" "<<n<<" "<<p<<" "<<h<<endl;
		assert(false);
	}*/
	char * c;
	if(g.first-1 <= n){
		c = "YES";
	}else{
		c = "NO";
	}
	return c;
}
void solve(){
	int T;
	scanf("%d", &T);
	while(T--){
		scanf("%d%d%d%d", &a, &n, &p, &h);//n < p
		if(h==0){
			puts("NO");continue;
		}
		a = a%p;
		if(a <= h){
			puts("YES");continue;
		}
		//a > h, n < p, (a,p)=1
		//a > h,a < p =>  so we must be able to reach a and so if we will find the minimum N for wich the answer is YES we are done
		printf("%s\n", solvefor());
	}
}

void testgen(){
	FILE * f = fopen("input.txt", "w");
	int T = 100*100*100;
	fprintf(f, "%d\n", T);
	for1(i,100){
		for1(j,100){
			if(i > j)continue;
			int x1, y1;
			gcd(i,j,x1,y1);
			for1(k,100){
				if((i*x1+j*y1)==1){
					fprintf(f, "%d 0 %d %d\n", i, j, k);
				}
			}
		}
	}
//	srand(time(NULL));
	fclose(f);
}
int main() {
#ifdef HOME
//	testgen();
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#else
#define task "can"
//	freopen(task".in", "r", stdin);
//	freopen(task".out", "w", stdout);
#endif

	cout<<fixed;
	cout.precision(2);
	cerr<<fixed;
	cerr.precision(3);

	solve();

#ifdef HOME
	cerr<<"Execution time = "<<clock()/1000.0<<"ms\n";
#endif
	return 0;
}


#pragma comment(linker, "/STACK:66777216")
#define _CRT_SECURE_NO_WARNINGS
//#include <bits/stdc++.h>
//#include <unordered_set>
//#include <unordered_map>
#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <fstream>
#include <cassert>
#include <iomanip>
#include <complex>
#include <cstring>
#include <cstdio>
#include <bitset>
#include <string>
#include <vector>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map>

#define forn(i,n) for(int i = 0; i < (int)(n); ++ i)
#define for1(i,n) for(int i = 1; i <= (int)(n); ++ i)
#define fore(i,a,b) for(int i = (int)(a); i <= (int)(b); ++ i)
#define ford(i,n) for(int i = (int)(n)-1; i >= 0; -- i)
#define ford1(i,n) for(int i = (int)(n); i >= 1; -- i)
#define fored(i,a,b) for(int i = (int)(b); i >= (int)(a);--i)
#define mp make_pair
#define pb push_back
#define sz(v) ((int)((v).size()))
#define all(v) (v).begin(), (v).end()
//#define fi first
//#define se second

using namespace std;

typedef long long i64;
typedef unsigned long long u64;
typedef long double ld;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
typedef vector<pii> vpi;
typedef vector<ld> vd;
typedef pair<ld,ld> pdd;
typedef vector<pdd> vpd;
typedef unsigned long long ULL;
typedef long long LL;
typedef long double LD;

int solvefor(int a, int b, int h){
	if(b <= h)
		return 0;
	if(a <= h)
		return (b-h+(a-1))/a;
	int u = solvefor(a-b%a, a, h);
	int e = b*u;
	int q = (e/a);
	//q must be done and now it is not yet possible
	int st = ((q+1)*a)%b+1;//st is the starting point
	assert(st <= a);
	//st+a*k <= b-1
	int k = (b-1-st)/a;
	return (k+1)+q;
}
void solve(){
	int T;
	scanf("%d", &T);
	while(T--){
		int a, n, p, h;
		scanf("%d%d%d%d", &a, &n, &p, &h);
		int u = solvefor(a, p, h);
//		cerr<<u<<endl;
		bool res = (n>=u);
		if(n >= u)
			puts("YES");
		else
			puts("NO");
	}
}

void testgen(){
	FILE * f = fopen("input.txt", "w");
//	fprintf(f, "%d %d\n", i, j);
//	srand(time(NULL));
//	LL T = 8000;
//	fprintf(f, "%d\n", T);
	fclose(f);
}
int main() {
#ifdef HOME
//	testgen();
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#else
#define task "end"
	freopen(task".in", "r", stdin);
	freopen(task".out", "w", stdout);
#endif

	cout<<fixed;
	cout.precision(2);
	cerr<<fixed;
	cerr.precision(3);

	solve();

#ifdef HOME
	cerr<<"Execution time = "<<clock()/1000.0<<"ms\n";
#endif
	return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;

using LL = long long;

bool test(int a, int b, int n, int m){
    LL x=a*LL(n);
    if(x<b) return max(a,b-a*n)<=m;
    return test(b%a,a,(x+a+b%a)/b-1,m);
}
/* Key:
1. using b%a instead of a-b%a is viable
2. only need to compare the height diff. with lower platform
*/
int main(){
    int cs,a,n,p,h;
    scanf("%d",&cs);
    while(cs--){
        scanf("%d%d%d%d",&a,&n,&p,&h); a%=p;
        puts((a*LL(n)<p?a<=h:test(a,p,n,h))?"YES":"NO");
    }
}


#include <cstdio>
#include <algorithm>
using namespace std;

typedef pair<int,int> PII;
typedef long long LL;

int gao(int a, int b, int n){
    LL x=LL(a)*n;
    if(x<b) return max(a,b-a*n);
    return gao(b%a,a,(x+a+b%a)/b-1);
}

int main(){
    int cs,a,n,p,h;
    scanf("%d",&cs);
    while(cs--){
        scanf("%d%d%d%d",&a,&n,&p,&h); a%=p;
        int g=a*LL(n)<p?a:gao(a,p,n);
        puts(g<=h?"YES":"NO");
    }
}


#include <cstdio>
#include <algorithm>
using namespace std;

typedef pair<int,int> PII;
typedef long long LL;

int gao(int a, int b, int n){
    LL x=LL(a)*n;
    if(x<b) return max(a,b-a*n);
    return gao(b%a,a,(x+a+b%a)/b-1);
}

int main(){
    int cs,a,n,p,h;
    scanf("%d",&cs);
    while(cs--){
        scanf("%d%d%d%d",&a,&n,&p,&h);
        int g=a*LL(n)<p?a:gao(a%p,p,n);
        puts(g<=h?"YES":"NO");
    }
}


#include <cstdio>
#include <algorithm>
using namespace std;

using LL = long long;

bool test(int a, int b, int n, int m){
    LL x=a*LL(n);
    if(x<b) return max(a,b-a*n)<=m;
    return test(b%a,a,(x+a+b%a)/b-1,m)<=m;
}
/* Key:
1. using b%a instead of a-b%a is viable
2. only need to compare the height diff. with lower platform
*/
int main(){
    int cs,a,n,p,h;
    scanf("%d",&cs);
    while(cs--){
        scanf("%d%d%d%d",&a,&n,&p,&h); a%=p;
        puts((a*LL(n)<p?a<=h:test(a,p,n,h))?"YES":"NO");
    }
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>

#define REP(i,n) for(int i=0; i<(n); ++i)
#define FOR(i,p,k) for(int i=(p); i<=(k); ++i)

template<typename T> void checkmin(T &a, T b){ if(a>b) a = b; }
template<typename T> void checkmax(T &a, T b){ if(a<b) a = b; }

typedef long long LL;

bool solve(LL a, LL n, LL p, LL h, bool ceil = 0)
{
  if(a*n<p) return a<=h && (!ceil || p-a*n<=h);
  if(2*a>p) return solve(p-a,n,p,h,1);
  LL s = p/a-1; // segments to be ignored
  LL p2 = p%a+a;
  LL n2 = n - (n*a/p*s+std::min(n*a%p/a+1,s));
  return solve(a,n2,p2,h,ceil);
}

void run()
{
  int t; scanf("%d",&t);
  while(t--)
  {
    int a,n,p,h; scanf("%d%d%d%d",&a,&n,&p,&h);
    a %= p;
    puts(solve(a,n,p,h) ? "YES" : "NO");
  }
}

void brute(int a, int n, int p, int h)
{
  std::vector<int> A;
  REP(i,n+1) A.push_back(a*n%p);
}

int main()
{
  run();
  return 0;
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>

#define REP(i,n) for(int i=0; i<(n); ++i)
#define FOR(i,p,k) for(int i=(p); i<=(k); ++i)

template<typename T> void checkmin(T &a, T b){ if(a>b) a = b; }
template<typename T> void checkmax(T &a, T b){ if(a<b) a = b; }

bool solve(int a, int n, int p, int h, bool ceil = 0)
{
  //printf("solve(a=%d; n=%d; p=%d; h=%d; ceil=%d)\n",a,n,p,h,ceil);
  if(a*n<p) return a<=h && (!ceil || p-a*n<=h);
  if(2*a>p) return solve(p-a,n,p,h,1);
  int s = p/a-1; // segments to be ignored
  int p2 = p%a+a;
  int n2 = n - (n*a/p*s+std::min(n*a%p/a+1,s));
  return solve(a,n2,p2,h,ceil);
}

int main()
{
  int t; scanf("%d",&t);
  while(t--)
  {
    int a,n,p,h; scanf("%d%d%d%d",&a,&n,&p,&h);
    a %= p;
    puts(solve(a,n,p,h) ? "YES" : "NO");
  }
  return 0;
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>

#define REP(i,n) for(int i=0; i<(n); ++i)
#define FOR(i,p,k) for(int i=(p); i<=(k); ++i)

template<typename T> void checkmin(T &a, T b){ if(a>b) a = b; }
template<typename T> void checkmax(T &a, T b){ if(a<b) a = b; }

bool solve(int a, int n, int p, int h, bool ceil = 0)
{
  //printf("solve(a=%d; n=%d; p=%d; h=%d; ceil=%d)\n",a,n,p,h,ceil);
  if(a*n<p) return a<=h && (!ceil || p-a*n<=h);
  if(2*a>p) return solve(p-a,n,p,h,1);
  int s = p/a-1; // segments to be ignored
  int p2 = p%a+a;
  int n2 = n - (n*a/p*s+std::min(n*a%p/a+1,s));
  return solve(a,n2,p2,h,ceil);
}

int main()
{
  int t; scanf("%d",&t);
  while(t--)
  {
    int a,n,p,h; scanf("%d%d%d%d",&a,&n,&p,&h);
    puts(solve(a,n,p,h) ? "YES" : "NO");
  }
  return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
long long t,a,n,p,h;
long long calc(long long a,long long n,long long p)
{
    if(a*n<=p)return max(a,p-a*(n-1));
    return calc(p%a,(a*n+p%a)/p,a);
}
int main()
{
    cin>>t;
    while(t--)
    {
        scanf("%I64d%I64d%I64d%I64d",&a,&n,&p,&h);
        a%=p;
        if(((a*n<p)?a:calc(a,n+1,p))<=h)puts("YES");
        else puts("NO");
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define int long long

bool solve(int p, int n, int a, int h)
{
    int r = p % a;
    if(a * n <= p)
        return (h >= max(a, p - ((n-1) * a)));
    return solve(a, (a * n + r) / p, r, h);
}

main()
{
    int t;
    cin>>t;
    for(int i = 0; i < t; i++)
    {
        int p, n, a, h;
        cin>>a>>n>>p>>h;
        a %= p;
        if(a * n < p && a <= h)
        {
            cout<<"YES\n";
            continue;
        }
        cout<<(solve(p, n + 1, a, h) ? "YES" : "NO")<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

bool solve(int p, int n, int a, int h)
{
    //cerr<<p<<" "<<n<<" "<<a<<" "<<h<<endl;
    if(!n && h < p)
        return false;
    int r = a - (p % a), k = n * a / p;
    if(!k)
        return (h >= max(a, p - (n * a)));
    return solve(a, k, r, h);
}

int main()
{
    int t;
    cin>>t;
    for(int i = 0; i < t; i++)
    {
        int p, n, a, h;
        cin>>a>>n>>p>>h;
        cout<<(solve(p, n, a, h) ? "YES" : "NO")<<endl;
    }

    return 0;
}


#include<stdio.h>
#include<stdlib.h>

int T,a,n,p,h;

//FUNKCIJA 'a'//
int poz(int x,int y,int z)
{
	if((long long)x*y<z)
	{
		if(z==p)return x;
		else return max(x,z-x*y);
	}
	int tmp=(long long)x*y/z+((long long)x*y%z!=0);
	if((long long)x*y%z+x<=z/x*x)--tmp;
	return poz(x-z%x,tmp-1,x);
}

//Funkcija max//
int max(int a, int b)
{
if(a>b) return a else return b;
}

///
int main()
{
scanf("%d",&T);
	for(;T;--T)
	{
		scanf("%d%d%d%d",&a,&n,&p,&h);
		if(poz(a%p,n,p)<=h)printf("YES\n");
		else printf("NO\n");
	}
}


#include<stdio.h>
#include<stdlib.h>

int t,a,n,p,h;

//FUNKCIJA 'a'//
int a(int x,int y,int z)
{
	if((long long)x*y<z)
	{
		if(z==p)return x;
		else return max(x,z-x*y);
	}
	int tmp=(long long)x*y/z+((long long)x*y%z!=0);
	if((long long)x*y%z+x<=z/x*x)--tmp;
	return a(x-z%x,tmp-1,x);
}

int max(int a, int b)
{
x>y?return x:return y
}

///
int main()
{
scanf("%d",&T);
	for(;T;--T)
	{
		scanf("%d%d%d%d",&a,&n,&p,&h);
		if(a(a%p,n,p)<=h)printf("YES\n");
		else printf("NO\n");
	}
}


#include<stdio.h>
#include<stdlib.h>

int T,a,n,p,h;

//FUNKCIJA 'a'//
int poz(int x,int y,int z)
{
	if((long long)x*y<z)
	{
		if(z==p)return x;
		else return max(x,z-x*y);
	}
	int tmp=(long long)x*y/z+((long long)x*y%z!=0);
	if((long long)x*y%z+x<=z/x*x)--tmp;
	return poz(x-z%x,tmp-1,x);
}

//Funkcija max//
int max(int a, int b)
{
a>b?return a:return b
}

///
int main()
{
scanf("%d",&T);
	for(;T;--T)
	{
		scanf("%d%d%d%d",&a,&n,&p,&h);
		if(poz(a%p,n,p)<=h)printf("YES\n");
		else printf("NO\n");
	}
}


#include<stdio.h>
#include<stdlib.h>

int T,a,n,p,h;

//FUNKCIJA 'a'//
int poz(int x,int y,int z)
{
	if((long long)x*y<z)
	{
		if(z==p)return x;
		else return max(x,z-x*y);
	}
	int tmp=(long long)x*y/z+((long long)x*y%z!=0);
	if((long long)x*y%z+x<=z/x*x)--tmp;
	return poz(x-z%x,tmp-1,x);
}

//Funkcija max//
int max(int a, int b)
{
x>y?return x:return y
}

///
int main()
{
scanf("%d",&T);
	for(;T;--T)
	{
		scanf("%d%d%d%d",&a,&n,&p,&h);
		if(poz(a%p,n,p)<=h)printf("YES\n");
		else printf("NO\n");
	}
}


#include<stdio.h>
#include<stdlib.h>

int t,a,n,p,h;

//FUNKCIJA 'poz'//
int poz(int x,int y,int z)
{
	if((long long)x*y<z)
	{
		if(z==p)return x;
		else return max(x,z-x*y);
	}
	int tmp=(long long)x*y/z+((long long)x*y%z!=0);
	if((long long)x*y%z+x<=z/x*x)--tmp;
	return poz(x-z%x,tmp-1,x);
}

//Funkcija max//
int max(int a, int b)
{
if(a>b) return a; else return b;
}

///
int main()
{
scanf("%d",&t);
	for(;t;--t)
	{
		scanf("%d%d%d%d",&a,&n,&p,&h);
		if(poz(a%p,n,p)<=h)printf("YES\n");
		else printf("NO\n");
	}
}


#include<stdio.h>
#include<stdlib.h>

int t,a,n,p,h;

//FUNKCIJA 'poz'//
int poz(int x,int y,int z)
{
	if((long long)x*y<z)
	{
		if(z==p)return x;
		else return max(x,z-x*y);
	}
	int tmp=(long long)x*y/z+((long long)x*y%z!=0);
	if((long long)x*y%z+x<=z/x*x)--tmp;
	return poz(x-z%x,tmp-1,x);
}

//Funkcija 'max'//
int max(int a, int b) {if(a>b) return a; else return b;}

//FUNKCIJA 'main'//
void main()
{
scanf("%d",&t);
	for(;t;--t)
	{
		scanf("%d %d %d %d",&a,&n,&p,&h);
		if(poz(a%p,n,p)<=h)printf("YES\n");
		else printf("NO\n");
	}
}


#include<stdio.h>
#include<stdlib.h>

int T,a,n,p,h;

//FUNKCIJA 'a'//
int poz(int x,int y,int z)
{
	if((long long)x*y<z)
	{
		if(z==p)return x;
		else return max(x,z-x*y);
	}
	int tmp=(long long)x*y/z+((long long)x*y%z!=0);
	if((long long)x*y%z+x<=z/x*x)--tmp;
	return poz(x-z%x,tmp-1,x);
}

//Funkcija max//
int max(int a, int b)
{
if(a>b) return a; else return b;
}

///
int main()
{
scanf("%d",&T);
	for(;T;--T)
	{
		scanf("%d%d%d%d",&a,&n,&p,&h);
		if(poz(a%p,n,p)<=h)printf("YES\n");
		else printf("NO\n");
	}
}


import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.math.BigInteger;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskE solver = new TaskE();
        int testCount = Integer.parseInt(in.next());
        for (int i = 1; i <= testCount; i++)
            solver.solve(i, in, out);
        out.close();
    }
}

class TaskE {
    int firstSmallMultiple(int mult, int modulo, int upto) {
        mult %= modulo;
        if (mult <= upto)
            return 1;
        if (upto == 0)
            return modulo;
        return (int) ((firstLargeMultiple(modulo, mult, mult - upto) * (long) modulo + mult - 1) / mult);
    }

    private int firstLargeMultiple(int mult, int modulo, int atleast) {
        mult %= modulo;
        if (atleast == 0) throw new RuntimeException();
        if (mult == 0)
            return modulo;
        if ((atleast + mult - 1) / mult * mult <= modulo)
            return (atleast + mult - 1) / mult;
        return (int) ((firstSmallMultiple(modulo, mult, modulo - atleast) * (long) modulo + atleast - modulo + mult - 1) / mult);
    }

    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int a = in.nextInt();
        int n = in.nextInt();
        int p = in.nextInt();
        int h = in.nextInt();
        a %= p;
        int left = firstSmallMultiple(a, p, h);
        int right = firstSmallMultiple(p - a, p, h);
        int atleast = Math.max(0, n - left + 1);
        int atmost = Math.min(n, right - 1);
        if (atleast < atmost) {
            out.println("NO");
            return;
        }
        if (atleast > atmost) {
            out.println("YES");
            return;
        }
        int max = p - firstSmallMultiple(BigInteger.valueOf(p - a).modInverse(BigInteger.valueOf(p)).intValue(), p, n);
        if ((atleast * (long) a) % p == max) {
            out.println("YES");
        } else {
            out.println("NO");
        }
    }
}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream));
        tokenizer = null;
    }

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

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

    }


#include <algorithm>

#include <stdio.h>

#include <memory.h>

#include <queue>

#include <cmath>

#include <set>

#include <map>

#include <string.h>

#include <string>

#include <ctype.h>

#include <ctime>

#include <complex>

#include <bitset>

using namespace std;

typedef long long INT;

typedef pair<int, int> pii;

#define eprintf(...) fprintf(stderr, __VA_ARGS__)

template <class T> inline void chkmin(T &a, T b) { if (a>b) a=b; }

template <class T> inline void chkmax(T &a, T b) { if (a<b) a=b; }

int solve(int a, int n, int p) {

	int t;

	t=1LL*a*n/p;

	if (t==0) return max(a, p-a*n);

	if (1LL*a*n%p<a*(p/a-1)) t--;

	return solve(min(p%a, a-p%a), t, a);

}

int main() {

	int T, A, N, P, H, diff;

	for (scanf("%d", &T); T--; ) {

		scanf("%d%d%d%d", &A, &N, &P, &H);

		A%=P;

		if (1LL*A*N<P) diff=A;

		else diff=solve(A, N, P);

		if (diff<=H) puts("YES");

		else puts("NO");

	}

	return 0;

}


#include <algorithm>
#include <cstdio>

#ifdef _WIN32
#define LLFORMAT "I64"
#else
#define LLFORMAT "ll"
#endif

int ia, in, ip, ih;

void init()
{
	scanf("%d%d%d%d", &ia, &in, &ip, &ih);
}

int f(int a, int n, int p)
{
	if ((long long)a * (n - 1) < p)
		return std::max(a, p - a * (n - 1));
	int k = (long long)a * n % p;
	int num = (long long)a * n / p + (k >= p / a * a);
	return f((a - p % a) % a, num, a);
}

void solve()
{
	++in;
	ia %= ip;
	int u;
	if ((long long)ia * (in - 1) < ip) u = ia;
	else u = f(ia, in, ip);
	if (u <= ih) printf("YES\n");
	else printf("NO\n");
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("9222.in", "r", stdin);
	freopen("9222.out", "w", stdout);
#endif

	int t;
	scanf("%d", &t);
	while (t--)
	{
		init();
		solve();
	}

#ifndef ONLINE_JUDGE
	fclose(stdin); fclose(stdout);
#endif
	return 0;
}


#include <cstdio>

#ifdef _WIN32
#define LLFORMAT "I64"
#else
#define LLFORMAT "ll"
#endif

const int MAXDEP = 55;

int ia, in, ip, ih;
int xlist[MAXDEP];
int ylist[MAXDEP];

void init()
{
	scanf("%d%d%d%d", &ia, &in, &ip, &ih);
}

void work_euc(int a, int b, int dep)
{
	if (a == 0)
	{
		xlist[dep] = 0;
		ylist[dep] = 1;
	}
	else
	{
		work_euc((a - b % a) % a, a, dep + 1);
		xlist[dep] = ylist[dep+1] + b / a * xlist[dep+1];
		ylist[dep] = -xlist[dep+1];
	}
}

bool ok(int a, int n, int p, int h, int dep)
{
	if ((long long)a * (n - 1) < p)
		return a <= h && p - a * (n - 1) <= h;
	int k = (long long)a * n % p;
	if (k >= p / a * a) k = (k + a) % p;
	int kmod = k % a;
	int xa = (ylist[dep] % a + a) % a;
	int num = (long long)xa * kmod % a;
	return ok((a - p % a) % a, num, a, h, dep + 1);
}

void solve()
{
	++in;
	work_euc(ia, ip, 0);
	if (ok(ia, in, ip, ih, 0)) printf("YES\n");
	else printf("NO\n");
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("9222.in", "r", stdin);
	freopen("9222.out", "w", stdout);
#endif

	int t;
	scanf("%d", &t);
	while (t--)
	{
		init();
		solve();
	}

#ifndef ONLINE_JUDGE
	fclose(stdin); fclose(stdout);
#endif
	return 0;
}


#include <cstdio>

#ifdef _WIN32
#define LLFORMAT "I64"
#else
#define LLFORMAT "ll"
#endif

int ia, in, ip, ih;

void init()
{
	scanf("%d%d%d%d", &ia, &in, &ip, &ih);
}

bool ok(int a, int n, int p, int h)
{
	if ((long long)a * (n - 1) < p)
		return a <= h && p - a * (n - 1) <= h;
	int k = (long long)a * n % p;
	int num = (long long)a * n / p + (k >= p / a * a);
	return ok((a - p % a) % a, num, a, h);
}

void solve()
{
	++in;
	ia %= ip;
	if (ok(ia, in, ip, ih)) printf("YES\n");
	else printf("NO\n");
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("9222.in", "r", stdin);
	freopen("9222.out", "w", stdout);
#endif

	int t;
	scanf("%d", &t);
	while (t--)
	{
		init();
		solve();
	}

#ifndef ONLINE_JUDGE
	fclose(stdin); fclose(stdout);
#endif
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
int F(LL a,LL n,LL p)
{
    LL newn=a*n/p;
    if(!newn)
        return max(a,p-a*n);
    if(a*n%p<a*(p/a-1))
        --newn;
    return F(min(a-p%a,p%a),newn,a);
}
int main()
{
    LL T,a,n,p,h;
    for(cin>>T;T;--T)
    {
        cin>>a>>n>>p>>h;
        a%=p;
        LL val=a*n<p?a:F(a,n,p);
        printf("%s\n",val>h?"NO":"YES");
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
int F(LL a,LL n,LL p)
{
    LL newn=a*n/p;
    if(!newn)
        return max(a,p-a*n);
    if(a*n%p<a*(p/a-1))
        --newn;
    return F(min(a-p%a,p%a),newn,a);
}
int main()
{
    int T,a,n,p,h;
    for(cin>>T;T;--T)
    {
        cin>>a>>n>>p>>h;
        printf("%s\n",F(a,n,p)>h?"NO":"YES");
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
int F(LL a,LL n,LL p)
{
    LL newn=a*n/p;
    if(!newn)
        return max(a,p-a*n);
    if(a*n%p<a*(n/p-1))
        --newn;
    return F(min(a-p%a,p%a),newn,a);
}
int main()
{
    int T,a,n,p,h;
    for(cin>>T;T;--T)
    {
        cin>>a>>n>>p>>h;
        printf("%s\n",F(a,n,p)>h?"NO":"YES");
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
int F(LL a,LL n,LL p)
{
    LL newn=a*n/p;
    if(!newn)
        return max(a,p-a*n);
    if(a*n%p<a*(p/a-1))
        --newn;
    return F(min(a-p%a,p%a),newn,a);
}
int main()
{
    int T,a,n,p,h;
    for(cin>>T;T;--T)
    {
        cin>>a>>n>>p>>h;
        printf("%s\n",F(a%p,n,p)>h?"NO":"YES");
    }
    return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
int f(int a,int n,int p)
{
	if(a>p/2)
		a=p-a;
	if((long long)(a)*n<p)
		return (a>p-a*n?a:p-a*n);
	return f(a-p%a,((long long)(a)*(n+1)+p%a)/p-1,a);
}
int main()
{
	int T;
	int a,n,p,h;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d%d%d",&a,&n,&p,&h);
		a%=p;
		int key;
		if((long long)(a)*n<p)
			key=a;
		else
			key=f(a,n,p);
		if (key<=h)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
int g(int a,int n,int p)
{
	long long z=a*n;
	if(z<p)
		return max(a,p-a*n);
	int za=a-p%a;
	int zp=a;
	int zn=z/p;
	if(z%p<p-p%a-a)
		zn--;
	return g(za,zn,zp);
}
void f()
{
	int a,n,p,h;
	scanf("%d%d%d%d",&a,&n,&p,&h);
	a%=p;
	int key;
	long long z=a*n;
	if(z<p)
		key=a;
	else
		key=g(a,n,p);
	if(key<=h)
		printf("YES\n");
	else
		printf("NO\n");
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
		f();
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
int g(int a,int n,int p)
{
	long long z=a;
	z*=n;
	if(z<p)
		return max(a,p-a*n);
	int za=a-p%a;
	int zp=a;
	int zn=z/p;
	if(z%p<p-p%a-a)
		zn--;
	return g(za,zn,zp);
}
void f()
{
	int a,n,p,h;
	scanf("%d%d%d%d",&a,&n,&p,&h);
	a%=p;
	int key;
	long long z=a;
	z*=n;
	if(z<p)
		key=a;
	else
		key=g(a,n,p);
	if(key<=h)
		printf("YES\n");
	else
		printf("NO\n");
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
		f();
	return 0;
}


#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#define For(i,x,y) for (i=x;i<=y;i++)
using namespace std;
int i,j,k,n,m,a,P,h;
int cal(int m,int n,int p) {
	if (n==0) return m;
	if (1ll*n*m<p) return max(P==p?0:p-n*m,m);
	int ge=1ll*m*n/p,C=1ll*n*m%p,len=p/m*m;
	if (C+m<=len) ge--;
	return cal(m-p%m,ge,m);
}
int main() {
	scanf("%d",&n);
	For(i,1,n) {
		scanf("%d%d%d%d",&a,&m,&P,&h);
		printf("%s\n",cal(a%P,m,P)<=h?"YES":"NO");
		if (i==79) printf("%d %d %d %d\n",a,m,P,h);
	}
	return 0;
}


#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#define For(i,x,y) for (i=x;i<=y;i++)
using namespace std;
int i,j,k,n,m,a,P,h;
int cal(int m,int n,int p) {
	if (n==0) return m;
	if (1ll*n*m<p) return max(P==p?0:p-n*m,m);
	int ge=1ll*m*n/p,C=1ll*n*m%p,len=p/m*m;
	if (C+m<=len) ge--;
	return cal(m-p%m,ge,m);
}
int main() {
	scanf("%d",&n);
	For(i,1,n) {
		scanf("%d%d%d%d",&a,&m,&P,&h);
		if (i<10000) printf("%s\n",cal(a%P,m,P)<=h?"YES":"NO");
		if (i==79) printf("%d %d %d %d\n",a,m,P,h);
	}
	return 0;
}


#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#define For(i,x,y) for (i=x;i<=y;i++)
using namespace std;
int i,j,k,n,m,a,P,h;
int cal(int m,int n,int p) {
	if (n==0) return p;
	if (1ll*n*m<p) return max(P==p?0:p-n*m,m);
	int ge=1ll*m*n/p+(1ll*m*n%p>0),C=1ll*n*m%p,len=p/m*m;
	if (C+m<=len) ge--;
	return cal(m-p%m,ge-1,m);
}
int main() {
	scanf("%d",&n);
	For(i,1,n) {
		scanf("%d%d%d%d",&a,&m,&P,&h);
		printf("%s\n",cal(a%P,m,P)<=h?"YES":"NO");
		//if (i==79) printf("%d %d %d %d\n",a,m,P,h);
	}
	return 0;
}


#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#define For(i,x,y) for (i=x;i<=y;i++)
using namespace std;
int i,j,k,n,m,a,P,h;
int cal(int m,int n,int p) {
	if (n==0) return m;
	if (1ll*n*m<p) return max(P==p?0:p-n*m,m);
	int ge=1ll*m*n/p,C=1ll*n*m%p,len=p/m*m;
	if (C+m<=len) ge--;
	return cal(m-p%m,ge,m);
}
int main() {
	scanf("%d",&n);
	For(i,1,n) {
		scanf("%d%d%d%d",&a,&m,&P,&h);
		printf("%s\n",cal(a%P,m,P)<=h?"YES":"NO");
	}
	return 0;
}


#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#define For(i,x,y) for (i=x;i<=y;i++)
using namespace std;
int i,j,k,n,m,a,P,h;
int cal(int m,int n,int p) {
	if (n==0) return m;
	if (1ll*n*m<p) return max(P==p?0:p-n*m,m);
	int ge=1ll*m*n/p,C=1ll*n*m%p,len=p/m*m;
	if (C+m<=len) ge--;
	return cal(m-p%m,ge,m);
}
int main() {
	scanf("%d",&n);
	For(i,1,n) {
		scanf("%d%d%d%d",&a,&m,&P,&h);
		printf("%s\n",cal(a,m,P)<=h?"YES":"NO");
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;

int a, p, h, n;

int opt(int a, int n, int p, int u)
{
	if(n<=0) return u?p:0;
	if(LL(n)*a < p) return max(a, u?p-n*a:0);

	int _n = (LL(n) * a) / p;
	if(LL(n)*a%p <= (p/a)*a-a) _n --;
	int _a = min(a - p%a, p%a);
	return opt(_a, _n, a, 1);
}

int main()
{
	scanf("%d", &n);
	while(scanf("%d%d%d%d", &a, &n, &p, &h) > 0)
	{
		a %= p;
		if(opt(a, n, p, 0) > h) puts("NO"); else puts("YES");
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;

int a, p, h, n, bp;

int opt(int a, int n, int p, int u)
{
	if(n<=0) return u?p:0;
	if(LL(n)*a < p) return max(a, u?p-n*a:0);

	int _n = (LL(n) * a) / p;
	if(LL(n)*a%p <= bp) _n --;
	int _a = min(a - p%a, p%a);
	return opt(_a, _n, a, 1);
}

int main()
{

	scanf("%d", &n);
	while(scanf("%d%d%d%d", &a, &n, &p, &h) > 0)
	{
		bp = (p / a) * a - a;
		if(opt(a, n, p, 0) > h) puts("NO"); else puts("YES");
	}

	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;

int a, p, h, n, bp;

int opt(int a, int n, int p, int u)
{
	if(n<=0) return u?p:0;
	if(LL(n)*a < p) return max(a, u?p-n*a:0);

	int _n = (LL(n) * a) / p;
	if(LL(n)*a%p <= bp) _n --;
	int _a = min(a - p%a, p%a);
	return opt(_a, _n, a, 1);
}

int main()
{

	scanf("%d", &n);
	while(scanf("%d%d%d%d", &a, &n, &p, &h) > 0)
	{
		a %= p, bp = (p / a) * a - a;
		if(opt(a, n, p, 0) > h) puts("NO"); else puts("YES");
	}

	return 0;
}


#include <algorithm>
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

typedef long long ll;

const int inf = 0x3f3f3f3f;
const ll oo = 0x3f3f3f3f3f3f3f3fll;

const int N = 1000010;

#define pb push_back
#define sz(x) ((x).size())

int dfs(int a, int n, int p, int top){
    if(1ll * a * n < p){
        int ans = a;
        if(top) ans = max(ans, p - a * n);
        return ans;
    }
    int cnt = 1ll * a * n / p;
    if((p / a - 1) * a > 1ll * a * n % p) cnt--;
    return dfs(min(p - p % a, p % a), cnt, a, 1);
}

int main(){
    int ca;
    int n, a, p, h;
    for(scanf("%d", &ca); ca--; ){
        scanf("%d%d%d%d", &a, &n, &p, &h);
        if(dfs(a % p, n, p, 0) > h) puts("NO");
        else puts("YES");
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
int T,a,n,p,h,f;
int work(int a,int n,int p){
    int t=(long long)a*n/p;
    if(!t){
        if(!f)return a;
        return max(a,p-a*n);
    }
    f=1;
    int a_,n_,p_;
    a_=min(p%a,a-p%a);
    n_=t;
    p_=a;
    if((long long)a*n%p < a*(p/a-1))n_--;
    return work(a_,n_,p_);
}
int main(){
    for(scanf("%d",&T);T--;){
        scanf("%d%d%d%d",&a,&n,&p,&h);
        f=0;
        puts(work(a%p,n,p)<=h?"YES":"NO");
    }
}


#include <cstdio>
#include <algorithm>
long long a,n,p,h;
int T;
long long f(long long a,long long n,long long p){
    long long t = a * n / p;
    if (a * n < p) return std::max(a,p - a * n);
    if (a * n % p < a * (p / a - 1)) --t;
    return f(std::min(p - p % a,p % a),t,a);
}
int main(){
    scanf("%d",&T);
    while (T--) {
        scanf("%I64d%I64d%I64d%I64d",&a,&n,&p,&h);
        a %= p;
        if (a * n < p) puts(a > h ? "NO" : "YES");
        else puts(f(a,n,p) > h ? "NO" : "YES");
    }
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
using namespace std;
typedef long long LL;
int T,a,n,p,h;

int get_max(LL a,LL n,LL p) {
	LL k=a*n/p;
	if (!k) return max(a,p-a*n);
	if (a*n%p<a*(p/a-1)) --k;
	return get_max(min(a-p%a,p%a),k,a);
}

int main() {
	for (scanf("%d",&T);T;T--) {
		scanf("%d%d%d%d",&a,&n,&p,&h); a%=p;
		int max_step=LL(a)*n<p?a:get_max(a,n,p);
		puts(max_step<=h?"YES":"NO");
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
using namespace std;
typedef long long LL;
int T,a,n,p,h;

int find(int a,int n,int p) {
	LL k=LL(a)*n/p;
	if (!k) return max(a,p-a*n);
	if (LL(a)*n%p<a*LL(p/a-1)) --k;
	return find(min(a-p%a,p%a),k,a);
}

int main() {
	for (scanf("%d",&T);T;T--) {
		scanf("%d%d%d%d",&a,&n,&p,&h);
		int max_step=LL(a)*n<p?a:find(a,n,p);
		puts(max_step<=h?"YES":"NO");
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define M 100100
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
int ABS(int u) {return u > 0 ? u : -u;}

ll func(ll a, ll n, ll p) {
    if (a * n <= p) {
        return max(a, p - n * a);
    }
    ll m = (p / a);
    return func((m + 1) * a - p, n * a / p, a);
}

int main() {
    //freopen("E.in", "r", stdin);
    int T;
    ll a, n, p, h;
    for (scanf("%d", &T); T--; ) {
        scanf("%I64d %I64d %I64d %I64d", &a, &n, &p, &h);
        if (a * n <= p) {
            puts(a > h ? "NO" : "YES");
            continue;
        }
        puts(func(a, n, p) > h ? "NO" : "YES");
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define M 100100
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
int ABS(int u) {return u > 0 ? u : -u;}

ll func(ll a, ll n, ll p) {
    if (a * n <= p) {
        return max(a, p - n * a);
    }
    ll m = (p / a);
    return func((m + 1) * a - p, n * a / p, a);
    return func((m + 1) * a - p, (n * a + a) / p - 1, a);
}

int main() {
    //freopen("E.in", "r", stdin);
    int T;
    ll a, n, p, h;
    for (scanf("%d", &T); T--; ) {
        scanf("%I64d %I64d %I64d %I64d", &a, &n, &p, &h);
        a %= p;
        if (a * n <= p) {
            puts(a > h ? "NO" : "YES");
            continue;
        }
        puts(func(a, n, p) > h ? "NO" : "YES");
    }
    return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
int T,a,n,p,h,ans;

int work(LL a,LL n,LL p)
{
	LL c=a*n/p;
	if(!c)return max(a,p-a*n);
	if(a*n%p<a*(p/a-1))c--;
	return work(a-p%a,c,a);
}

int main()
{
	for(scanf("%d",&T);T--;)
	{
		scanf("%d%d%d%d",&a,&n,&p,&h);a%=p;
		ans=(LL)a*n<p?a:work(a,n,p);
		puts(h<ans?"NO":"YES");
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define M 100100
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
int ABS(int u) {return u > 0 ? u : -u;}
/**
int work(LL a,LL n,LL p)
{
	LL c=a*n/p;
	if(!c)return max(a,p-a*n);
	if(a*n%p<a*(p/a-1))c--;
	return work(min(a-p%a,p%a),c,a);
}
*/
ll func(ll a, ll n, ll p) {
    if (a * n <= p) {
        return max(a, p - n * a);
    }
    ll m = (p / a);
    //return func((m + 1) * a - p, n * a / p, a);
    return func((m + 1) * a - p, (n * a / p) - (a*n%p < a*(p/a-1)), a);
}

int main() {
    //freopen("E.in", "r", stdin);
    int T;
    ll a, n, p, h;
    for (scanf("%d", &T); T--; ) {
        scanf("%I64d %I64d %I64d %I64d", &a, &n, &p, &h);
        a %= p;
        if (a * n <= p) {
            puts(a > h ? "NO" : "YES");
            continue;
        }
        puts(func(a, n, p) > h ? "NO" : "YES");
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define M 100100
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
int ABS(int u) {return u > 0 ? u : -u;}
/**
int work(LL a,LL n,LL p)
{
	LL c=a*n/p;
	if(!c)return max(a,p-a*n);
	if(a*n%p<a*(p/a-1))c--;
	return work(min(a-p%a,p%a),c,a);
}
*/
ll func(ll a, ll n, ll p) {
    if (a * n <= p) {
        return max(a, p - n * a);
    }
    ll m = (p / a);
    //printf("a = %I64d, n = %I64d, p = %I64d\n", a, n, p);
    //printf("%I64d %I64d\n", a * n % p, a * (p/a - 1));
    //printf("%I64d %I64d\n", (a*n+a + (p%a))/p, (a*n/p));
    return func((m + 1) * a - p, (n * a / p) - ((a*n+a+(p%a))/p == (a*n/p)), a);
    return func((m + 1) * a - p, (n * a / p) - (a*n%p<a*(p/a-1)), a);
}

int main() {
    //freopen("E.in", "r", stdin);
    int T;
    ll a, n, p, h;
    for (scanf("%d", &T); T--; ) {
        scanf("%I64d %I64d %I64d %I64d", &a, &n, &p, &h);
        a %= p;
        if (a * n <= p) {
            puts(a > h ? "NO" : "YES");
            continue;
        }
        //printf("%I64d\n", func(a, n, p));
        puts(func(a, n, p) > h ? "NO" : "YES");
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define M 100100
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
int ABS(int u) {return u > 0 ? u : -u;}

ll func(ll a, ll n, ll p) {
    if (a * n <= p) {
        return max(a, p - n * a);
    }
    ll m = (p / a);
    return func((m + 1) * a - p, n / (m + 1), a);
}

int main() {
    //freopen("E.in", "r", stdin);
    int T;
    ll a, n, p, h;
    for (scanf("%d", &T); T--; ) {
        scanf("%I64d %I64d %I64d %I64d", &a, &n, &p, &h);
        if (a * n <= p) {
            puts(a > h ? "NO" : "YES");
            continue;
        }
        puts(func(a, n, p) > h ? "NO" : "YES");
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;

int T,a,n,p,h,i,j,k,ans;

int cal(int x,int y,int z)
{
	if((long long)x*y<z)
	{
		if(z==p)return x;
		else return max(x,z-x*y);
	}
	int tmp=(long long)x*y/z;
	if((long long)x*y%z+x<=z/x*x)--tmp;
	return cal(min(x-z%x,z%x),tmp,x);
}

int main()
{
	scanf("%d",&T);
	for(;T;--T)
	{
		scanf("%d%d%d%d",&a,&n,&p,&h);
		if(cal(a%p,n,p)<=h)printf("YES\n");
		else printf("NO\n");
	}
}


/*** keep hungry and calm CoolGuang!***/
#pragma GCC optimize(2)
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#include <bits/stdc++.h>
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<string.h>
#include<iostream>
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll INF=1e17;
const int maxn=1e6+6;
const int mod=998244353;
const double eps=1e-3;
inline bool read(ll &num)
{char in;bool IsN=false;
    in=getchar();if(in==EOF) return false;while(in!='-'&&(in<'0'||in>'9')) in=getchar();if(in=='-'){ IsN=true;num=0;}else num=in-'0';while(in=getchar(),in>='0'&&in<='9'){num*=10,num+=in-'0';}if(IsN) num=-num;return true;}
ll n,m,p;
int sz[maxn],mx[maxn],rt=0,sum;
int vis[maxn],dfn=0;
ll t[maxn*5];
vector<int>v[maxn];
ll res[maxn];
char str[maxn];
int c[maxn];
bool f[maxn*5];
void getrt(int u,int fa){
    mx[u]=0;sz[u]=1;
    for(int e:v[u]){
        if(e==fa||vis[e]) continue;
        getrt(e,u);
        sz[u]+=sz[e];
        mx[u]=max(mx[u],sz[e]);
    }
    mx[u]=max(mx[u],sum-sz[u]);
    if(mx[rt]>mx[u]) rt = u;
}
void getdis(int u,int fa,int s,int val){
    if(u!=fa) t[s]+=val;
    for(int e:v[u]){
        if(e==fa||vis[e]) continue;
        getdis(e,u,s^c[e],val);
    }
}
ll tempy = 0,tempx = 0;
ll go(int u,int fa,int s,int val){
    ll ans = t[s^val];
   // printf("%d %d\n",u,s);
    for(int i=0;i<20;i++){
        int x = 1<<i;
        ans+=t[s^x^val];
    }
    tempy+=ans;

    int judgex = s^val;
    //debug(judgex);
    if(f[judgex]){
        tempx++;
        ans++;
    }
    for(int e:v[u]){
        if(e==fa||vis[e]) continue;
        ans+=go(e,u,s^c[e],val);
    }
    res[u]+=ans;
    return ans;
}
void calc(int u){
   // printf("--------------------%d\n",u);
    getdis(u,u,0,1);
  //  for(int i=0;i<=4;i++)
  //      printf("%d : %d\n",i,t[i]);
    tempy = tempx = 0;
    for(int e:v[u]){
        if(vis[e]) continue;
        getdis(e,u,c[e],-1);
   //     printf("%d:\n",e);
   //     for(int i=0;i<=4;i++)
  //          printf("%d : %d\n",i,t[i]);
        go(e,u,c[e],c[u]);
        getdis(e,u,c[e],1);
    }
 //   debug(tempx);
  //  debug(tempy);
    res[u]+=tempy/2+tempx;

    getdis(u,u,0,-1);

}
void Solve(int u){
   // debug(u);
   getrt(u,u);
    vis[u]=1;calc(u);
    for(int e:v[u]){
        if(vis[e]) continue;
        sum = sz[e];mx[rt = 0] = n;
        getrt(e,u);
        Solve(rt);
    }
}
int main(){
    read(n);
    for(int i=1;i<=n-1;i++){
        int x,y;scanf("%d%d",&x,&y);
        v[x].push_back(y);
        v[y].push_back(x);
    }
    f[0]=true;
    for(int i=0;i<20;i++) f[1<<i]=true;
    scanf("%s",str+1);
    for(int i=1;i<=n;i++){
        int x = str[i]-'a';
        c[i] = 1<<x;
    }
    ///restart is OK
    sum = mx[rt=0]=n;
    getrt(1,0);
    Solve(rt);

    for(int i=1;i<=n;i++) printf("%lld ",res[i]+1);
    return 0;
}
/**

**/


/*** keep hungry and calm CoolGuang!***/
#pragma GCC optimize(2)
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#include <bits/stdc++.h>
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<string.h>
#include<iostream>
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll INF=1e17;
const int maxn=1e6+6;
const int mod=998244353;
const double eps=1e-3;
inline bool read(ll &num)
{char in;bool IsN=false;
    in=getchar();if(in==EOF) return false;while(in!='-'&&(in<'0'||in>'9')) in=getchar();if(in=='-'){ IsN=true;num=0;}else num=in-'0';while(in=getchar(),in>='0'&&in<='9'){num*=10,num+=in-'0';}if(IsN) num=-num;return true;}
ll n,m,p;
int sz[maxn],mx[maxn],rt=0,sum;
int vis[maxn],dfn=0;
ll t[maxn*5];
vector<int>v[maxn];
ll res[maxn];
char str[maxn];
int c[maxn];
bool f[maxn*5];
void getrt(int u,int fa){
    mx[u]=0;sz[u]=1;
    for(int e:v[u]){
        if(e==fa||vis[e]) continue;
        getrt(e,u);
        sz[u]+=sz[e];
        mx[u]=max(mx[u],sz[e]);
    }
    mx[u]=max(mx[u],sum-sz[u]);
    if(mx[rt]>mx[u]) rt = u;
}
void getdis(int u,int fa,int s,int val){
    if(u!=fa) t[s]+=val;
    for(int e:v[u]){
        if(e==fa||vis[e]) continue;
        getdis(e,u,s^c[e],val);
    }
}
ll tempy = 0,tempx = 0;
ll go(int u,int fa,int s,int val){
    ll ans = t[s^val];
   // printf("%d %d\n",u,s);
    for(int i=0;i<20;i++){
        int x = 1<<i;
        ans+=t[s^x^val];
    }
    tempy+=ans;

    int judgex = s^val;
    //debug(judgex);
    if(f[judgex]){
        tempx++;
        ans++;
    }
    for(int e:v[u]){
        if(e==fa||vis[e]) continue;
        ans+=go(e,u,s^c[e],val);
    }
    res[u]+=ans;
    return ans;
}
void calc(int u){
   // printf("--------------------%d\n",u);
    getdis(u,u,0,1);
  //  for(int i=0;i<=4;i++)
  //      printf("%d : %d\n",i,t[i]);
    tempy = tempx = 0;
    for(int e:v[u]){
        if(vis[e]) continue;
        getdis(e,u,c[e],-1);
   //     printf("%d:\n",e);
   //     for(int i=0;i<=4;i++)
  //          printf("%d : %d\n",i,t[i]);
        go(e,u,c[e],c[u]);
        getdis(e,u,c[e],1);
    }
 //   debug(tempx);
  //  debug(tempy);
    res[u]+=tempy/2+tempx;

    getdis(u,u,0,-1);

}
void Solve(int u){
   // debug(u);
    vis[u]=1;calc(u);
    for(int e:v[u]){
        if(vis[e]) continue;
        sum = sz[e];mx[rt = 0] = n;
        getrt(e,u);
        Solve(rt);
    }
}
int main(){
    read(n);
    for(int i=1;i<=n-1;i++){
        int x,y;scanf("%d%d",&x,&y);
        v[x].push_back(y);
        v[y].push_back(x);
    }
    f[0]=true;
    for(int i=0;i<20;i++) f[1<<i]=true;
    scanf("%s",str+1);
    for(int i=1;i<=n;i++){
        int x = str[i]-'a';
        c[i] = 1<<x;
    }
    ///restart is OK
    sum = mx[rt=0]=n;
    getrt(1,0);
    getrt(rt,rt);
    Solve(rt);

    for(int i=1;i<=n;i++) printf("%lld ",res[i]+1);
    return 0;
}
/**

**/


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

const int MAXN=2e5+10;
const int MAXMASK=(1<<20);

bool ok[MAXN];
int sz[MAXN];
long long pat[MAXN];
int cnt[MAXMASK];
int msk[MAXN];
int val[MAXN];
long long bad[MAXN];
long long ans[MAXN];
vector<int> g[MAXN];
string s;

void prep(int u,int p)
{
    sz[u]=1;
    for(auto v:g[u])
        if(ok[v]&&v!=p)
            prep(v,u),sz[u]+=sz[v];
}

int centroid(int u,int r)
{
    for(auto v:g[u])
        if(ok[v]&&sz[v]<sz[u]&&sz[v]>=sz[r]/2)
            return centroid(v,r);
    return u;
}

void add(int u,int p)
{
    msk[u]^=(1<<val[u]);
    cnt[msk[u]]++;
    for(auto v:g[u])
        if(ok[v]&&v!=p)
            msk[v]=msk[u],add(v,u);
}

void rem(int u,int p)
{
    cnt[msk[u]]--;
    pat[u]=0;
    bad[u]=0;
    msk[u]=0;
    for(auto v:g[u])
        if(ok[v]&&v!=p)
            rem(v,u);
}

void calc(int u,int p,int mask)
{
    mask^=(1<<val[u]);
    pat[u]+=cnt[mask];
    bad[u]+=cnt[mask];
    if(mask==0||__builtin_popcount(mask)==1)
        pat[u]++;
    for(int i=0;i<20;i++)
        pat[u]+=cnt[mask^(1<<i)],bad[u]+=cnt[mask^(1<<i)];
    for(auto v:g[u])
        if(ok[v]&&v!=p)
            calc(v,u,mask),pat[u]+=pat[v],bad[u]+=bad[v];
    ans[u]+=pat[u];
}

void solve(int u)
{
    prep(u,0);
    u=centroid(u,u);
    for(auto v:g[u])
        if(ok[v])
            add(v,u);
    for(auto v:g[u])
    {
        if(ok[v])
        {
            rem(v,u);
            calc(v,u,(1<<val[u]));
            pat[u]+=pat[v];
            bad[u]+=bad[v];
            add(v,u);
        }
    }
    for(auto v:g[u])
        if(ok[v])
            rem(v,u);
    ans[u]+=pat[u]-bad[u]/2+1;
    pat[u]=0;
    bad[u]=0;
    ok[u]=false;
    for(auto v:g[u])
        if(ok[v])
            solve(v);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,u,v;
    cin>>n;
    for(int i=1;i<n;i++)
        cin>>u>>v,g[u].emplace_back(v),g[v].emplace_back(u);
    cin>>s;
    for(int i=0;i<n;i++)
        val[i+1]=s[i]-'a',ok[i+1]=true;
    solve(1);
    for(int i=1;i<=n;i++)
        cout<<ans[i]<<" ";
}


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

const long long MAXN=1e6+10;

bool ok[MAXN];
long long sz[MAXN];
long long pat[MAXN];
long long cnt[MAXN];
long long msk[MAXN];
long long val[MAXN];
long long bad[MAXN];
long long ans[MAXN];
vector<long long> g[MAXN];
string s;

void prep(long long u,long long p)
{
    sz[u]=1;
    for(auto v:g[u])
        if(ok[v]&&v!=p)
            prep(v,u),sz[u]+=sz[v];
}

long long centroid(long long u,long long r)
{
    for(auto v:g[u])
        if(ok[v]&&sz[v]<sz[u]&&sz[v]>=sz[r]/2)
            return centroid(v,r);
    return u;
}

void add(long long u,long long p)
{
    msk[u]^=(1<<val[u]);
    cnt[msk[u]]++;
    for(auto v:g[u])
        if(ok[v]&&v!=p)
            msk[v]=msk[u],add(v,u);
}

void rem(long long u,long long p)
{
    cnt[msk[u]]--;
    pat[u]=0;
    bad[u]=0;
    msk[u]=0;
    for(auto v:g[u])
        if(ok[v]&&v!=p)
            rem(v,u);
}

void clr(long long u,long long p)
{
    pat[u]=0;
    bad[u]=0;
    msk[u]=0;
    for(auto v:g[u])
        if(ok[v]&&v!=p)
            clr(v,u);
}

void calc(long long u,long long p,long long mask)
{
    mask^=(1<<val[u]);
    pat[u]+=cnt[mask];
    bad[u]+=cnt[mask];
    if(mask==0||__builtin_popcount(mask)==1)
        pat[u]++;
    for(long long i=0;i<20;i++)
        pat[u]+=cnt[mask^(1<<i)],bad[u]+=cnt[mask^(1<<i)];
    for(auto v:g[u])
        if(ok[v]&&v!=p)
            calc(v,u,mask),pat[u]+=pat[v],bad[u]+=bad[v];
    ans[u]+=pat[u];
}

void solve(long long u)
{
    prep(u,0);
    u=centroid(u,u);
    for(auto v:g[u])
        if(ok[v])
            add(v,u);
    for(auto v:g[u])
    {
        if(ok[v])
        {
            rem(v,u);
            calc(v,u,(1<<val[u]));
            pat[u]+=pat[v];
            bad[u]+=bad[v];
            clr(v,u);
            add(v,u);
        }
    }
    for(auto v:g[u])
        if(ok[v])
            rem(v,u);
    ans[u]+=pat[u]-bad[u]/2+1;
    pat[u]=0;
    bad[u]=0;
    ok[u]=false;
    for(auto v:g[u])
        if(ok[v])
            solve(v);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long n,u,v;
    cin>>n;
    for(long long i=1;i<n;i++)
        cin>>u>>v,g[u].emplace_back(v),g[v].emplace_back(u);
    cin>>s;
    for(long long i=0;i<n;i++)
        val[i+1]=s[i]-'a',ok[i+1]=true;
    solve(1);
    for(long long i=1;i<=n;i++)
        cout<<ans[i]<<" ";
}


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

const long long MAXN=1e6+10;

bool ok[MAXN];
long long sz[MAXN];
long long pat[MAXN];
long long cnt[MAXN];
long long msk[MAXN];
long long val[MAXN];
long long bad[MAXN];
long long ans[MAXN];
vector<long long> g[MAXN];
string s;

void prep(long long u,long long p)
{
    sz[u]=1;
    for(auto v:g[u])
        if(ok[v]&&v!=p)
            prep(v,u),sz[u]+=sz[v];
}

long long centroid(long long u,long long r)
{
    for(auto v:g[u])
        if(ok[v]&&sz[v]<sz[u]&&sz[v]>=sz[r]/2)
            return centroid(v,r);
    return u;
}

void add(long long u,long long p)
{
    msk[u]^=(1<<val[u]);
    cnt[msk[u]]++;
    for(auto v:g[u])
        if(ok[v]&&v!=p)
            msk[v]=msk[u],add(v,u);
}

void rem(long long u,long long p)
{
    cnt[msk[u]]--;
    pat[u]=0;
    bad[u]=0;
    msk[u]=0;
    for(auto v:g[u])
        if(ok[v]&&v!=p)
            rem(v,u);
}

void clr(long long u,long long p)
{
    pat[u]=0;
    bad[u]=0;
    msk[u]=0;
    for(auto v:g[u])
        if(ok[v]&&v!=p)
            clr(v,u);
}

void calc(long long u,long long p,long long mask)
{
    mask^=(1<<val[u]);
    pat[u]+=cnt[mask];
    bad[u]+=cnt[mask];
    if(mask==0||__builtin_popcount(mask)==1)
        pat[u]++;
    for(long long i=0;i<20;i++)
        pat[u]+=cnt[mask^(1<<i)],bad[u]+=cnt[mask^(1<<i)];
    for(auto v:g[u])
        if(ok[v]&&v!=p)
            calc(v,u,mask),pat[u]+=pat[v];
    ans[u]+=pat[u];
}

void solve(long long u)
{
    prep(u,0);
    u=centroid(u,u);
    for(auto v:g[u])
        if(ok[v])
            add(v,u);
    for(auto v:g[u])
    {
        if(ok[v])
        {
            rem(v,u);
            calc(v,u,(1<<val[u]));
            pat[u]+=pat[v];
            bad[u]+=bad[v];
            clr(v,u);
            add(v,u);
        }
    }
    for(auto v:g[u])
        if(ok[v])
            rem(v,u);
    ans[u]+=pat[u]-bad[u]/2+1;
    pat[u]=0;
    bad[u]=0;
    ok[u]=false;
    for(auto v:g[u])
        if(ok[v])
            solve(v);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long n,u,v;
    cin>>n;
    for(long long i=1;i<n;i++)
        cin>>u>>v,g[u].emplace_back(v),g[v].emplace_back(u);
    cin>>s;
    for(long long i=0;i<n;i++)
        val[i+1]=s[i]-'a',ok[i+1]=true;
    solve(1);
    for(long long i=1;i<=n;i++)
        cout<<ans[i]<<" ";
}