import random
import sys

keys = []
queryFile = 'queries_sort_G' + sys.argv[1] + '_p' + sys.argv[2] + '.txt'
shaffleFile = 'queries' + sys.argv[1] + '_p' + sys.argv[2] + '.txt'

fQuery = open(queryFile, 'r')
fShaffle = open(shaffleFile, 'w')

for line in fQuery:
    tokens = line.split()
    for i in range (0, int(tokens[1])):
        keys.append(tokens[0])    

#Fisher-Yates shuffle

for i in reversed(range(1, len(keys))):
    j = random.randrange(i + 1)
    keys[i], keys[j] = keys[j], keys[i]

for j in range (0, len(keys)):
    fShaffle.write(keys[j] + "\n")

fQuery.close()
fShaffle.close()
