Rescue Haibara Gym - 101778E

Haibara is one of Conan's best friends. Conan sent her as a spy in order to discover the killer in one of his investigations. Haibara was on her first mission as a spy. Unfortunately, her mission did not go as planned, and she has been compromised. Haibara got panicked and she forgot what she learned from Conan. So, it is your time to help her to avoid arrest. Your task is to help Haibara find the best safe house for her based on her current situation. There are n safe houses around Haibara's location. The ith house is di kilometers away from Haibara, and it contains mi coins.

According to the manual, the best safe house must be at distance no more than x and must contain at least y coins. If there is more than one safe house satisfying these conditions, you must choose the nearest one. If there is more than one safe house at the same distance, you must choose the one that contains the biggest amount of coins. Finally, if there is more than one safe house satisfying all the previous conditions, you must choose the one with minimum index.

Do not forget that Haibara is in a real danger, so you must find the best safe house for her as soon as possible. Conan will be very happy if you rescue Haibara. Can you do this?

Input

The first line contains an integer T (1 ≤ T ≤ 500), in which T is the number of test cases.

The first line of each test case contains three integers n, x, and y (1 ≤ n ≤ 500) (1 ≤ x, y ≤ 1000), in which n is the number of safe houses, and x and y are the values from the manual as described in the statement.

Then n lines follow, each line contains two integers di and mi (1 ≤ di, mi ≤ 1000), in which di is the distance to the ith safe house, and mi is the amount of coins it contains. Safe houses are numbered from 1 to n in the order given in the input.

Output

For each test case, print a single line containing  - 1 if there is no suitable safe house for Haibara. Otherwise, print the index of the safe house you have chosen for her.

Example

Input

2
3 5 3
4 2
7 1
5 7
4 4 6
3 7
3 8
2 4
5 2

Output

3
2

Note

In the second test case, you need to find a safe house at distance no more than 4 and must contain at least 6 coins. You can choose the 1st or 2nd safe houses, but according to the manual, the 2nd safe house is the most suitable.

AC代码(结构体排序)

Select Code

#include <iostream>
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
struct node
{
 int d, m, num;
}a[510];
int cmp(struct node xx, struct node yy)
{
 if(xx.d!=yy.d)
 return xx.d<yy.d;
 else if(xx.m!=yy.m)
 return xx.m>yy.m;
 else if(xx.num!=yy.num)
 return xx.num<yy.num;
 return -1;
}
int main()
{
    int t, i, n, x, y, u, v;
    scanf("%d",&t);
    while(t--)
    {
     int h = 0;
     scanf("%d %d %d", &n, &x, &y);
     for(i = 0;i<n;i++)
     {
      scanf("%d %d",&u, &v);
      if(u<=x&&v>=y)
      {
       a[h].num = i+1, a[h].d = u, a[h++].m = v;
      }
     }
     sort(a, a+h, cmp);
     if(h==0)
     printf("-1\n");
     else
     printf("%d\n",a[0].num);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值