DIVEQL - The Magical Bag

no tags 

Dukkar has the magical bag of power 'P'. Here power 'P' of magical bag means any thing kept in the bag will be 'P' times.

Now Dukkar wanted to distribute equal number of Chocolates among his 'N' students using that magical bag in the following manner:-

Initially Dukkar has 'Z' chocolates and he give 'X' chocolates to first student and keep the remaining chocolates to magical bag so that it became 'P' times on next step, again he will take out 'X' chocolates from bag and give it to the second student and the remaining chocolates in the bag at this step will get 'P' time on the next step, this process continue.

Here you have to find minimum 'Z' so that at last step there are no chocolates in the magical bag (After giving 'X' chocolates to last student no chocolates should remain in bag)

Input

First line of input contain T (<100000) number of test cases and the following T lines will contain N (2<=N<=1018) and P (2<=P<=109).

Output

For each test case you have to print minimum 'Z' and corresponding 'X'. As answer can be large print answers modulo 1000000007.

(Z % 1000000007 and X % 1000000007)

Example

Input:
1
3 2

Output:
7 4

Explanation:

At Z=7, Initially Dukkar will give 4 chocolates to first student and keep 3 chocolates in bag. In the next step it became 6 now he gives 4 chocolates to second student. In the next step remaining 2 chocolates will be came 4 which he will give to third student. Now the bag became empty.


hide comments
P_Quantum: 2014-07-15 19:55:43

nice!!

:(){ :|: & };:: 2014-07-12 03:21:42

Time limit is a bit strict for python.

re(vamsi): there are AC submissions with Python

Last edit: 2015-01-17 12:37:21
Zeus: 2014-07-06 12:40:56

@Nishant Sir, i took into account the things you said and did some optimizations.
for i/p
2
1000000000000000000 1000000000
123456789786657 164387
o/p
**
but still its giving w.a. please help... id: 11897631

Edit(Nishant):- output of your programme is not correct.Check that.
for further query you can mail me @ ***

Last edit: 2014-07-11 04:51:47
to_test: 2014-07-05 23:17:51

could you give me the test case for which i am getting wrong answer, i have tested a lot,still WA

Edit(Nishant):- Your logic is not correct

Last edit: 2014-07-06 07:05:25
Zeus: 2014-07-05 22:47:05

@Nishant Sir, can you please tell me where is my code going wrong ??? please help... id: 11895118

Edit(Nishant):- you have to see the constrain then try some optimisation in your code your code will fail for
n=123456789786657 and p=164387 just try it

Edit: OK got it... thanxx...

Last edit: 2014-07-06 10:57:48
Rahul Kumar Dahmiwal: 2014-07-03 23:49:34

plese explain why my code is giving wrong answer
http://*****

Edit(Nishant):-don't post link of your code here.Your code has minor mistake correct them.

Last edit: 2014-07-04 05:00:27
BLANKRK: 2014-07-03 17:11:43

nice :)

अमूल्य: 2014-07-03 13:31:37

dukkar :)

_will: 2014-07-03 13:18:22

can someone please tell me where i am doing wrong, getting WA with this code,

http://*****

Last edit: 2014-07-04 14:05:20
pika_pika: 2014-07-02 19:43:49

good question @Nishant, similar concept to DIVISION or more like a part of it ;)

Last edit: 2014-07-02 19:44:47

Added by:NISHANT RAJ
Date:2014-07-01
Time limit:5s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:own