#C1695. Distinct Project Count After Merges

    ID: 44928 Type: Default 1000ms 256MiB

Distinct Project Count After Merges

Distinct Project Count After Merges

An employee has worked on several projects represented by lowercase letters. Over time, some projects have been merged together. Each merge operation is represented by a pair of lowercase letters and indicates that the two projects should be considered as one. Given a string s representing the projects the employee worked on and a list of merge operations, your task is to determine the number of distinct projects (after considering all merges) that the employee has worked on.

For example, if s = "abcdab" and there are merge operations merging 'a' with 'b' and 'c' with 'd', then the effective groups become {a, b} and {c, d}. Therefore, the distinct project count is 2.

The merge operations are symmetric and transitive. In other words, if project a is merged with project b and project b is merged with project c, then all three projects are considered the same.

inputFormat

The input is read from stdin and has the following format:

  1. A line containing a non-empty string s consisting of lowercase letters, representing the projects.
  2. A line containing an integer m (m ≥ 0), representing the number of merge operations.
  3. Then, m lines follow; each line contains two space-separated lowercase letters, representing a merge operation between the two projects.

outputFormat

Output the number of distinct projects after performing all merge operations to stdout.

## sample
abcdab
2
a b
c d
2