Algorithm


A. Everyone Loves to Sleep
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vlad, like everyone else, loves to sleep very much.

Every day Vlad has to do n things, each at a certain time. For each of these things, he has an alarm clock set, the i-th of them is triggered on hiℎ� hours mi�� minutes every day (0hi<24,0mi<600≤ℎ�<24,0≤��<60). Vlad uses the 2424-hour time format, so after h=12,m=59ℎ=12,�=59 comes h=13,m=0ℎ=13,�=0 and after h=23,m=59ℎ=23,�=59 comes h=0,m=0ℎ=0,�=0.

This time Vlad went to bed at H hours M minutes (0H<24,0M<600≤�<24,0≤�<60) and asks you to answer: how much he will be able to sleep until the next alarm clock.

If any alarm clock rings at the time when he went to bed, then he will sleep for a period of time of length 00.

Input

The first line of input data contains an integer t (1t1001≤�≤100) — the number of test cases in the test.

The first line of the case contains three integers nH and M (1n10,0H<24,0M<601≤�≤10,0≤�<24,0≤�<60) — the number of alarms and the time Vlad went to bed.

The following n lines contain two numbers each hiℎ� and mi�� (0hi<24,0mi<600≤ℎ�<24,0≤��<60) — the time of the i alarm. It is acceptable that two or more alarms will trigger at the same time.

Numbers describing time do not contain leading zeros.

Output

Output t lines, each containing the answer to the corresponding test case. As an answer, output two numbers  — the number of hours and minutes that Vlad will sleep, respectively. If any alarm clock rings at the time when he went to bed, the answer will be 0 0.

Example
input
Copy
3
1 6 13
8 0
3 6 0
12 30
14 45
6 0
2 23 35
20 15
10 30
output
Copy
1 47
0 0
10 55

 



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

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


#define ll long long
#define endl '\n'
#define debug(n) cout<<(n)<<endl;
const ll INF = 2e18 + 99;


int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t;
  cin>>t;
  while(t--){
    int n;
    cin>>n;
    int H, M;
    cin>>H>>M;
    int h, m;
    int timeh, timem, mintimeh = 24, mintimem = 60;
    while(n--){
      cin>>h>>m;
      timeh = h - H;
      timem = m - M;
      if(timem < 0){
        timem += 60;
        timeh--;
      }
      if(timeh < 0){
        timeh += 24;
      }
      if(timeh < mintimeh){
        mintimeh = timeh;
        mintimem = timem;
      }
      else if(timeh == mintimeh && timem < mintimem){
        mintimem = timem;
      }

    }
    cout<<mintimeh<<" "<<mintimem<<endl;

  }

}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3
1 6 13
8 0
3 6 0
12 30
14 45
6 0
2 23 35
20 15
10 30

Output

x
+
cmd
1 47
0 0
10 55
Advertisements

Demonstration


Codeforcess Solution 1714-A A. Everyone Loves to Sleep ,C++, Java, Js and Python,1714-A,Codeforcess Solution

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+