#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long long MAX = 101;
const double PI = 3.1415926;
class Disjoint_Sets {
 public:
  long long* parent;
  long long* size;
  long long n;
  void Initialise() {
    parent = new long long[n];
    size = new long long[n];
    for (int i = 0; i < n; i++) parent[i] = i;
    for (int i = 0; i < n; i++) size[i] = 1;
  }
  void merge(long long a, long long b) {
    long long parent_a = find(a);
    long long parent_b = find(b);
    if (parent_a == parent_b) return;
    if (size[parent_a] < size[parent_b]) {
      parent[parent_a] = parent_b;
      size[parent_b] += size[parent_a];
      size[parent_a] = 0;
    } else {
      parent[parent_b] = parent_a;
      size[parent_a] += size[parent_b];
      size[parent_b] = 0;
    }
  }
  long long find(long long a) {
    if (parent[a] == a) {
      return a;
    }
    parent[a] = find(parent[a]);
    return parent[a];
  }
};
Disjoint_Sets allColor[MAX];
int N, m;
vector<pair<long long, long long> > adj[MAX];
bool visited[MAX];
void dfs(long long s) {
  visited[s] = 1;
  for (pair<long long, long long> x : adj[s]) {
    allColor[x.second].merge(s, x.first);
    if (!visited[x.first]) dfs(x.first);
  }
}
void dfsPre() {
  for (int i = 0; i < N; ++i)
    if (!visited[i]) dfs(i);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> N >> m;
  memset(visited, 0, sizeof(visited));
  for (int i = 0; i < MAX; ++i) {
    allColor[i].n = N;
    allColor[i].Initialise();
  }
  for (int i = 0; i < m; ++i) {
    int x, y, c;
    cin >> x >> y >> c;
    x--;
    y--;
    c--;
    adj[x].push_back(make_pair(y, c));
    adj[y].push_back(make_pair(x, c));
  }
  dfsPre();
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    int counter = 0;
    for (int j = 0; j < MAX; ++j)
      if (allColor[j].find(x) == allColor[j].find(y)) counter++;
    cout << counter << endl;
  }
  return 0;
}
