This page is still under construction.

Parts of this page are still being built. What you see may change.

Folder Cleanup (small)

Interview

Time limit1sMemory limit1024 MB

Summary
Given a folder tree with files, answer queries that ask, for each folder, how many distinct file names and how many total files sit under it.
Level

Medium5 of 10

Topics
Tree, DFS, Hash map, String
Solved
No attempts yet

Problem

The main folder contains various files and folders.

main
 ├─ FolderA
 │    ├─ File1
 │    └─ File2
 └─ FolderB
       ├─ FolderC
       ├─ File1
       └─ File3

The structure above shows the hierarchy under the main folder. FolderA, FolderB, and FolderC are folders, and File1, File2, and File3 are files. Files with the same name are the same file.

A folder cannot contain two or more files with the same name.

A directory under main cannot contain two or more folders with the same name.

To clean up the folders, you want to inspect the files under the main folder.

Write a program that answers queries about folders and files.

Input

The first line gives the total number of folders NN and the total number of files MM under the main folder, separated by a space.

Lines 2 through N+M+1N + M + 1 each give the name of the parent folder PP, the name of the folder or file FF, and a value CC indicating whether it is a folder, separated by spaces.

CC is 1 if FF is a folder and 0 if FF is a file.

Line N+M+2N + M + 2 gives the number of queries QQ.

The next QQ lines each contain a query. Each query gives the path of a folder starting from main. For example, if a query is about FolderB inside the main folder, it is given as main/FolderB, the path of FolderB. The path given in a query is guaranteed to contain an existing folder.

Output

For each query, in order, print on one line the number of distinct file names and the total number of files under that folder.

The number of distinct file names counts identical files as one. The total number of files counts each file separately even if there are identical files.

For example, if there are 5 files named File1, the number of distinct file names is 1 and the total number of files is 5.

Constraints

  • 1≤N≤1,0001 \le N \le 1,000
  • 1≤M≤1,0001 \le M \le 1,000
  • 1≤∣P∣≤101 \le |P| \le 10
  • 1≤∣F∣≤101 \le |F| \le 10
  • 0≤C≤10 \le C \le 1
  • 1≤Q≤1,0001 \le Q \le 1,000
  • PP and FF consist only of uppercase and lowercase English letters and digits.

Examples2

  1. Example 1

    Input
    3 4
    main FolderA 1
    main FolderB 1
    FolderA File1 0
    FolderA File2 0
    FolderB FolderC 1
    FolderB File1 0
    FolderB File3 0
    4
    main
    main/FolderA
    main/FolderB
    main/FolderB/FolderC
    
    Expected output
    3 4
    2 2
    2 2
    0 0
    
  2. Example 2

    Input
    4 1
    main FolderA 1
    FolderA FolderB 1
    FolderB FolderC 1
    FolderC FolderD 1
    FolderD File1 0
    3
    main
    main/FolderA
    main/FolderA/FolderB/FolderC/FolderD
    
    Expected output
    1 1
    1 1
    1 1